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

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;
    }

}

猜你喜欢

转载自blog.csdn.net/andy_zhang2007/article/details/81317201