Spring Boot Web 应用中Spring Security DelegatingFilterProxy 的注入

本文深入探讨了DelegatingFilterProxyRegistrationBean在Servlet3.0+容器中注册代理过滤器的设计与实现。该类允许以Spring友好的方式指定实际的代理过滤器,并在调用时才实例化,提供了灵活的URL模式和servlet关联。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DelegatingFilterProxyRegistrationBean

package org.springframework.boot.web.servlet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;

/**
 * A ServletContextInitializer to register DelegatingFilterProxys in a
 * Servlet 3.0+ container. Similar to the ServletContext#addFilter(String, Filter)
 * registration features provided by ServletContext but with a Spring Bean
 * friendly design.
 * 
 * The bean name of the actual delegate Filter should be specified using the
 * targetBeanName constructor argument. Unlike the FilterRegistrationBean,
 * referenced filters are not instantiated early. In fact, if the delegate filter bean is
 * marked @Lazy it won't be instantiated at all until the filter is called.
 * 
 * Registrations can be associated with #setUrlPatterns URL patterns and/or
 * servlets (either by #setServletNames name or via a
 * #setServletRegistrationBeans ServletRegistrationBeans. When no URL pattern or
 * servlets are specified the filter will be associated to '/*'. The targetBeanName will
 * be used as the filter name if not otherwise specified.
 *
 * @author Phillip Webb
 * @since 1.4.0
 * @see ServletContextInitializer
 * @see ServletContext#addFilter(String, Filter)
 * @see FilterRegistrationBean
 * @see DelegatingFilterProxy
 */
public class DelegatingFilterProxyRegistrationBean extends AbstractFilterRegistrationBean
		implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	private final String targetBeanName;

	/**
	 * Create a new DelegatingFilterProxyRegistrationBean instance to be
	 * registered with the specified ServletRegistrationBeans.
	 * @param targetBeanName name of the target filter bean to look up in the Spring
	 * application context (must not be null).
	 * @param servletRegistrationBeans associate {@link ServletRegistrationBean}s
	 */
	public DelegatingFilterProxyRegistrationBean(String targetBeanName,
			ServletRegistrationBean... servletRegistrationBeans) {
		super(servletRegistrationBeans);
		Assert.hasLength(targetBeanName, "TargetBeanName must not be null or empty");
		this.targetBeanName = targetBeanName;
		setName(targetBeanName);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	protected String getTargetBeanName() {
		return this.targetBeanName;
	}

	@Override
	public Filter getFilter() {
		return new DelegatingFilterProxy(this.targetBeanName,
				getWebApplicationContext()) {

			@Override
			protected void initFilterBean() throws ServletException {
				// Don't initialize filter bean on init()
			}

		};
	}

	private WebApplicationContext getWebApplicationContext() {
		Assert.notNull(this.applicationContext, "ApplicationContext be injected");
		Assert.isInstanceOf(WebApplicationContext.class, this.applicationContext);
		return (WebApplicationContext) this.applicationContext;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值