springSecurity源码分析——DelegatingFilterProxy类的作用

转自http://www.cnblogs.com/hzhuxin/archive/2011/12/19/2293730.html


使用过springSecurity的朋友都知道,首先需要在web.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>

从这个配置中,可能会给我们造成一个错觉,以为DelegatingFilterProxy类就是springSecurity的入口,但其实这个类位于spring-web-3.0.5.RELEASE.jar这个jar下面,说明这个类本身是和springSecurity无关。DelegatingFilterProxy类继承于抽象类GenericFilterBean,间接地implement 了javax.servlet.Filter接口,Servlet容器在启动时,首先会调用Filter的init方法,GenericFilterBean的作用主要是可以把Filter的初始化参数自动地set到继承于GenericFilterBean类的Filter中去。在其init方法的如下代码就是做了这个事:

?
1
2
3
4
5
6
PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this .requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess( this );
ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
bw.registerCustomEditor(Resource. class , new ResourceEditor(resourceLoader));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true );

 另外在init方法中调用了initFilterBean()方法,该方法是GenericFilterBean类是特地留给子类扩展用的,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void initFilterBean() throws ServletException {
         // If no target bean name specified, use filter name.
         if ( this .targetBeanName == null ) {
             this .targetBeanName = getFilterName();
         }
 
         // Fetch Spring root application context and initialize the delegate early,
         // if possible. If the root application context will be started after this
         // filter proxy, we'll have to resort to lazy initialization.
         synchronized ( this .delegateMonitor) {
             WebApplicationContext wac = findWebApplicationContext();
             if (wac != null ) {
                 this .delegate = initDelegate(wac);
             }
         }
     }

 可以看出上述代码首先看Filter是否提供了targetBeanName初始化参数,如果没有提供则直接使用filter的name做为beanName,产生了beanName后,由于我们在web.xml的filter的name是springSecurityFilterChain,从spring的IOC容器中取出bean的代码是initDelegate方法,下面是该方法代码:

?
1
2
3
4
5
6
7
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
         Filter delegate = wac.getBean(getTargetBeanName(), Filter. class );
         if (isTargetFilterLifecycle()) {
             delegate.init(getFilterConfig());
         }
         return delegate;
}

 通过跟踪代码,发现取出的bean是org.springframework.security.FilterChainProxy,该类也是继承于GenericFilterBean,取出bean后,判断targetFilterLifecycle属性是false还是true,决定是否调用该类的init方法。这个FilterChainProxy bean实例最终被保存在DelegatingFilterProxy类的delegate属性里,

下面看一下DelegatingFilterProxy类的doFilter方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
             throws ServletException, IOException {
 
         // Lazily initialize the delegate if necessary.
         Filter delegateToUse = null ;
         synchronized ( this .delegateMonitor) {
             if ( this .delegate == null ) {
                 WebApplicationContext wac = findWebApplicationContext();
                 if (wac == null ) {
                     throw new IllegalStateException( "No WebApplicationContext found: no ContextLoaderListener registered?" );
                 }
                 this .delegate = initDelegate(wac);
             }
             delegateToUse = this .delegate;
         }
 
         // Let the delegate perform the actual doFilter operation.
         invokeDelegate(delegateToUse, request, response, filterChain);
     }

 真正要关注invokeDelegate(delegateToUse, request, response, filterChain);这句代码,在下面可以看出DelegatingFilterProxy类实际是用其delegate属性即org.springframework.security.FilterChainProxy实例的doFilter方法来响应请求。

?
1
2
3
4
5
6
protected void invokeDelegate(
             Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
             throws ServletException, IOException {
 
         delegate.doFilter(request, response, filterChain);
     }

 

以上就是DelegatingFilterProxy类的一些内部运行机制,其实主要作用就是一个代理模式的应用,可以把servlet 容器中的filter同spring容器中的bean关联起来。


内容概要:本文介绍了一个基于事件触发机制的孤岛微电网二次电压与频率协同控制仿真模型,该模型通过Simulink实现,旨在提升微电网在孤岛运行模式下的电压和频率稳定性。通过引入事件触发机制,有效降低了控制器的通信负担和计算开销,同时保证了控制性能。模基于事件触发机制的孤岛微电网二次电压与频率协同控制仿真模型(Simulink仿真实现)型涵盖了微电网中分布式电源的动态特性、控制策略的设计与实现,并通过仿真验证了所提方法在抑制电压偏差、频率波动及提升系统响应速度方面的有效性。此外,文档还列举了大量相关的电力系统、优化算法、信号处理、机器学习等领域仿真资源,展示了其在科研与工程应用中的广泛技术支持。; 适合人群:从事电力系统、微电网控制、自动化及相关领域的科研人员、研究生以及具备一定MATLAB/Simulink基础的工程技术人员。; 使用场景及目标:①研究孤岛微电网中电压与频率的协同控制策略;②优化微电网控制系统中的通信效率与实时性;③开展基于事件触发机制的控制算法仿真与验证;④结合其他智能算法(如优化算法、机器学习)进行微电网高级控制策略开发。; 阅读建议:建议读者结合文中提供的仿真模型与相关资源链接,动手实践Simulink建模过程,重点关注事件触发条件的设计与控制效果的对比分析,同时可拓展学习文档中提及的优化算法与智能控制方法,以提升综合科研能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值