springboot中配置過濾器以及可能出現的問題

  在springboot添加過濾器有兩種方式:前端

    一、經過建立FilterRegistrationBean的方式(建議使用此種方式,統一管理,且經過註解的方式若不是本地調試,若是在filter中須要增長cookie可能會存在寫不進前端狀況)java

    二、經過註解@WebFilter的方式web

 

  經過建立FilterRegistrationBean的方式建立多個filter以及設置執行順序:spring

     一、建立兩個實現Filter接口的類TestFilter1 、TestFilter2springboot

package com.aoxun.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TestFilter1 implements Filter {

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

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}

}
package com.aoxun.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TestFilter2 implements Filter {

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

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}

}

    二、建立配置類cookie

      有多個filter就建立多個FilterRegistrationBean ,若需註明filter的執行順序,可經過registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE - 1)配置,值越大,執行順序越靠後ide

package com.aoxun.config.web;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.aoxun.core.filter.TestFilter1;
import com.aoxun.core.filter.TestFilter2;

/**
 * filter配置
 * @author zcj
 *
 */
@Configuration
public class FilterConfig2 {

    @Bean
    public FilterRegistrationBean companyUrlFilterRegister() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入過濾器
        registration.setFilter(new TestFilter1());
        //攔截規則
        registration.addUrlPatterns("/*");
        //過濾器名稱
        registration.setName("testFilter1");
        //過濾器順序
        registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
        return registration;
    }
    
    @Bean
    public FilterRegistrationBean outLinkSecurityFilterRegister() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入過濾器
        registration.setFilter(new TestFilter2());
        //攔截規則
        registration.addUrlPatterns("/*");
        //過濾器名稱
        registration.setName("testFilter2");
        //過濾器順序
        registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE - 1);
        return registration;
    }
    
}

    

    經過註解@WebFilter的方式url

     一、在啓動類上增長@ServletComponentScan註解,自動掃描帶有過濾器註解的包spa

        二、在類上使用@WebFilter和@Order組合實現調試

    

package com.aoxun.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.annotation.Order;

@WebFilter(filterName="testFilter1",urlPatterns= {"/*"})
@Order(FilterRegistrationBean.LOWEST_PRECEDENCE)
public class TestFilter1 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        chain.doFilter(request, response);

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
相關文章
相關標籤/搜索