Filter flow
Filter preprocesses the user's request, then hands the request to the Servlet for preprocessing and generating the response. Finally, filter reprocesses the server's response.
shortcoming
Do not know which controller the current request belongs to
Realization way
Filter is the interface class of servlet, which needs to implement three interfaces.
My environment:
win 7 64bit
IDEA 2018 1.3
Implementation mode 1:
1) Using the annotation of servlet 3.0 for configuration 2) Add @ ServletComponentScan in the startup class to scan 3) Create a new Filter class, implements Filter, and implement the corresponding interface 4) @WebFilter marks a class as filter, which is scanned by spring urlPatterns: interception rules, regular support 6) Control the method call of chain.doFilter to realize whether it can pass the release No release. Scenario: permission control, user login (non front end and back end separation scenario), etc
Code:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //Scan servlet filter @ServletComponentScan public class MydefinationApplication { public static void main(String[] args) { SpringApplication.run(MydefinationApplication.class, args); } }
package com.example.mydefination.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; //Specify the filter rules, you can use regular expressions. filtername specifies the filter name @WebFilter(urlPatterns = "/api/v1/*", filterName = "version1 filter") public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init Filter1"); } /** * The complete process of using Filter is as follows: * Filter Preprocess the user's request, and then submit the request to the Servlet for preprocessing and generating the response, * Finally, Filter processes the response of the server. */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter version1"); HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; //Some other judgments if ("user".equals(req.getParameter("username"))){ filterChain.doFilter(req, resp); }else{ resp.sendRedirect("/index.html"); return; } } @Override public void destroy() { System.out.println("destroy Filter1"); } }
Implementation mode II
Write your own configuration class and register your own filter
Code:
Registration class:
import com.example.mydefination.filter.MyFilterTwo; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration public class FilterConfig { @Bean public FilterRegistrationBean twoFilter(){ MyFilterTwo myFilterTwo = new MyFilterTwo(); FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(myFilterTwo); List<String> urls = new ArrayList<>(); urls.add("/api/v2/*"); filterRegistrationBean.setUrlPatterns(urls); return filterRegistrationBean; } }
Custom filter:
import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class MyFilterTwo implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init Filter2"); } /** * The complete process of using Filter is as follows: * Filter Preprocess the user's request, and then submit the request to the Servlet for preprocessing and generating the response, * Finally, Filter processes the response of the server. */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter version2"); HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; //Some other judgments if ("user".equals(req.getParameter("username"))){ System.out.println("url:" + req.getRequestURL()); filterChain.doFilter(req, resp); }else{ resp.sendRedirect("/index.html"); return; } } @Override public void destroy() { System.out.println("destroy Filter2"); } }
The above two test Controller codes:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { /** * Test URL * http://localhost:8080/api/v1/hello1?username=user * @return */ @GetMapping(value = "api/v1/hello1") public String hello1(){ System.out.println("Get into hello1 11111111111"); return "hello1"; } /** * Test URL * http://localhost:8080/api/v2/hello2?username=user * @return */ @GetMapping(value = "api/v2/hello2") public String hello2(){ System.out.println("Get into hello22222222"); return "hello2"; } }
Conclusion:
One way | Mode two |
Implementation is simple | complex |
Multiple filter order depends on filter name initialization | Initialize according to registration order |
Mode 2 is better than mode 1 in initialization and destruction |