1. 程式人生 > >Springboot添加filter方法

Springboot添加filter方法

nbsp eth lowest DDU ava 通過 filter style cor

在springboot添加filter有兩種方式:

  (1)、通過創建FilterRegistrationBean的方式(建議使用此種方式,統一管理,且通過註解的方式若不是本地調試,如果在filter中需要增加cookie可能會存在寫不進前端情況)

  (2)、通過註解@WebFilter的方式

一、通過創建FilterRegistrationBean的方式創建多個filter以及設置執行順序:

   1、創建兩個實現Filter接口的類TestFilter1 、TestFilter2

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() {} }

2、創建配置類

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

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的方式

   1、在啟動類上增加@ServletComponentScan註解,自動掃描帶有過濾器註解的包

   2、在類上使用@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

    }

}

(轉自:https://www.cnblogs.com/begin2016/p/8947887.html)

Springboot添加filter方法