In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces java how to use the filter to achieve login interception processing, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Filter to achieve login interception processing 1. What is a filter
A filter is a filter between the resource file of the client and the server (the Web component that resides on the server side). Before accessing the resource file, the filter modifies and judges the request through a series of filters to intercept or modify the request that does not conform to the rules. Responses can also be filtered, intercepted or modified
Second, working principle and life cycle
For example, when we log in to the system, we can access the page, and when we log out, we have to log in again to access it. This is what the filter does. When we visit an interface, the filter intercepts the request, determines whether the current user is logged in, allows access if logged in, and returns the specified page (usually a login page or a customer-friendly prompt page) if not logged in.
This process includes the life cycle of the filter:
1. Instantiation
two。 Initialization
3. Perform filtering operations (including request operations before access and response operations on return)
4. Destroy
III. Filter use
Simply use the filter to intercept login in the springboot project
1. Implement the filter
Public class MyFilter implements Filter {private static final String CURRENT_USER = "current_user"; / / configure whitelist protected static List patterns = new ArrayList (); / / static code block, which loads and executes static {patterns.add ("/ index") only once when the class is loaded by the virtual machine. Patterns.add (Pattern.compile ("/ login")); patterns.add (Pattern.compile ("/ register"));} @ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest httpRequest = (HttpServletRequest) servletRequest HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper (httpResponse); String url = httpRequest.getRequestURI (). Substring (httpRequest.getContextPath (). Length ()); if (isInclude (url)) {/ / url in the whitelist, release access to filterChain.doFilter (httpRequest, httpResponse) Return;} if (SessionUtils.getSessionAttribute (CURRENT_USER)! = null) {/ / release access to filterChain.doFilter (httpRequest, httpResponse) for login status; return } else {/ / otherwise default access to index interface wrapper.sendRedirect ("/ index") } @ Override public void destroy () {} / / determine whether the current request is on the whitelist private boolean isInclude (String url) {for (Pattern pattern: patterns) {Matcher matcher = pattern.matcher (url) If (matcher.matches ()) {return true;}} return false;}}
two。 Registration filter
@ Configurationpublic class WebConfig {/ * configure filter * @ return * / @ Bean public FilterRegistrationBean someFilterRegistration () {FilterRegistrationBean registration = new FilterRegistrationBean (); registration.setFilter (myFilter ()) / / intercept / * access multilevel matching (springboot filter / * and matching / * * multilevel matching) registration.addUrlPatterns ("/ *"); registration.setName ("myFilter"); return registration Create a bean * @ return * / @ Bean (name = "myFilter") public Filter myFilter () {return new MyFilter ();}}
3. Run the project
Visit / index and you will find that it is not intercepted and the correct result will be returned.
In the unlogged-in state, access to the / update interface will be blocked and jump to the / index page
In the login state, access the / update interface, which can be accessed
Here can also be seen in the program debug. The simple filter function is complete.
Commonly used filters and their use to learn later.
Simple implementation of login function interception by filter
The eighth lesson of summer project internship, filter easily realizes the interception of login function.
LoginFliterpublic class LoginFliter implements Filter {@ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; HttpSession session = request.getSession (); User user = (User) session.getAttribute ("user"); String uri = request.getRequestURI () System.out.println (uri.indexOf ("findAll.do")); System.out.println (uri.indexOf ("login.do")); if (user==null & & uri.indexOf ("login.do") =-1) {response.sendRedirect (request.getContextPath () + "/");} else {filterChain.doFilter (request,response) } @ Override public void destroy () {}} controller @ RequestMapping ("/ login.do") public ModelAndView login (User user, HttpSession session) {boolean flag = userService.login (user.getName (), user.getPassword ()); ModelAndView modelAndView = new ModelAndView (); if (flag) {session.setAttribute ("user", user); modelAndView.setViewName (".. / ok") } else {modelAndView.setViewName (".. / failure");} return modelAndView;} result diagram
Thank you for reading this article carefully. I hope the article "java how to use filters to intercept login processing" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
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.