반응형

spring boot 사용시 필터 여러개 적용하는 방법입니다.

아래 코드와 같이 FilterRegistrationBean Bean을 여러개 등록하면 됩니다.

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
 
  ...
  @Bean
  public FilterRegistrationBean encodingFilter() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    CustomCharacterEncodingFilter characterEncodingFilter = new CustomCharacterEncodingFilter();
    characterEncodingFilter.setFlag("FilterRegistrationBean");
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    
    registrationBean.setFilter(characterEncodingFilter);
    registrationBean.setOrder(Integer.MIN_VALUE);
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
  }
 
  @Bean
  public FilterRegistrationBean xssFilter() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean(new XSSFilter());
    registrationBean.setOrder(Integer.MIN_VALUE);
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
  }
  ...
}
반응형

+ Recent posts