詳解Java攔截器以及自定義注解的使用
1,設(shè)置預(yù)處理,設(shè)置不需要攔截的請求
@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.UserTokenInterceptor ,securityInterceptor分別處理不同的請求攔截,執(zhí)行不同的攔截邏輯。
2個處理的類請求上可以有交集,2個處理類都執(zhí)行。
@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的值不為本系統(tǒng)生成的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加密數(shù)據(jù)
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 結(jié)束
return false;
}
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// 響應(yīng)結(jié)束后刪除對象
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);
}
}3.關(guān)于注解的使用
@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()獲得注解內(nèi)容"SUPER_ADMIN", "SYSTEM_ADMIN"。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
idea 2023.1字體設(shè)置及自動調(diào)整大小的圖文教程
這篇文章主要介紹了idea 2023.1字體設(shè)置及自動調(diào)整大小的教程,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板
這篇文章主要介紹了詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
關(guān)于Spring?Cloud的熔斷器監(jiān)控問題
Turbine是一個聚合Hystrix監(jiān)控數(shù)據(jù)的工具,它可將所有相關(guān)/hystrix.stream端點的數(shù)據(jù)聚合到一個組合的/turbine.stream中,從而讓集群的監(jiān)控更加方便,接下來通過本文給大家介紹Spring?Cloud的熔斷器監(jiān)控,感興趣的朋友一起看看吧2022-01-01
Java程序中使用JavaMail發(fā)送帶圖片和附件的郵件
這篇文章主要介紹了Java程序中使用JavaMail發(fā)送帶圖片和附件的郵件,JavaMail是專門用來處理郵件的Java API,需要的朋友可以參考下2015-11-11
Springboot項目實現(xiàn)將類從@ComponentScan中排除
這篇文章主要介紹了Springboot項目實現(xiàn)將類從@ComponentScan中排除,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java結(jié)合keytool如何實現(xiàn)非對稱加密與解密詳解
這篇文章主要給大家介紹了關(guān)于java結(jié)合keytool如何實現(xiàn)非對稱加密與解密的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

