详解Java * 以及自定义注解的使用
作者:李三岁yep 时间:2023-05-14 01:40:16
1,设置预处理,设置不需要拦截的请求
@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分别处理不同的请求拦截,执行不同的拦截逻辑。
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);
}
}
3.关于注解的使用
@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"。
来源:https://blog.csdn.net/lisengjiej/article/details/121973949
标签:Java,自定义,自定义,注解
0
投稿
猜你喜欢
实例讲解Java并发编程之闭锁
2023-10-25 14:25:07
Android实现缓存大图到SD卡
2022-07-13 07:13:50
Spring中实现定时调度的几种方法
2021-08-29 13:04:44
AndroidStudio:手势识别
2022-04-04 00:53:58
基于Scala和Java方法的相互调用
2021-07-05 11:53:04
C语言实现航空订票系统课程设计
2023-11-15 10:50:20
C# [ImportDll()] 知识小结
2022-09-15 07:42:18
c#开发cad预览图块步骤详解
2022-12-30 10:49:07
java基本教程之synchronized关键字 java多线程教程
2023-02-20 04:11:17
java使用poi生成excel的步骤
2021-08-25 07:03:45
Java设计模式之工厂模式案例详解
2023-11-27 20:08:03
Java面向对象编程的三大特征
2023-09-19 06:20:34
Java多线程(单例模式,堵塞队列,定时器)详解
2022-09-18 16:22:20
android中实现OkHttp下载文件并带进度条
2022-04-11 17:20:20
spring boot整合RabbitMQ实例详解(Fanout模式)
2022-08-18 18:52:30
springboot注册拦截器所遇到的问题
2023-01-17 21:18:30
AndroidStudio Gradle基于友盟的多渠道打包方法
2022-02-24 22:04:43
ThreadPoolExecutor中的submit()方法详细讲解
2022-02-18 03:02:39
Java的类型擦除式泛型详解
2022-01-02 11:59:00
关于国际化、OGNL表达式语言
2023-09-04 15:20:45