In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Based on how to intercept web login based on SpringMVC, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
1. Brief introduction
SpringMVC's processor interceptor is similar to the filter Filter in Servlet development and is used for pre-processing and post-processing of processors.
The difference between interceptor and filter lies in the concrete application of interceptor to AOP idea.
Filter
Part of the servlet specification that can be used by any java web project
After / * is configured in url-pattern, all resources to be accessed can be intercepted
Need to rewrite method
Interceptor
The SpringMVC framework is its own, and only projects that use the SpringMVC framework can use the
The interceptor will only intercept the accessed controller methods, but will not intercept if the access is jsp/html/css/image/js.
No need to rewrite the method
two。 Custom interceptor
−− > create a new Module and add web support
−− > configure web.xml,applicationContext.xml, add a controller package
−− > write tests
@ RestControllerpublic class TestController {@ GetMapping ("/ T1") public String test () {System.out.println ("TestController-- > test () executed"); return "ok";}}
Add lib in Artifact, and configure Tomcat. Startup test appears, which proves that Spring is configured.
−− > write interceptor
Package com.hxl.config;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyInterceptor implements HandlerInterceptor {/ / execute / / return true before the request processing method Execute the next interceptor / / if false is returned, do not execute the next interceptor public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("- before -"); return true } / / execute public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println ("- after-processing -") after the request processing method is executed;} / / do the cleaning work after dispatcherServlet processing. Public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println ("- clean -");}}
−− > configure interceptor in applicationContext.xml
We do not move in front of us, running, we can see the effect
So then use an example to experience the interceptor (login)
All pages or resources under WEB-INF can only be accessed through controller or servlet
3. Log in to block 3.1 and make a page first.
Index.jsp
$Title$ login home page
Main.jsp
Title Home Page
Login.jsp
Login page username: password:
LoginController
Package com.hxl.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpSession;@Controllerpublic class LoginController {@ RequestMapping ("/ goMain") public String goMain () {return "main";} @ RequestMapping ("/ goLogin") public String goLogin () {return "login" } @ RequestMapping ("/ login") public String login (HttpSession session,String username,String password) {/ / record user identity information to session System.out.println ("receiving front end = =" + username); session.setAttribute ("user", username); return "main";}}
test
Because we can't directly access the page under WEB-INF, we jump in index and request to login.jsp. At this point, if we type it here, we will bring the account password in session.
But at this time is not consistent, because we have not yet logged in to go to the home page, so we need to write an interceptor function
3.2 login intercept
Index.jsp
$Title$ login home page
Login.jsp
Login page username: password:
Main.jsp
Title Home Page
${username}
Write off
LoginInterceptor
At this point, we go to write a login interceptor to determine when it will intercept.
Package com.hxl.config;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginInterceptor implements HandlerInterceptor {public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {HttpSession session = request.getSession () / / release: determine when there is no login / / when the login page releases if (request.getRequestURI (). Contains ("goLogin")) {return true;} if (request.getRequestURI (). Contains ("login")) {return true;} / / the user has logged in, and there is no session when logging in for the first time. If (session.getAttribute ("user")! = null) {return true;} / / determine when request.getRequestDispatcher ("/ WEB-INF/jsp/login.jsp") .forward (request, response); return false;}}
LoginController
Here we have a class url, the following requests need to add / user, you can add one when configuring the interceptor to intercept only those under the user request. And logout function
Package com.hxl.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpSession;@Controller@RequestMapping ("/ user") public class LoginController {@ RequestMapping ("/ goMain") public String goMain () {return "main";} @ RequestMapping ("/ goLogin") public String goLogin () {return "login" } @ RequestMapping ("/ login") public String login (HttpSession session, String username, String password, Model model) {/ / record user identity information to session System.out.println ("receiving front end = =" + username); session.setAttribute ("user", username); model.addAttribute ("username", username); return "main" } @ RequestMapping ("/ goOut") public String goOut (HttpSession session) {/ * / destroy, the following good session.invalidate (); * / / remove session.removeAttribute ("user"); return "main";}}
ApplicationContext.xml
test
At this time, after we start, if we do not log in, we will redirect to the goLogin page, and then log in and jump to the main page, where there is a logout function. Before logging out, you can go to the index page and click on the home page to jump normally. If you log out, session is gone, then you will jump to the login page.
This is the answer to the question about how to intercept web login based on SpringMVC. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.