지금까지 Spring 설정파일이나 Web.xml 파일의 JavaConfig형태로 사용하는것에 대해서 큰 필요성을 느끼지 못해 굳이 사용하고 있지 않았었다.
그런데 최근 Filter를 운영환경에서만 추가로 설정해야 할 필요가 생겨났다.
Filter를 상속받아서 처리해도 되겠지만 생각난김에 web.xml을 Java Code로 변경해 보았다.
[WebXml.java]
package config; import java.util.EnumSet; import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.filter.DelegatingFilterProxy; import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.multipart.support.MultipartFilter; import org.springframework.web.servlet.DispatcherServlet; import pe.kr.ddakker.core.framework.web.GlobalsProperties; import pe.kr.ddakker.core.support.util.StringUtils; /** * /WEB-INF/Web.xml 설정 * @author ddakker 2014. 10. 13. */ public class WebXml implements WebApplicationInitializer { private static Logger log = LoggerFactory.getLogger(WebXml.class); @Override public void onStartup(ServletContext servletContext) throws ServletException { // 스프링 설정 XmlWebApplicationContext rootContext = new XmlWebApplicationContext(); rootContext.setConfigLocations(new String[] { "classpath:config/spring/context-*.xml" }); rootContext.refresh(); rootContext.start(); GlobalsProperties globalsProperties = rootContext.getBean("globalsProperties", GlobalsProperties.class); String serverType = StringUtils.defaultString(globalsProperties.getProperty("server.type"), "local") String domain = GlobalsProperties.REAL.equals(serverType)?"board.ddakker.pe.kr":serverType + "-board.ddakker.pe.kr"; log.debug("========== domain: {} ========== ", domain); log.debug("========== serverType: {} ========== ", serverType); // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS Filter 영역 SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS if (GlobalsProperties.STAGE.equals(serverType) || GlobalsProperties.REAL.equals(serverType)) { // 상황에 따른 필터 추가 } // 인코딩 설정 CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncodingFilter", characterEncodingFilter); characterEncoding.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); // 스프링 RestFull 설정 HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter(); FilterRegistration.Dynamic hiddenHttpMethod = servletContext.addFilter("hiddenHttpMethodFilter", hiddenHttpMethodFilter); hiddenHttpMethod.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); // 파일업로드 설정 MultipartFilter multipartFilter = new MultipartFilter(); FilterRegistration.Dynamic multipart = servletContext.addFilter("multipartFilter", multipartFilter); multipart.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); // String Security 설정 DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy(); FilterRegistration.Dynamic springSecurity = servletContext.addFilter("springSecurityFilterChain", springSecurityFilterChain); springSecurity.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); // 사이트메쉬 설정 SiteMeshFilter siteMeshFilter = new SiteMeshFilter(); FilterRegistration.Dynamic siteMeshEncoding = servletContext.addFilter("siteMeshFilter", siteMeshFilter); siteMeshEncoding.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/*"); // EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE Filter 영역 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS Listener 영역 SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS // String Core 설정 servletContext.addListener(new ContextLoaderListener(rootContext)); // Spring Security 증복로그인 관련 처리 여부(resources/config/spring/context-security.xml session-management->concurrency-control 부분) //servletContext.addListener(new HttpSessionEventPublisher()); // EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE Listener 영역 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS Servlet 영역 SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS // 스프링 MVC 설정 XmlWebApplicationContext xmlWebApplicationContext = new XmlWebApplicationContext(); xmlWebApplicationContext.setConfigLocation("classpath:config/spring/servlet-mvc.xml"); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(xmlWebApplicationContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); /* EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE Servlet 영역 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE */ } }
[web.xml]
... contextConfigLocation classpath:config/spring/context-*.xml monitoring net.bull.javamelody.MonitoringFilter monitoring /* net.bull.javamelody.SessionListener encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter /* org.springframework.security.web.session.HttpSessionEventPublisher httpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter httpMethodFilter /* multipartFilter org.springframework.web.multipart.support.MultipartFilter multipartFilter /* springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* sitemesh com.opensymphony.sitemesh.webapp.SiteMeshFilter sitemesh /* REQUEST FORWARD org.springframework.web.context.ContextLoaderListener action org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:config/spring/servlet-mvc.xml 1 action /