아키텍처와 함께

블로그 이미지
by gregorio
  • Total hit
  • Today hit
  • Yesterday hit

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를 정의한다.








AND

ARTICLE CATEGORY

분류 전체보기 (56)
Spring Framrwork (33)
Linux (1)
APM (1)
Java (8)
python (0)
ant (1)
chart (1)
OS (1)
tomcat (1)
apache (1)
database (0)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

ARCHIVE

LINK