In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand Java SpringBoot interceptor". In daily operation, I believe many people have doubts about how to understand Java SpringBoot interceptor. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Java SpringBoot interceptor"! Next, please follow the editor to study!
Catalogue
1. Interceptor
two。 Interceptor use
2.1 preparation work
2.2 Controller
2.3 Interceptor
2.4 Config
2.5 Test
2.5.1 normal login
2.5.2 abnormal login
2.5.3 interceptor
1. Interceptor
In Spring Boot, you can perform operations in the following situations:
Before sending the request to the controller, before sending the response to the client
two。 Interceptor use
Let's implement a simple login authentication function.
2.1 preparation work
Step1: add thymeleaf support to front-end pages
Step2: the main HTML content is as follows:
Log in
Note: URL in th:action= "@ {/ SignUp}" is defined in Controller.
Add a view controller to Step3:Config to implement the addViewControllers method in WebMvcConfigurer interface
@ Overridepublic void addViewControllers (ViewControllerRegistry registry) {registry.addViewController ("/") .setViewName ("/ SignUp"); registry.addViewController ("/ SignUp.html") .setViewName ("/ SignUp"); registry.addViewController ("/ SuccessPage.html") .setViewName ("/ SuccessPage");} 2.2 Controller
The most important thing to realize the login function is Controller. In Controller, for the sake of simplicity and convenience, we do not go to the database and directly use the equal method to compare parameters.
Controllerpublic class SignUpController {@ RequestMapping ("/ SignUp") public String SignUp (@ RequestParam ("email") String email, @ RequestParam ("password") String password, Model model, HttpSession session) {if (email.equals ("123@qq.com") & & password.equals ("123456")) {session.setAttribute ("email") Email) Return "SuccessPage";} else {model.addAttribute ("msg", "incorrect mailbox or password"); return "SignUp";}
Insert the output message that the id is msg on the front page.
2.3 Interceptor
The filter is implemented as follows:
Public class SignUpInterceptor implements HandlerInterceptor {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {if (request.getSession (). GetAttribute ("email") = null) {request.setAttribute ("msg", "you do not have permission to enter, please log in"); request.getRequestDispatcher ("SignUp.html") .forward (request,response); return false;} else {return true } 2.4 Config
The Config class implements the WebMvcConfigurer interface, and you need to implement the addInterceptors method to add an interceptor.
AddPathPatterns: refers to where and where to add interceptors. / * * indicates all files and subdirectories under this path.
ExcludePathPatterns: indicates the url that needs to exclude the interceptor
@ Configurationpublic class Boot_Config implements WebMvcConfigurer {@ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (new SignUpInterceptor ()) .addPathPatterns ("/ *") ExcludePathPatterns ("/", "/ SignUp", "/ SignUp.html");} 2.5 Test 2.5.1 normal login
2.5.2 abnormal login
2.5.3 interceptor
At this point, the study on "how to understand the Java SpringBoot interceptor" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.