2016-03-30 107 views
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 
+0

我不習慣@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管理器將被配置。 –

+0

好了做了一個關於配置方法的簡短測試: 我用'@ Override'替換了'@ Autowired',沒有註釋:所有結果都一樣,我得到了上述錯誤。但是我也注意到,當我在該方法的開始處添加system.out時,該方法未被調用 – bwright

回答

0

好吧,我設法解決這個問題:

1)我用我的CustomUserDetailsS​​ervice這是用彈簧固定的註解內的用戶服務。我創造了只CustomUserDetailsS​​ervice一個單獨的服務,沒有安全檢查(只具有所需的方法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(); 
}