In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use SpringBoot interceptor to achieve login interception, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
It can intercept the URL path, and can be used for permission verification, garbled resolution, operation logging, performance monitoring, exception handling, etc.
Implementation code
Create a new interceptor package
Add interceptor code
Package com.qcby.interceptor; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; public class LoginInterceptor implements HandlerInterceptor {@ Autowired private HttpSession httpSession / / before Controller logic execution @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("preHandle...."); String uri = request.getRequestURI (); System.out.println ("current path" + uri) The method of marking @ RequestMapping in / * * HandlerMethod= > Controller * adds this logic when static resources are not intercepted, add this logic = > front and back end separate project * / if (! (handler instanceof HandlerMethod)) {return true } if (httpSession.getAttribute ("username") = = null) {/ / jump to the login interface throw new RuntimeException ("no login!") without login;} else {return true }} / / Controller logic has been executed but before the view parser has parsed @ Override public void postHandle (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println ("postHandle....") } / / Controller logic and view parser completed @ Override public void afterCompletion (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println ("afterCompletion....");}}
Register, configure intercept path and exclude login access path
Package com.qcby.config; import com.qcby.interceptor.LoginInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @ Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (loginInterceptor ()) .addPathPatt erns ("/ * *") / those paths do not intercept .pathPatterns ("/ user/login", "/ error");} @ Bean public LoginInterceptor loginInterceptor () {return new LoginInterceptor ();}}
Implementation class
@ RestController@RequestMapping ("user") public class UserController {@ Autowired private UserService userService; @ Autowired private HttpSession session; @ ApiOperation ("user login interface") @ RequestMapping (value= "login", method = {RequestMethod.GET,RequestMethod.POST}) public Maplogin (User user) {Map map=new HashMap (); map.put ("code", 0) If (StringUtils.isEmpty (user.getUsername ()) | | StringUtils.isEmpty (user.getPassword () {map.put ("msg", "user or password is empty!") ; return map;} QueryWrapper queryWrapper=new QueryWrapper (); queryWrapper.eq ("username", user.getUsername ()) .eq ("password", user.getPassword ()); User user1=userService.getOne (queryWrapper); if (user1 invalid null) {map.put ("cod", 1); map.put ("data", user1) Session.setAttribute ("username", user1.getUsername ());} else {map.put ("msg", "wrong username or password!");} return map;}}
We cannot go to the blocked page when we are not logged in
Log in
After logging in, we can enter the hello method.
The above is all the content of the article "how to use SpringBoot interceptor to achieve login interception". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.