Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Java interceptors and custom annotations

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report