码迷,mamicode.com
首页 > 编程语言 > 详细

springboot使用过滤器Filter

时间:2019-05-29 18:17:00      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:mem   ESS   reg   time   pes   url   etl   his   stat   

先创建过滤器配置类:


## 引入的包部分省略...
@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

标签:mem   ESS   reg   time   pes   url   etl   his   stat   

原文地址:https://www.cnblogs.com/jimmyshan-study/p/10945104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!