JavaConfig没有名为'的bean;springSecurityFilterChain';定义

JavaConfig没有名为'的bean;springSecurityFilterChain';定义,java,spring,spring-security,web.xml,spring-java-config,Java,Spring,Spring Security,Web.xml,Spring Java Config,我按照教程使用Java配置而不是XML创建了一个安全过滤器 未创建bean,应用程序无法加载: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanD

我按照教程使用Java配置而不是XML创建了一个安全过滤器

未创建bean,应用程序无法加载:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:323)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:234)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:332)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:90)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3783)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4409)
    at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:313)
    at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:145)
    at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:461)
    at org.jboss.web.deployers.WebModule.startModule(WebModule.java:122)
    at org.jboss.web.deployers.WebModule.start(WebModule.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
    at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
这是
SecurityConfig
类:

@配置
@EnableWebMvc
公共类SecurityConfig扩展WebMVCConfigureAdapter实现ResourceLoaderWare{
私有资源加载器;
@豆子
public FilterChainProxy springSecurityFilterChain()引发异常{
AuthenticationManager am=AuthenticationManager();
SecurityContextPersistenceFilter sif=getSecurityContextPersistenceFilter();
J2eePreAuthenticatedProcessingFilter j2eePreAuthFilter=getJ2eePreAuthenticatedProcessingFilter(am);
LogoutFilter LogoutFilter=getLogoutFilter();
ExceptionTranslationFilter etf=getExceptionTranslationFilter();
FilterSecurityInterceptor fsi=getFilterSecurityInterceptor(am);
FilterChainProxy fcp=新的FilterChainProxy(新的DefaultSecurityFilterChain(
新的AntPathRequestMatcher(“/**”),
sif、j2eePreAuthFilter、logoutFilter、etf、fsi
));
返回fcp;
}
专用筛选器安全侦听器GetFilterSecurity侦听器(AuthenticationManager am){
AccessDecisionVoter roleVoter=新roleVoter();
列表决策投票者=新建LinkedList();
决策投票人。添加(roleVoter);
基于确认的httpRequestAccessDecisionManager=新的基于确认的(DecisionVorters);
httpRequestAccessDecisionManager.setAllowIfallDecisions(false);
FilterSecurityInterceptor FilterSecurityInterceptor=新的FilterSecurityInterceptor();
filterSecurityInterceptor.setAuthenticationManager(am);
filterSecurityInterceptor.setAccessDecisionManager(httpRequestAccessDecisionManager);
LinkedHashMap requestMap=新建LinkedHashMap();
List configs=new ArrayList();
configs.add(新的org.springframework.security.access.SecurityConfig(“hasRole(ADMIN)”);
put(新的AntPathRequestMatcher(“/**”),configs);
FilterInvocationSecurityMetadataSource FilterInvocationSecurityMetadataSource=新表达式BasedFilterInvocationSecurityMetaDataSource(
requestMap,新的DefaultWebSecurityExpressionHandler());
过滤器安全接收器
.setSecurityMetadataSource(FilterInocationSecurityMetadataSource);
返回过滤器安全接收器;
}
私有LogoutFilter getLogoutFilter(){
org.springframework.security.web.authentication.logout.LogoutFilter LogoutFilter=new LogoutFilter(“/”,new SecurityContextLogoutHandler());
返回注销过滤器;
}
私有ExceptionTranslationFilter getExceptionTranslationFilter(){
ExceptionTranslationFilter ExceptionTranslationFilter=新的ExceptionTranslationFilter(
新的Http403ForbiddenEntryPoint());
返回exceptionTranslationFilter;
}
私有SecurityContextPersistenceFilter getSecurityContextPersistenceFilter(){
返回新的org.springframework.security.web.context.SecurityContextPersistenceFilter();
}
私有J2eePreAuthenticatedProcessingFilter getJ2eePreAuthenticatedProcessingFilter(AuthenticationManager am)引发异常{
WebXmlMappableAttributesRetriever mappableRolesRetriever=新的WebXmlMappableAttributesRetriever();
mappableRolesRetriever.setResourceLoader(this.resourceLoader);
mappableRolesRetriever.afterPropertiesSet();
SimpleAttributes2GrantedAuthoritiesMapper用户角色2GrantedAuthoritiesMapper=新SimpleAttributes2GrantedAuthoritiesMapper();
userRoles2GrantedAuthoritiesMapper.setConvertAttributeToUpperCase(true);
J2EEBasedPreauthenticatedWebAuthenticationDetails来源J2EEBasedPreauthenticatedWebAuthenticationDetails来源
=新的J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource();
j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.setMappableRolesRetriever(mappableRolesRetriever);
j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.setUserRoles2GrantedAuthoritiesMapper(userRoles2GrantedAuthoritiesMapper);
J2eePreAuthenticatedProcessingFilter J2eePreAuthenticatedProcessingFilter=新的J2eePreAuthenticatedProcessingFilter();
j2eePreAuthenticatedProcessingFilter.setAuthenticationManager(am);
j2eePreAuthenticatedProcessingFilter.setAuthenticationDetailsSource(j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource);
返回j2eePreAuthenticatedProcessingFilter;
}
@豆子
公共AuthenticationManager AuthenticationManager()引发异常{
PreAuthenticatedGrantedAuthoritiesUserDetailsService PreAuthenticatedGrantedAuthoritiesUserDetailsService=新的PreAuthenticatedGrantedAuthoritiesUserDetailsService();;
PreAuthenticateAuthenticationProvider PreAuthenticateAuthenticationProvider=新的PreAuthenticateAuthenticationProvider();
预验证数据验证提供程序。setPreAuthenticatedUserDetailsService(预验证GrandedAuthoritiesUserDetailsService);
List lProviders=new LinkedList();
lProviders.add(预验证身份验证提供程序);
AuthenticationManager am=新的ProviderManager(lProviders);
返回am;
}
@凌驾
public void setResourceLoader(ResourceLoader arg0){
this.resourceLoader=arg0;
}       
}

有什么帮助吗?

与示例不同,在这种情况下简化了,如果过滤器类的包设置为组件扫描,则会创建过滤器类

web.xml的以下部分不需要:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
  </context-param>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.config.SecurityConfig</param-value>
  </context-param>

上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
com.config.SecurityConfig
xml定义了应用程序上下文,该上下文将
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/application-context-${environment}.xml</param-value>
</context-param>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security.xsd>
    <context:component-scan base-package="com.config" />
</beans>