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

How to use interceptors and filters in SSM projects

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use interceptors and filters in the SSM project". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Overview of interceptors

Spring MVC can also use interceptors to intercept requests. Users can customize interceptors to achieve specific functions. Custom interceptors must implement HandlerInterceptor interfaces.

PreHandle (): this method is called before the business processor processes the request, where the user request request is processed. If the programmer decides that the interceptor will call another interceptor after intercepting the request, or the business processor will process it, return true; if the programmer decides that no other components need to be called to process the request, then return false.

PostHandle (): this method is called after the business processor has processed the request, but before the DispatcherServlet returns a response to the client, where the user request request is processed.

AfterCompletion (): this method is called after the DispatcherServlet has fully processed the request, and you can perform some resource cleaning operations in this method.

II. Interceptor configuration steps

1. Create an interceptor (implement HandlerInterceptor interface)

Package com.ssm.Interceptor;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;// interceptor public class LoginInterceptor implements HandlerInterceptor {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {/ / get the requested URL String requestURI=request.getRequestURI (); HttpSession session=request.getSession () Object userInfo=request.getSession () .getAttribute ("USERINFO"); System.out.println ("link:" + requestURI+ "enter the interceptor"); if (userInfo==null) {/ / indicates that you want to enter the system without logging in / / redirect directly to the login interface String serverPath=request.getScheme () + ": / /" + request.getServerName () + "+ request.getServerPort () + request.getContextPath () +" / index.jsp " Response.sendRedirect (serverPath); return false;} else {/ / login succeeded, do not intercept return true;} @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println ("intercept after jump") @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println ("intercept after rendering");}}

I just write a Demo for this interceptor, so I only verify whether or not to log in, and you can also judge some permissions here in the real project, and so on.

2. Configure the interceptor

Since it is configured to intercept all resources, we must release static resources and some requests that do not intercept (login, etc.) with tags. Is the interceptor created in the first step.

3. Test the interceptor:

Try entering an illegal request in the address bar

As you can see from the address bar, the page has been redirected to the login interface and blocked successfully.

But the interceptor cannot block access to jsp pages, so there are two options:

1. Put all jsp pages in the WEB-INF directory

2. Use Servlet filter (although the technology feels a little old)

III. Overview of filters

The Servlet filter is mainly used to filter the request from the client (browser), and then transfer the filtered request to the next resource.

IV. Filter configuration steps

1. Create a filter (implement Filter interface)

Package com.ssm.Filter;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;// Custom filter public class LoginFilter implements Filter {/ / initialization method @ Override public void init (FilterConfig filterConfig) throws ServletException {/ / initialization processing System.out.println (filter initialization) } @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {/ / filter processing / / servletRequest is an interface, HttpServletRequest is implemented, but some methods are unique to HttpServletRequest. For example, the getSession / / HttpServletRequest interface inherits the servletRequest interface and adds the http-related method HttpServletRequest request= (HttpServletRequest) servletRequest; HttpServletResponse response= (HttpServletResponse) servletResponse;// String requestURI=request.getRequestURI () / / System.out.println ("link:" + requestURI+ "enter filter"); HttpSession session=request.getSession (); if (session.getAttribute ("USERINFO") = = null) {/ / illegal request, jump directly to the login interface String serverPath=request.getScheme () +: / / "+ request.getServerName () +": "+ request.getServerPort () + request.getContextPath () +" / index.jsp " Response.sendRedirect (serverPath);} else {/ / normal login, release filterChain.doFilter (request,response);} @ Override public void destroy () {/ / release resource System.out.println ("filter destruction");}}

2. Configure web.xml

LoginFilter com.ssm.Filter.LoginFilter LoginFilter / views/*

The explanation of each tag is written in the comments. As for the resources that need to be filtered, you can add them according to your own needs. I mainly want to filter all the jsp under the views directory.

That's all for "how to use interceptors and filters in the SSM project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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