SpringBoot 配置过滤器

1.配置过滤器

package com.qingfeng.filter;

public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    	System.out.println("进入了init方法。。。。。。" );
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("进入了doFilter方法。。。。。。" );
    }
    @Override
    public void destroy() {
 System.out.println("进入了destroy方法。。。。。。" );
    }
}

  

package com.qingfeng.config;
importorg.springframework.boot.web.servlet.FilterRegistrationBea;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.qingfeng.filter.UserFilter;

@Component
public class FilterConfig {
	@Bean
    public FilterRegistrationBean userFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new UserFilter());// 设置你的过滤器或监听器对象
        registrationBean.addUrlPatterns("*.do");//设置URL过滤规则,这里使用*.do的请求才能filter过滤,允许访问,没有以.do结尾的都要被拦截,不被访问
        registrationBean.setName("UserFilter");//设置过滤器名字
        registrationBean.addInitParameter("serverName1", " http://localhost:8080" );//给对象属性赋值
        registrationBean.addInitParameter("serverName2", " http://localhost:8082" );//给对象属性赋值
        registrationBean.setOrder(1);//用于设置过滤器或监听器的执行顺序
        return registrationBean;
    }	
}

  

2.这是ssm的xml配置的过滤器

 

 

3.这是Springboot的过滤器与SSM的过滤器关系图

 

 

posted @ 2020-05-15 19:30  Amy清风  阅读(649)  评论(0编辑  收藏  举报