Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何为Spring AbstractPreAuthenticationdProcessingFilter连接AuthenticationManager_Spring Mvc_Spring Security_Autowired - Fatal编程技术网

Spring mvc 如何为Spring AbstractPreAuthenticationdProcessingFilter连接AuthenticationManager

Spring mvc 如何为Spring AbstractPreAuthenticationdProcessingFilter连接AuthenticationManager,spring-mvc,spring-security,autowired,Spring Mvc,Spring Security,Autowired,在Spring安全过滤器链中,我有一个从AbstractPreAuthenticatedProcessingFilter派生的类。此筛选器的目的是将公司身份验证服务留在特殊主体对象中的角色数据传递到集合中,以便SpringSecurity可以使用它们 但是,我无法通过此异常: Caused by: java.lang.IllegalArgumentException: An AuthenticationManager must be set at org.springframew

在Spring安全过滤器链中,我有一个从AbstractPreAuthenticatedProcessingFilter派生的类。此筛选器的目的是将公司身份验证服务留在特殊主体对象中的角色数据传递到集合中,以便SpringSecurity可以使用它们

但是,我无法通过此异常:

Caused by: java.lang.IllegalArgumentException: An AuthenticationManager must be set
        at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
        at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.afterPropertiesSet(AbstractPreAuthenticatedProcessingFilter.java:97) ~[spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
我使用的是Java配置,而不是XML配置。我的代码如下所示:

  • 安全配置器适配器

    @Configuration
    @EnableWebSecurity
    public class MyWebSecurityAdaptor extends WebSecurityConfigurerAdapter {
    
        ...
        @Bean(name = "myAuthenticationManager")
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    }
    
  • 过滤器类本身:

    @Component
    public class MyPreauthFilter extends AbstractPreAuthenticatedProcessingFilter {
    
    
            ...
        @Autowired
        @Override
        public void setAuthenticationManager(AuthenticationManager authenticationManager) {
            super.setAuthenticationManager(authenticationManager);
        }   
    }
    
  • 如果不使用上面第1项中的代码,我将尝试以下操作:

        @Autowired
        @Override
        protected AuthenticationManager authenticationManager() throws Exception   
    {
            // TODO Auto-generated method stub
            return super.authenticationManager();
        }
    
    然后错误就改变了

    然后变成:

    原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[org.springframework.security.authentication.AuthenticationManager]的符合条件的bean:应至少有1个bean符合autowire候选项的条件}


    我想这是有道理的,这种方式并没有定义bean。但是为什么最初定义bean的方法没有失败呢?

    而不是将“MyAutherNotificationManager”添加到WebSecurityConfigureAdapter类中。将其直接添加到过滤器类并自动关联

    @Autowired
    @Override
    public void setMyAuthenticationManager(MyAuthenticationManager myAuthenticationManager) {
        this.myAuthenticationManager  = myAuthenticationManager;
        super.setAuthenticationManager(this.myAuthenticationManager);
    }
    

    从您的Web安全配置适配器中删除与myAuthenticationManager相关的所有代码。

    您必须在代码中定义了管理身份验证活动的authenticationManager,这是必须提供给MyPreauthFilter类的ben。