您的位置:首页 > 编程语言 > Java开发

Spring DelegatingFilterProxy解析

2014-09-02 18:16 399 查看
以前DelegatingFilterProxy是在需要使用spring security 的时候在xml中配置,如下:

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


所以一直以为此类是spring security中的类,后来发现不是这样的,这个类位于spring-web包,和security没有关系。

这个类实际上就是一个Filter的委派代理类。

这样配置的意思是 spring管理的bean中有一个叫名字叫springSecurityFilterChain,然后把其注册进来。

在security的AbstractSecurityWebApplicationInitializer中是这样使用的:

/**
* Registers the springSecurityFilterChain
* @param servletContext the {@link ServletContext}
*/
private void insertSpringSecurityFilterChain(ServletContext servletContext) {
String filterName = DEFAULT_FILTER_NAME;
DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy(filterName);
String contextAttribute = getWebApplicationContextAttribute();
if(contextAttribute != null) {
springSecurityFilterChain.setContextAttribute(contextAttribute);
}
registerFilter(servletContext, true, filterName, springSecurityFilterChain);
}


这样就是把filter配置成了一个bean,并且让spring来管理器生命周期。

DelegatingFilterProxy本身也是一个filter,>> GenericFilterBean >> Filter

其中有一个参数名是targetBeanName ,这就是需要代理的filter名称,如果这个名称为空的话,则会去找名称是:

<filter-name>springSecurityFilterChain</filter-name>
的bean.
其中有个属性targetFilterLifecycle标示是否调用代理filter的init方法。
如果为true则调用,否则不调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: