반응형
ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean을 이용해서 등록할 수 있다.
1. 필터 생성 및 등록
LogFilter1
public class LogFilter1 extends GenericFilterBean {
private static final Logger log = LoggerFactory.getLogger(LogFilter1.class);
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("이것은 필터1입니다.");
filterChain.doFilter(servletRequest, servletResponse);
}
}
LogFilter2
public class LogFilter2 extends GenericFilterBean {
private static final Logger log = LoggerFactory.getLogger(LogFilter2.class);
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("이것은 필터2입니다.");
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Configuration
public class FilterConfiguration {
@Bean
public FilterRegistrationBean logFilter1RegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new LogFilter1());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
filterRegistrationBean.setOrder(2);
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean logFilter2RegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new LogFilter2());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
filterRegistrationBean.setOrder(1);
return filterRegistrationBean;
}
}
2. Servlet 생성 및 등록
LogServlet1
public class LogServlet1 extends HttpServletBean {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("LogServlet1");
}
}
LogServlet2
public class LogServlet2 extends HttpServletBean {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("LogServlet2");
}
}
@Configuration
public class ServletConfiguration {
@Bean
public ServletRegistrationBean logServlet1RegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new LogServlet1());
servletRegistrationBean.setUrlMappings(Arrays.asList("/log1"));
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean logServlet2RegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new LogServlet2());
servletRegistrationBean.setUrlMappings(Arrays.asList("/log2"));
return servletRegistrationBean;
}
}
SpringBoot를 띄운 후, /log1 /log2 에 각각 요청을 보내면 Servlet과 Filter가 정상 동작하는 것을 볼 수 있다.
2019-11-05 21:58:35.055 INFO 65987 --- [nio-8080-exec-6] com.nhn.corn.sample.filter.LogFilter2 : 이것은 필터2입니다.
2019-11-05 21:58:35.055 INFO 65987 --- [nio-8080-exec-6] com.nhn.corn.sample.filter.LogFilter1 : 이것은 필터1입니다.
2019-11-05 21:58:39.393 INFO 65987 --- [nio-8080-exec-7] com.nhn.corn.sample.filter.LogFilter2 : 이것은 필터2입니다.
2019-11-05 21:58:39.393 INFO 65987 --- [nio-8080-exec-7] com.nhn.corn.sample.filter.LogFilter1 : 이것은 필터1입니다.
반응형
'프로그래밍 노트 > SPRING BOOT' 카테고리의 다른 글
[Spring Boot] Custom한 배너를 만들어보자! (0) | 2019.11.05 |
---|---|
[Spring Boot] freemarker 설정하기 (0) | 2019.11.05 |
[Spring Boot] 자동 설정 이해하기2 (0) | 2019.11.05 |
[Spring Boot] 스프링부트 테스트하기 (0) | 2019.09.16 |
[Spring Boot] 자동 설정 이해하기 @EnableAutoConfiguration (0) | 2019.07.11 |