0
我正在嘗試爲我的Spring MVC應用程序添加方法安全性。Spring Security 4需要驗證管理器
我使用 春:4.2.3 彈簧安全4.0.3
問題是,我得到的錯誤
Caused by: java.lang.IllegalArgumentException: An AuthenticationManager is required
但是當我添加
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
}
我的SecurityConfiguration extends WebSecurityConfigurerAdapter
我收到以下錯誤:
Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.aut[email protected]43744ca2 to already built object
這裏我完全SecurityConfiguration:
@Configuration
@EnableWebSecurity
@ComponentScan
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
OntoRAISUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
formLogin().
and().
logout().
and().
authorizeRequests().
// antMatchers("/api/search/users/all").permitAll().
antMatchers("/login").permitAll().
anyRequest().authenticated().
and().csrf().disable();
}
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder encoder = passwordEncoder();
auth
.userDetailsService(userDetailsService)
.passwordEncoder(encoder);
}
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
}
private BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public OntoRAISUserDetailsService getUserDetailsService() {
return userDetailsService;
}
public void setUserDetailsService(OntoRAISUserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
}
目前我MethodSecurityConfiguration
是空的。
UPDATE: 我當時一看進一步向上的堆棧跟蹤,發現原來除了有一些更多的信息,這可能是有益的。因此,這裏是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: OntoRais.security.OntoRAISUserDetailsService OntoRais.config.SecurityConfiguration.userDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ontoRAISUserDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private OntoRais.datalayer.ontology.service.UserService OntoRais.security.OntoRAISUserDetailsService.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in file [/home/bwright/Repositories/ontology-toolchain/OntoRais/target/OntoRais/WEB-INF/classes/OntoRais/datalayer/ontology/service/UserService.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Unexpected AOP exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodSecurityInterceptor' defined in class path resource [OntoRais/config/MethodSecurityConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: An AuthenticationManager is required
我不習慣@Autowired上configure(...)方法 - 是否有效(即,該方法是否被激活)? [docs](http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html#authenticationManager)指出如果調用configure(...)方法,Auth管理器將被配置。 –
好了做了一個關於配置方法的簡短測試: 我用'@ Override'替換了'@ Autowired',沒有註釋:所有結果都一樣,我得到了上述錯誤。但是我也注意到,當我在該方法的開始處添加system.out時,該方法未被調用 – bwright