springboot使用过滤器Filter

Posted jimmyshan-study

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot使用过滤器Filter相关的知识,希望对你有一定的参考价值。

先创建过滤器配置类:


## 引入的包部分省略...
@Configuration
public class FilterConfig {
      @SuppressWarnings({"rawtypes", "unchecked"})
      @Bean
      public FilterRegistrationBean registFilter() {
           FilterRegistrationBean registration = new FilterRegistrationBean();
           registration.setFilter(this.SzlDemoFiler());
           registration.addUrlPatterns("/demo/*");
           registration.setName("JimmyShan");
           registration.setOrder(1);
           return registration;
      }

      @Bean
      public Filter SzlDemoFilter() {
           return new SzlDemoFilter();
      }

}

 

然后,我们自定义类实现Filter接口:

## 引入的包部分省略...

public
class SzlDemoFilter implements Filter { private final static Logger log = LoggerFactory.getLogger(SzlDemoFilter.class); //@Autowired //private DemoService demoService; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { long start = System.currentTimeMillis(); HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; String methodName = req.getMethod(); log.info("Request method is : " + methodName); String uri = req.getRequestURI(); log.info("Request uri is : " + uri); chain.doFilter(request, response); log.info("Execute filter = " + (System.currentTimeMillis() - start) + " ms"); } }

 

以上是关于springboot使用过滤器Filter的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot——SpringBoot中使用过滤器Filter的两种方式

springboot使用过滤器Filter

在springboot工程中使用filter

使用springboot怎么添加一个filter过滤器

SpringBoot SpringBoot过滤器Filter

SpringBoot使用Filter过滤器处理是否登录的过滤时,用response.sendRedirect()转发报错