首页 > 解决方案 > 是否有一个名为 springSecurityFilterChain 的 bean?

问题描述

好奇问题:美好的一天,我正在研究 Spring Security,似乎有一个实际上称为“springSecurityFilterChain”的 bean。在网上搜索会No bean named ‘springSecurityFilterChain’ is defined返回一堆关于如何解决问题的结果和解决方案。注意:我没有遇到问题,只是好奇。

现在,我尝试在 Spring MVC 应用程序的 Root 上下文或 WebApplicationContex 上下文中查找该 bean,但没有 springSecurityFilterChain bean。有一个实际的 Spring Security 链,是的,但没有 bean。

因此,任何见解都值得赞赏。更多注释如下。

为了进行测试,我正在使用 PactkPub spring security book 中的示例中的示例应用程序。标准配置:

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

委托过滤器代理,将代理到 springSecurityFilterChain。一切都清楚,但没有名为 springSecurityFilterChain 的 bean。

这是我用来获取 bean 的代码:

    ApplicationContext parent = applicationContext.getParent();
    System.out.println(parent.getDisplayName());
    System.out.println("Beans in parent = " + parent.getBeanDefinitionCount());
    int i = 0;
    for (String beanName : parent.getBeanDefinitionNames()) {
        System.out.println("Bean in parent: " + i + ": " +
                beanName + " : " +
                parent.getBean(beanName).getClass().toString());
        i++;
    }

    System.out.println("Context: " + applicationContext.getDisplayName());
    System.out.println("Beans in context = " + applicationContext.getBeanDefinitionCount());
    i = 0;
    for (String beanName : applicationContext.getBeanDefinitionNames()) {
        System.out.println("Bean in child: " + i + ": " +
                beanName + " : " +
                applicationContext.getBean(beanName).getClass().toString());
        i++;
    }

bean 注册列表是:

