springSecurity源码分析-springSecurityFilterChain

在web.xml文件中配置

问题:为什么DelegatingFilterProxy的filter-name必须是springSecurityFilterChain?

springSecurity源码分析-springSecurityFilterChain_第1张图片

DelegatingFilterProxy并不是真正的Filter,在其initFilterBean方法中会从WebApplicationContext根据delegate来获取到 

protected void initFilterBean() throws ServletException {

	synchronized (this.delegateMonitor) {
		if (this.delegate == null) {
		// 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.
	WebApplicationContext wac = findWebApplicationContext();
			if (wac != null) {
				this.delegate = initDelegate(wac);
			}
		}
	}
}

在上这代码中this.targetBeanName=getFilterName()就是获取名称叫做springSecurityFilterChain

通过在doFilter就去中我们会发现真正干活的其实是delegate这个Filter,而delegate其实就是FilterChainProxy

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
	throws ServletException, IOException {
	
	// Lazily initialize the delegate if necessary.
	Filter delegateToUse = this.delegate;
	if (delegateToUse == null) {
		synchronized (this.delegateMonitor) {
		delegateToUse = this.delegate;
		if (delegateToUse == null) {
			WebApplicationContext wac = findWebApplicationContext();
			if (wac == null) {
				throw new IllegalStateException("No WebApplicationContext found: " +
				"no ContextLoaderListener or DispatcherServlet registered?");
			}
				delegateToUse = initDelegate(wac);
		}
			this.delegate = delegateToUse;
		}
	}
	// Let the delegate perform the actual doFilter operation.
	invokeDelegate(delegateToUse, request, response, filterChain);
	
}

FilterChainProxy是spring在解析配置文件时装配到上下文中,并且beanName为springSecurityFilterChain,

因此在web.xml中需要配置filter-name为springSecurityFilterChain

你可能感兴趣的:(springSecurity源码分析-springSecurityFilterChain)