Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of Java interceptors and custom annotations

Shulou Source: shulou.com Published: 2022-06-02 05:42:29 10月03日 Update

What is the use of Java interceptors and custom annotations? I believe many inexperienced people are at a loss about this. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Set preprocessing, set @ 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 for requests that do not need to be intercepted } @ Override public void addInterceptors (InterceptorRegistry registry) {/ / define the path configuration that excludes swagger access String [] swaggerExcludes = new String [] {"/ swagger-ui.html", "/ swagger-resources/**", "/ webjars/**"} Registry .addInterceptor (userTokenInterceptor) .addPathPatterns ("/ * *") .principdePathPatterns ("/ user/login", "/ static/**", "/ * .html", "/ * .ico", "/ * .json", "/ * .png", "/ heartbeat/**") .recipdePathPatterns (swaggerExcludes) Registry .addInterceptor (securityInterceptor) .addPathPatterns ("/ maintain/**", "/ user/**") .recipdePathPatterns ("/ user/login");}} 2.UserTokenInterceptor, securityInterceptor handle different request interceptions and perform different interception logic.

There can be an intersection on two processed class requests, and both processing classes are executed.

@ Componentpublic 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 {/ / verify whether handler is HandlerMethod if (! (handler instanceof HandlerMethod)) {return true;} / / get token String token = request.getHeader ("Authorization") from the request header / * update:2021/11/30 ShengJieLi * add logic: when the value of Authorization is not the token generated by the system, decrypt Authorization, obtain token and verify * / if (StrUtil.isNotEmpty (token)) {EmpInfo securityEmployee = empInfoService.queryToken (token); if (securityEmployee! = null) {/ / token valid String ref = empInfoService.isRef (token) If (StrUtil.isNotBlank (ref)) {response.setHeader ("Access-Control-Expose-Headers", "token"); response.addHeader ("token", ref);} else {/ / Authorization encrypted data for PBE securityEmployee = empInfoService.analyticQueryToken (token,response) } if (securityEmployee! = null) {/ / token is valid / / put User objects into 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 ends return false;} @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {/ / after the response ends, delete the object UserLocal.remove ();} @ SecurityGrade ({"SUPER_ADMIN", "SYSTEM_ADMIN"}) public class SecurityController {private final EmpInfoService empInfoService Public SecurityController (EmpInfoService empInfoService) {this.empInfoService = empInfoService;} @ GetMapping ("getUserInformation") @ ApiOperation ("login user information") @ NoAuthorization public ResponseResult getUserInformation (@ ApiIgnore HttpServletResponse response) {return empInfoService.getUserInformation (response);}} 3. About the use of annotations @ SecurityGrade ({"SUPER_ADMIN", "SYSTEM_ADMIN"}) public class SecurityController {private final EmpInfoService empInfoService; public SecurityController (EmpInfoService empInfoService) {this.empInfoService = empInfoService;} @ GetMapping ("getUserInformation") @ ApiOperation ("login user information") @ NoAuthorization public ResponseResult getUserInformation (@ ApiIgnore HttpServletResponse response) {return empInfoService.getUserInformation (response);}}

Method.getMethodAnnotation (SecurityGrade.class) gets the annotation information, and methodAnnotation.value () gets the annotation content "SUPER_ADMIN" and "SYSTEM_ADMIN".

After reading the above, have you mastered the Java interceptor and how to use custom annotations? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Comments information content interceptors different effective methods more users logic problems processing helpless for this intersection reasons for this objects skills data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi Huawei MariaDB Shulou Technology