Spring mvc 如何为Spring AbstractPreAuthenticationdProcessingFilter连接AuthenticationManager
在Spring安全过滤器链中,我有一个从AbstractPreAuthenticatedProcessingFilter派生的类。此筛选器的目的是将公司身份验证服务留在特殊主体对象中的角色数据传递到集合中,以便SpringSecurity可以使用它们 但是,我无法通过此异常: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
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);
}
}
@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。