springboot初步认识---自定义filter

Posted Daydrea_M_ENG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot初步认识---自定义filter相关的知识,希望对你有一定的参考价值。

Spring Boot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,直接自定义Filter。

步骤:

1.实现Filter接口,实现Filter方法

2.添加@Configuration 注解,将自定义Filter加入过滤链

自定义的filter类:

@Configuration
public class WebConfiguration {
    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }

    public class MyFilter implements Filter {
     @Override
public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) srequest; System.out.println("this is MyFilter,url :" + request.getRequestURI()); filterChain.doFilter(srequest, sresponse); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } }

 


以上是关于springboot初步认识---自定义filter的主要内容,如果未能解决你的问题,请参考以下文章

JAVA-初步认识-常用对象API(集合框架-Map集合-hashmap存储自定义对象)

Spring Boot初步认识

Spring Boot初步认识

JAVA-初步认识-第四章-函数-定义

链表_初步认识

JAVA-初步认识-第六章-二维数组-另一种定义方式