首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要Spring安全4身份验证管理器

需要Spring安全4身份验证管理器
EN

Stack Overflow用户
提问于 2016-03-30 13:09:40
回答 1查看 3.4K关注 0票数 0

我试图将方法安全性添加到Spring应用程序中。

我使用Spring: 4.2.3 Spring-Security4.0.3

问题是,我得到了错误

代码语言:javascript
复制
Caused by: java.lang.IllegalArgumentException: An AuthenticationManager is required

然而,当我添加

代码语言:javascript
复制
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
       return super.authenticationManagerBean();
}

对于我的SecurityConfiguration extends WebSecurityConfigurerAdapter,我得到以下错误:

代码语言:javascript
复制
Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer@43744ca2 to already built object

这里是我的完整SecurityConfiguration:

代码语言:javascript
复制
@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是空的。

更新:我在堆栈跟踪中进一步查看了,发现原始异常包含一些更多的信息,这可能会有所帮助。下面是:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-03-31 13:06:19

好的,我设法解决了这个问题:

1.)我在我的CustomUserDetailsService中使用了一个用户服务,它是使用spring注释进行保护的。我只为CustomUserDetailsService创建了一个单独的服务,没有安全检查(只有必需的方法loadUserbyUsername)。

2.)我的新SecurityConfiguration看起来如下:

代码语言:javascript
复制
 @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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36309850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档