Spring Security Filter Chain Registration Using WebApplicationInitializer for Servlet 3.x
来源:互联网 发布:富士打印机扫描软件 编辑:程序博客网 时间:2024/06/09 14:47
Starting from Servlet 3.x Specification some code saving features have been introduced such as XML less annotation based Servlet and Filter registration. In conjunction with this Spring has introducedWebApplicationInitializer interface which allows the registration of Servlet and Filters using Java instead of using web.xml file.
This is neat feature because this will eliminate the need for the XML files required in the Spring MVC based web applications. Since most of the Spring Application Context configurations are also directing towards Java Config, Moving to would be ideal;
Can effectively be replaced by the following implementation of WebApplicationInitializer
This is neat feature because this will eliminate the need for the XML files required in the Spring MVC based web applications. Since most of the Spring Application Context configurations are also directing towards Java Config, Moving to would be ideal;
I read the documentation of the Spring WebApplicationInitializer and was able to register both DispatcherServlet and DelegatingFilterProxy in using it. So the following web.xml tags;
<context-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.lkbotics.visualizer.config.AppConfig</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>eTagFilter</filter-name> <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>eTagFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>com.lkbotics.visualizer.web.config.MvcConfig</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Can effectively be replaced by the following implementation of WebApplicationInitializer
import javax.servlet.FilterRegistration;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.ContextLoaderListener;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.filter.DelegatingFilterProxy;import org.springframework.web.filter.ShallowEtagHeaderFilter;import org.springframework.web.servlet.DispatcherServlet;import com.lkbotics.visualizer.config.AppConfig;import com.lkbotics.visualizer.web.config.MvcConfig;public class CustomWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); container.addListener(new ContextLoaderListener(rootContext)); AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(MvcConfig.class); FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy()); springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*"); springSecurityFilterChain.setAsyncSupported(true); FilterRegistration.Dynamic shallowETagFilter = container.addFilter("shallowETagFilter", new ShallowEtagHeaderFilter()); shallowETagFilter.addMappingForUrlPatterns(null, true, "/*"); shallowETagFilter.setAsyncSupported(true); ServletRegistration.Dynamic dispatcher = container.addServlet("servletDispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.setAsyncSupported(true); dispatcher.addMapping("/"); }}
0 0
- Spring Security Filter Chain Registration Using WebApplicationInitializer for Servlet 3.x
- WebApplicationInitializer (spring 3.x.x以上版本)
- Spring Security Web Application 之 Security Filter Chain
- spring security 标准Filter及其在filter chain的顺序
- spring security 标准Filter及其在filter chain的顺序
- Spring Web DelegatingFilterProxy和Spring Security Web Filter Chain
- Spring Security 3.x 入门教程
- 9. Spring Security Filter
- Spring Security Filter
- Spring Security Filter详解
- Looking for an example for inserting content into the response using a servlet filter
- WebApplicationInitializer in Spring MVC
- spring security filter源码分析
- spring security (五)Filter
- spring security添加自定义filter
- spring security 自定义filter参考资料
- spring security 3.X 入门例子
- Spring Security 3.x 完整入门教程
- 当时明月在,曾照彩云归
- 百度地图添加右键菜单
- 图片 剪切 水印 失真 处理
- Delphi中Android运行和JNI交互分析
- 【Android安卓教学-零基础到就业】面向就业(3)
- Spring Security Filter Chain Registration Using WebApplicationInitializer for Servlet 3.x
- 异常:java.lang.IllegalStateException
- Relative 布局的基本属性
- Eclipse UML插件 AmaterasUML 的配置及使用
- vs2010, IIS5.0, asp.net 环境搭建相关问题
- leetcode74---杨氏矩阵查找SearchIn2DMatrix
- myeclipse10 tomcat7 struts2
- 经典二十个CSS技巧
- JEECMS二次开发 -------标签使用说明