是否有一个名为springSecurityFilterChain的bean?

时间:2018-08-20 18:39:54

标签: spring spring-security

好奇心问题:好的,我正在研究Spring Security,似乎有一个实际上称为“ springSecurityFilterChain”的bean。在网络上搜索No bean named ‘springSecurityFilterChain’ is defined会返回大量结果和解决方法。 注意:我没有遇到这个问题,只是好奇。

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

因此,感谢您提供任何见识。下面有更多注释。

为了进行测试,我正在玩PactkPub春季安全性手册中的示例应用程序。标准配置:

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

beans注册列表为:

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

1 个答案:

答案 0 :(得分:0)

我想我找到了答案: 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)

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

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

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