Root WebApplicationContext
Beans in parent = 39
Bean in parent: 0: jdbcEventDao : class com.sun.proxy.$Proxy14
Bean in parent: 1: jdbcCalendarUserDao : class com.sun.proxy.$Proxy15
Bean in parent: 2: defaultCalendarService : class com.packtpub.springsecurity.service.DefaultCalendarService
Bean in parent: 3: userContextStub : class com.packtpub.springsecurity.service.UserContextStub
Bean in parent: 4: org.springframework.context.annotation.internalConfigurationAnnotationProcessor : class org.springframework.context.annotation.ConfigurationClassPostProcessor
Bean in parent: 5: org.springframework.context.annotation.internalAutowiredAnnotationProcessor : class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
Bean in parent: 6: org.springframework.context.annotation.internalRequiredAnnotationProcessor : class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
Bean in parent: 7: org.springframework.context.annotation.internalCommonAnnotationProcessor : class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
Bean in parent: 8: org.springframework.aop.config.internalAutoProxyCreator : class org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator
Bean in parent: 9: org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0 : class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource
Bean in parent: 10: org.springframework.transaction.interceptor.TransactionInterceptor#0 : class org.springframework.transaction.interceptor.TransactionInterceptor
Bean in parent: 11: org.springframework.transaction.config.internalTransactionAdvisor : class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor
Bean in parent: 12: dataSource : class org.springframework.jdbc.datasource.SimpleDriverDataSource
Bean in parent: 13: transactionManager : class org.springframework.jdbc.datasource.DataSourceTransactionManager
Bean in parent: 14: jdbcTemplate : class org.springframework.jdbc.core.JdbcTemplate
Bean in parent: 15: messageSource : class org.springframework.context.support.ReloadableResourceBundleMessageSource
Bean in parent: 16: org.springframework.security.filterChains : class java.util.ArrayList
Bean in parent: 17: org.springframework.security.filterChainProxy : class org.springframework.security.web.FilterChainProxy
Bean in parent: 18: org.springframework.security.web.DefaultSecurityFilterChain#0 : class org.springframework.security.web.DefaultSecurityFilterChain
Bean in parent: 19: org.springframework.security.web.PortMapperImpl#0 : class org.springframework.security.web.PortMapperImpl
Bean in parent: 20: org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0 : class org.springframework.security.authentication.ProviderManager
Bean in parent: 21: org.springframework.security.authentication.ProviderManager#0 : class org.springframework.security.authentication.ProviderManager
Bean in parent: 22: org.springframework.security.web.context.HttpSessionSecurityContextRepository#0 : class org.springframework.security.web.context.HttpSessionSecurityContextRepository
Bean in parent: 23: org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy#0 : class org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy
Bean in parent: 24: org.springframework.security.web.savedrequest.HttpSessionRequestCache#0 : class org.springframework.security.web.savedrequest.HttpSessionRequestCache
Bean in parent: 25: org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler#0 : class org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler
Bean in parent: 26: org.springframework.security.access.vote.AffirmativeBased#0 : class org.springframework.security.access.vote.AffirmativeBased
Bean in parent: 27: org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0 : class org.springframework.security.web.access.intercept.FilterSecurityInterceptor
Bean in parent: 28: org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator#0 : class org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator
Bean in parent: 29: org.springframework.security.authentication.AnonymousAuthenticationProvider#0 : class org.springframework.security.authentication.AnonymousAuthenticationProvider
Bean in parent: 30: org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint#0 : class org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint
Bean in parent: 31: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0 : class org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
Bean in parent: 32: org.springframework.security.userDetailsServiceFactory : class org.springframework.security.config.http.UserDetailsServiceFactoryBean
Bean in parent: 33: org.springframework.security.web.DefaultSecurityFilterChain#1 : class org.springframework.security.web.DefaultSecurityFilterChain
Bean in parent: 34: org.springframework.security.provisioning.InMemoryUserDetailsManager#0 : class org.springframework.security.provisioning.InMemoryUserDetailsManager
Bean in parent: 35: org.springframework.security.authentication.dao.DaoAuthenticationProvider#0 : class org.springframework.security.authentication.dao.DaoAuthenticationProvider
Bean in parent: 36: org.springframework.security.authentication.DefaultAuthenticationEventPublisher#0 : class org.springframework.security.authentication.DefaultAuthenticationEventPublisher
Bean in parent: 37: org.springframework.security.authenticationManager : class org.springframework.security.authentication.ProviderManager
Bean in parent: 38: org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0 : class org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
Context: WebApplicationContext for namespace 'Spring MVC Dispatcher Servlet-servlet'
Beans in context = 23
Bean in child: 0: eventsController : class com.packtpub.springsecurity.web.controllers.EventsController
Bean in child: 1: welcomeController : class com.packtpub.springsecurity.web.controllers.WelcomeController
Bean in child: 2: defaultController : class com.packtpub.springsecurity.web.controllers.DefaultController
Bean in child: 3: webMvcConfig : class com.packtpub.springsecurity.web.config.WebMvcConfig$$EnhancerByCGLIB$$2f942b5a
Bean in child: 4: org.springframework.context.annotation.internalConfigurationAnnotationProcessor : class org.springframework.context.annotation.ConfigurationClassPostProcessor
Bean in child: 5: org.springframework.context.annotation.internalAutowiredAnnotationProcessor : class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
Bean in child: 6: org.springframework.context.annotation.internalRequiredAnnotationProcessor : class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
Bean in child: 7: org.springframework.context.annotation.internalCommonAnnotationProcessor : class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
Bean in child: 8: org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0 : class org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
Bean in child: 9: org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#0 : class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$1dfc2733
Bean in child: 10: requestMappingHandlerMapping : class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
Bean in child: 11: viewControllerHandlerMapping : class org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
Bean in child: 12: beanNameHandlerMapping : class org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
Bean in child: 13: resourceHandlerMapping : class org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
Bean in child: 14: defaultServletHandlerMapping : class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
Bean in child: 15: requestMappingHandlerAdapter : class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
Bean in child: 16: mvcConversionService : class org.springframework.format.support.DefaultFormattingConversionService
Bean in child: 17: mvcValidator : class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
Bean in child: 18: httpRequestHandlerAdapter : class org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
Bean in child: 19: simpleControllerHandlerAdapter : class org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
Bean in child: 20: handlerExceptionResolver : class org.springframework.web.servlet.handler.HandlerExceptionResolverComposite
Bean in child: 21: contentNegotiatingViewResolver : class org.springframework.web.servlet.view.ContentNegotiatingViewResolver
Bean in child: 22: internalResolver : class org.springframework.web.servlet.view.InternalResourceViewResolver

标签: springspring-security

解决方案


我想我找到了答案:springSecurityFilterChain不是 bean 的实际名称,而是一个别名。这通过函数AbstractBeanFactory.transformedBeanName转换为实际或规范名称,该函数采用别名springSecurityFilterChain并返回org.springframework.security.filterChainProxy

近似的伪调用链是

DelegatingFilterProxy.initFilterBean()
 DelegatingFilterProxy.initDelegate(webapplicationcontext wac)
  Filter delegate = wac.getBean(getTargetBeanName(), Filter.class)
   ..
   AbtractBeanFactory.doGetBean(name)  // name here is springSecurityFilterChain
    beanName = AbstractBeanFactory.transformedBeanName(name)

函数transformBeanName非常简单,基本上调用canonicalName:

protected String transformedBeanName(String name) {
    return canonicalName(BeanFactoryUtils.transformedBeanName(name));
}

canonicalName,它是 SimpleAliasRegistry 类的一个方法,它在一个名为 aliasMap 的哈希映射中查找,它恰好有一个用于 springSecurityFilterChain 的条目。


推荐阅读