1. 程式人生 > >Spring Boot新增自定義Filter

Spring Boot新增自定義Filter

第一步:編寫自己的Filter

public class MyFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {  
        //do you works
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
public void destroy() { } }

第二步:在啟動主類引入編寫的Filter

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