class DelegatingFilterProxy

/**
 * Proxy for a standard Servlet Filter, delegating to a Spring-managed bean that
 * implements the Filter interface. Supports a "targetBeanName" filter init-param
 * in {@code web.xml}, specifying the name of the target bean in the Spring
 * application context.
 *
 * <p>{@code web.xml} will usually contain a {@code DelegatingFilterProxy} definition,
 * with the specified {@code filter-name} corresponding to a bean name in
 * Spring‘s root application context. All calls to the filter proxy will then
 * be delegated to that bean in the Spring context, which is required to implement
 * the standard Servlet Filter interface.
 *
 * <p>This approach is particularly useful for Filter implementation with complex
 * setup needs, allowing to apply the full Spring bean definition machinery to
 * Filter instances. Alternatively, consider standard Filter setup in combination
 * with looking up service beans from the Spring root application context.
 *
 * <p><b>NOTE:</b> The lifecycle methods defined by the Servlet Filter interface
 * will by default <i>not</i> be delegated to the target bean, relying on the
 * Spring application context to manage the lifecycle of that bean. Specifying
 * the "targetFilterLifecycle" filter init-param as "true" will enforce invocation
 * of the {@code Filter.init} and {@code Filter.destroy} lifecycle methods
 * on the target bean, letting the servlet container manage the filter lifecycle.
 *
 */

1. 这是一个过滤器代理 <filter-name>shiroFilter</filter-name> 的值会传递给 targetBeanName ,

targetBeanName = "shiroFilter";

shiroFilter 是由Spring管理的bean,并实现了Filter 接口。

2. 所有的请求会被转发给shiroFilter

3. 当过滤器配置复杂的时候,这个代理非常有用

4. 通过指定 targetFilterLifecycle = "true"; 来强制调用过滤器的初始化方法和销毁方法,从而被servlet 纳入管理

实例:

web.xml 部分代码

    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

applicationContext-Shiro.xml  代码片段

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="webSecurityManager" />
    </bean>
时间: 2024-10-10 05:23:24

class DelegatingFilterProxy的相关文章

【Spring-Security】【2】DelegatingFilterProxy

Spring Security 对我们应用的影响是通过一系列的 ServletRequest 过滤器实现的. Spring Security 使用了 o.s.web.filter.DelegatingFilterProxy 这个 servlet 过滤器来包装所有的应用请求,从而保证它们是安全的. DelegatingFilterProxy 实际上是 Spring 框架提供的,而不是安全特有的.这个过滤器一般在Spring 构建的 web 应用工程中使用,并将依赖于 servlet 过滤器的 Sp

[转]springSecurity源码分析—DelegatingFilterProxy类的作用

使用过springSecurity的朋友都知道,首先需要在web.xml进行以下配置, <filter>  <filter-name>springSecurityFilterChain</filter-name>  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param>         <para

Spring DelegatingFilterProxy解析

以前DelegatingFilterProxy是在需要使用spring security 的时候在xml中配置,如下: <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <f

spring之DelegatingFilterProxy

DelegatingFilterProxy是一个标准servlet Filter的代理,代理实现了Filter接口的spring管理的Bean.支持一个在web.xml的init-param定义的"targetBeanName" filter,在spring applicationContext中指定了target bean的名称. web.xml通常包含一个DelegatingFilterProxy的定义,它在root springContext中指定了filter-name及对应的

SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)

一.SpringSecurity的模块 At the least, you’ll want to include the Core and Configuration modules in your application’s classpath. Spring Security is often used to secure web applications, and that’s certainly the case with the Spittr application, so you’l

配置DelegatingFilterProxy使用Spring管理filter

项目环境:JDK7 + Maven3.04 0. 项目使用springmvc作为controller层 1. 引入spring-security <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.0.1.RELEASE</version> &

DelegatingFilterProxy(委派过滤器代理类)使用

本文转自:http://blog.csdn.net/flyingfalcon/article/details/8543898 DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过spring容器来管理servlet filter的生命周期,还有就是如果filter中需要一些Spring容器的实例,可以通过spring直接注入,另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现. DelegatingFilterProx

spring DelegatingFilterProxy的原理及运用

DelegatingFilterProxy的原理及使用 DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过Spring容器来管理servlet filter的生命周期,还有就是如果filter中需要一些Spring容器的实例,可以通过spring直接注入,另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现. DelegatingFilterProxy的使用方法, 首先在web.xml中配置: <filter><

Spring Security 梳理 - DelegatingFilterProxy

可能你会觉得奇怪,我们在web应用中使用Spring Security时只在web.xml文件中定义了如下这样一个Filter,为什么你会说是一系列的Filter呢? <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class&g