我试图将方法安全性添加到Spring应用程序中。
我使用Spring: 4.2.3 Spring-Security4.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.authentication.configurers.userdetails.DaoAuthenticationConfigurer@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是空的。
更新:我在堆栈跟踪中进一步查看了,发现原始异常包含一些更多的信息,这可能会有所帮助。下面是:
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发布于 2016-03-31 13:06:19
好的,我设法解决了这个问题:
1.)我在我的CustomUserDetailsService中使用了一个用户服务,它是使用spring注释进行保护的。我只为CustomUserDetailsService创建了一个单独的服务,没有安全检查(只有必需的方法loadUserbyUsername)。
2.)我的新SecurityConfiguration看起来如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
formLogin().
and().
logout().
and().
authorizeRequests().
antMatchers("/login").permitAll().
anyRequest().authenticated().
and().csrf().disable();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(ontoRAISUserDetailsService);
auth.authenticationProvider(authenticationProvider());
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(ontoRAISUserDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}https://stackoverflow.com/questions/36309850
复制相似问题