Spring Boot에서 Filter를 적용하는 방법이다.
먼저 Spring MVC에서는 Filter를 적용하기 위해 web.xml 파일에 다음과 같이 등록한다.
■web.xml
<filter> <filter-name>abcFilter</filter-name> <filter-class>xxx.yyyy.ABCFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> |
ABCFilter를 적용하며, 모든 요청 URI에 ABCFilter를 적용하는 예제이다.
Spring Boot에서는 Filter를 적용하기 위해 먼저 Filter를 생성한다.
■ABCFilter
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.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(Ordered.LOWEST_PRECEDENCE -1) @Component public class ABCFilter implements Filter{ private static final Logger LOGGER = LoggerFactory.getLogger(ABCFilter.class);
@Override public void init(FilterConfig filterConfig) throws ServletException { }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; LOGGER.info("Inside ABCFilter: "+ req.getRequestURI()); chain.doFilter(request, response);
}
@Override public void destroy() { } } |
@Order Annotation을 이용하여 Filter적용 순서를 결정한다. 적용할 Filter가 많을 경우 Filter의 적용 순서를 정의할 수 있다.
다음로 FilterRegistrationBean을 이용하여 Filter를 적용한다.
■WebConfig.java
import java.util.ArrayList; import java.util.List; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.Ordered; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import dymn.demo.filter.ABCFilter; import dymn.demo.interceptor.SessionInterceptor; import dymn.demo.util.PropertiesUtil; @Configuration // @EnableWebMvc // public class WebConfig extends WebMvcConfigurerAdapter { // public class WebConfig extends WebMvcConfigurationSupport { public class WebConfig implements WebMvcConfigurer { @Autowired private ABCFilter abcFilter; /** Register Filter **/ /** If you want to register other filer, Add Bean for other fileter **/ @Bean public FilterRegistrationBean<ABCFilter> getFilterRegistrationBean() { FilterRegistrationBean<ABCFilter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(abcFilter); registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 1); registrationBean.addUrlPatterns("/*"); return registrationBean; } |
FilterRegistrationBean의 Generic Type을 ABCFilter로하여 FilterRegistrationBean을 생성한다.
Filter Mapping은 addUrlPatterns를 이용하여 Fillter이 필요한 URI를 정의한다.
'Spring Framrwork' 카테고리의 다른 글
[Spring Boot] Mybatis 적용 (0) | 2018.08.13 |
---|---|
[Spring Boot] Intercepor 적용 (0) | 2018.08.13 |
[Spring Boot] welcome-file-list 적용 (0) | 2018.08.13 |
[Spring Boot] ReloadableResourceBundleMessageSource를 이용한 다국어 처리 (0) | 2018.08.13 |
[Spring Boot] Schedule Task (0) | 2018.08.13 |