Java拦截器以及自定义注解的使用是怎么样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1,设置预处理,设置不需要拦截的请求@Componentpublic cla
Java拦截器以及自定义注解的使用是怎么样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
@Componentpublic 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 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拦截器以及自定义注解的使用是怎么样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!
--结束END--
本文标题: Java拦截器以及自定义注解的使用是怎么样的
本文链接: https://lsjlt.com/news/302447.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0