詳解Java攔截器以及自定義注解的使用
1,設(shè)置預(yù)處理,設(shè)置不需要攔截的請(qǐng)求
@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訪問(wèn)的路徑配置
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分別處理不同的請(qǐng)求攔截,執(zhí)行不同的攔截邏輯。
2個(gè)處理的類(lèi)請(qǐng)求上可以有交集,2個(gè)處理類(lèi)都執(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 {
// 校驗(yàn)handler是否是HandlerMethod
if (!(handler instanceof HandlerMethod)) {
return true;
}
// 從請(qǐng)求頭中獲取token
String token = request.getHeader("Authorization");
/**
* update:2021/11/30 ShengJieLi
* 增加邏輯:Authorization的值不為本系統(tǒng)生成的token時(shí),解密Authorization,獲取token并驗(yàn)證
*/
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對(duì)象放入到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é)束后刪除對(duì)象
UserLocal.remove();
}
}
@SecurityGrade({"SUPER_ADMIN", "SYSTEM_ADMIN"})
public class SecurityController {
private final EmpInfoService empInfoService;
public SecurityController(EmpInfoService empInfoService) {
this.empInfoService = empInfoService;
}
@GetMapping("getUserInformation")
@ApiOperation("登陸用戶(hù)信息")
@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("登陸用戶(hù)信息")
@NoAuthorization
public ResponseResult getUserInformation(@ApiIgnore HttpServletResponse response) {
return empInfoService.getUserInformation(response);
}
}method.getMethodAnnotation(SecurityGrade.class) 獲得注解信息,methodAnnotation.value()獲得注解內(nèi)容"SUPER_ADMIN", "SYSTEM_ADMIN"。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
java線(xiàn)程的中斷和同步問(wèn)題的實(shí)現(xiàn)
本文主要介紹了java線(xiàn)程的中斷和同步問(wèn)題的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
關(guān)于IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群?jiǎn)栴}
這篇文章主要介紹了IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
SpringBoot集成PDFBox實(shí)現(xiàn)電子簽章的代碼詳解
Apache PDFBox 是一個(gè)開(kāi)源的 Java 庫(kù),用于處理 PDF 文檔,它提供了一系列強(qiáng)大的功能,包括創(chuàng)建、渲染、拆分、合并、加密、解密 PDF 文件,以及從 PDF 中提取文本和元數(shù)據(jù)等,本文給大家介紹了SpringBoot集成PDFBox實(shí)現(xiàn)電子簽章,需要的朋友可以參考下2024-09-09
Java中創(chuàng)建線(xiàn)程的兩種方式詳細(xì)說(shuō)明
這篇文章主要介紹了Java中創(chuàng)建線(xiàn)程的兩種方式詳細(xì)說(shuō)明,Java使用java.lang.Thread類(lèi)代表線(xiàn)程,所有的線(xiàn)程對(duì)象都必須是Thread類(lèi)或其子類(lèi)的實(shí)例,每個(gè)線(xiàn)程的作用是完成一定的任務(wù),實(shí)際上就是執(zhí)行一段程序流即一段順序執(zhí)行的代碼,需要的朋友可以參考下2023-11-11
spring cloud 的監(jiān)控turbine-rabbitmq的示例
這篇文章主要介紹了spring cloud 的監(jiān)控turbine-rabbitmq的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
JAVA 根據(jù)設(shè)置的概率生成隨機(jī)數(shù)的方法
本篇文章主要介紹了JAVA 根據(jù)設(shè)置的概率生成隨機(jī)數(shù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

