Java攔截器以及自定義注解的使用是怎么樣的,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
@Component
public class MyWebConfig implements WebMvcConfigurer {
private final UserTokenInterceptor userTokenInterceptor;
private final SecurityInterceptor securityInterceptor;
public MyWebConfig(
UserTokenInterceptor userTokenInterceptor, SecurityInterceptor securityInterceptor) {
this.userTokenInterceptor = userTokenInterceptor;
this.securityInterceptor = securityInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 定義排除swagger訪問的路徑配置
String[] swaggerExcludes =
new String[] {"/swagger-ui.html", "/swagger-resources/**", "/webjars/**"};
registry
.addInterceptor(userTokenInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(
"/user/login", "/static/**", "/*.html", "/*.ico", "/*.json", "/*.png", "/heartbeat/**")
.excludePathPatterns(swaggerExcludes);
registry
.addInterceptor(securityInterceptor)
.addPathPatterns("/maintain/**", "/user/**")
.excludePathPatterns("/user/login");
}
}2個處理的類請求上可以有交集,2個處理類都執行。
@Component
public class UserTokenInterceptor implements HandlerInterceptor {
private final EmpInfoService empInfoService;
public UserTokenInterceptor(EmpInfoService empInfoService) {
this.empInfoService = empInfoService;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 校驗handler是否是HandlerMethod
if (!(handler instanceof HandlerMethod)) {
return true;
}
// 從請求頭中獲取token
String token = request.getHeader("Authorization");
/**
* update:2021/11/30 ShengJieLi
* 增加邏輯:Authorization的值不為本系統生成的token時,解密Authorization,獲取token并驗證
*/
if (StrUtil.isNotEmpty(token)) {
EmpInfo securityEmployee = empInfoService.queryToken(token);
if(securityEmployee != null){
// token有效
String ref = empInfoService.isRef(token);
if (StrUtil.isNotBlank(ref)) {
response.setHeader("Access-Control-Expose-Headers", "token");
response.addHeader("token", ref);
}
}else{
//Authorization為PBE加密數據
securityEmployee = empInfoService.analyticQueryToken(token,response);
}
if (securityEmployee != null) {
// token有效
// 將User對象放入到ThreadLocal中
UserLocal.set(securityEmployee);
return true;
}
return false;
}
// String s = JSONUtil.toJsonStr(ResponseResult.error(ErrorCode.TOKEN_ERROR));
// response.setContentType("text/html;charset=UTF-8");
// JSONUtil.toJsonStr(s, response.getWriter());
// response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
InterceptorExceptionResolver.interceptorError(response,ErrorCode.TOKEN_ERROR);
//update 結束
return false;
}
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// 響應結束后刪除對象
UserLocal.remove();
}
}@SecurityGrade({"SUPER_ADMIN", "SYSTEM_ADMIN"})
public class SecurityController {
private final EmpInfoService empInfoService;
public SecurityController(EmpInfoService empInfoService) {
this.empInfoService = empInfoService;
}
@GetMapping("getUserInformation")
@ApiOperation("登陸用戶信息")
@NoAuthorization
public ResponseResult getUserInformation(@ApiIgnore HttpServletResponse response) {
return empInfoService.getUserInformation(response);
}
}@SecurityGrade({"SUPER_ADMIN", "SYSTEM_ADMIN"})
public class SecurityController {
private final EmpInfoService empInfoService;
public SecurityController(EmpInfoService empInfoService) {
this.empInfoService = empInfoService;
}
@GetMapping("getUserInformation")
@ApiOperation("登陸用戶信息")
@NoAuthorization
public ResponseResult getUserInformation(@ApiIgnore HttpServletResponse response) {
return empInfoService.getUserInformation(response);
}
}method.getMethodAnnotation(SecurityGrade.class) 獲得注解信息,methodAnnotation.value()獲得注解內容"SUPER_ADMIN", "SYSTEM_ADMIN"。
看完上述內容,你們掌握Java攔截器以及自定義注解的使用是怎么樣的的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。