要新建Filter的继承类和WebConfiguration将自定义 Filter 加入过滤链

会导致requestBody为null,暂时不适用

方式一

1 自定义过滤器

MyFilter

  1. import javax.servlet.*;
  2. import javax.servlet.http.HttpServletRequest;
  3. import java.io.IOException;
  4. /**
  5. * @Description
  6. * @Author 田云
  7. * @Date 2019/3/7 10:04
  8. * @Version 1.0
  9. */
  10. public class MyFilter implements Filter {
  11. @Override
  12. public void init(FilterConfig filterConfig) throws ServletException {
  13. }
  14. @Override
  15. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  16. HttpServletRequest request = (HttpServletRequest) servletRequest;
  17. System.out.println("拦截的请求:"+request.getRequestURI());
  18. filterChain.doFilter(servletRequest, servletResponse);
  19. }
  20. @Override
  21. public void destroy() {
  22. }
  23. }

2、加入过滤器链

filter/WebConfiguration.java

  1. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. /**
  5. * @Description
  6. * @Author 田云
  7. * @Date 2019/3/7 10:23
  8. * @Version 1.0
  9. */
  10. @Configuration
  11. public class WebConfiguration {
  12. @Bean
  13. public FilterRegistrationBean testFilterRegistration() {
  14. FilterRegistrationBean registration = new FilterRegistrationBean();
  15. registration.setFilter(new MyFilter());
  16. registration.addUrlPatterns("/*");
  17. registration.setName("MyFilter");
  18. registration.setOrder(6);
  19. return registration;
  20. }
  21. // 另一个自定义的过滤,Order表示顺序,小的先执行
  22. @Bean
  23. public FilterRegistrationBean test2FilterRegistration() {
  24. FilterRegistrationBean registration = new FilterRegistrationBean();
  25. registration.setFilter(new MyFilter2());
  26. registration.addUrlPatterns("/*");
  27. registration.setName("MyFilter2");
  28. registration.setOrder(1);
  29. return registration;
  30. }
  31. }

方式二(推荐)

1、添加webfilter注解

  1. import javax.servlet.*;
  2. import javax.servlet.annotation.WebFilter;
  3. import javax.servlet.http.HttpServletRequest;
  4. import java.io.IOException;
  5. /**
  6. * created with IntelliJ IDEA.
  7. * author: fxbin
  8. * date: 2018/9/9
  9. * time: 14:22
  10. * description:
  11. */
  12. @WebFilter(filterName = "myFilter", urlPatterns = "/*")
  13. public class MyFilter implements Filter {
  14. @Override
  15. public void init(FilterConfig filterConfig) throws ServletException {
  16. System.out.println("init...");
  17. }
  18. @Override
  19. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  20. HttpServletRequest request = (HttpServletRequest) servletRequest;
  21. String requestURI = request.getRequestURI();
  22. if(requestURI.contains("my")){
  23. System.out.println("成功啦。。。, 请求URI是:" + requestURI);
  24. }
  25. filterChain.doFilter(request, servletResponse);
  26. }
  27. @Override
  28. public void destroy() {
  29. System.out.println("destroy...");
  30. }
  31. }

2、启动类添加@ServletComponentScan注解


跨域处理

  1. import lombok.extern.slf4j.Slf4j;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import javax.servlet.http.HttpServletResponse;
  5. import java.io.IOException;
  6. @WebFilter
  7. public class CorsFilter implements Filter {
  8. @Override
  9. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  10. HttpServletResponse response = (HttpServletResponse) res;
  11. response.setHeader("Access-Control-Allow-Origin", "*");
  12. response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
  13. response.setHeader("Access-Control-Max-Age", "3600");
  14. response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
  15. chain.doFilter(req, res);
  16. }
  17. public void init(FilterConfig filterConfig) {
  18. // something init
  19. }
  20. public void destroy() {
  21. // destroy something
  22. }
  23. }