1. 程式人生 > >spring boot2.0配置過濾器filter

spring boot2.0配置過濾器filter

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

import javax.servlet.Filter;

@Configuration
public class WebComponentConfig {

@Bean
public FilterRegistrationBean corsFilterRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(CorsFilter());
    registration.addUrlPatterns("/*");
    registration.setName("corsFilter");
    registration.setOrder(1);
    return registration;
}

@Bean
public FilterRegistrationBean apiFilterRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(ApiFilter());
    registration.addUrlPatterns("/ides/api/*");
    registration.setName("apiFilter");
    registration.setOrder(2);
    return registration;
}

@Bean
public Filter CorsFilter() {
    return new CorsFilter();
}

@Bean
public Filter ApiFilter() {
    return new ApiFilter();
}

}

CorsFilter類支援跨域過濾器 過濾路徑/*
ApiFilter類介面認證過濾器 過濾路徑自定義
多個過濾器執行順序通過registration.setOrder(1);registration.setOrder(2); 語句設定