2016-03-30 24 views
0

Spring MVC uygulamasına yöntem güvenliği eklemeye çalışıyorum.Spring Security 4 Kimlik Denetimi Yöneticisi Gerekli

kullanıyorum Bahar: 4.2.3 İlkbahar-Güvenlik 4.0.3

Sorun i eklediğinizde

Caused by: java.lang.IllegalArgumentException: An AuthenticationManager is required 

Ancak hata alıyorum yani,

@Bean 
@Override 
public AuthenticationManager authenticationManager() throws Exception { 
     return super.authenticationManagerBean(); 
} 

SecurityConfiguration extends WebSecurityConfigurerAdapter için aşağıdaki hatayı alıyorum:

Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.aut[email protected]43744ca2 to already built object 

İşte benim tam 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; 
} 
} 

anda benim MethodSecurityConfiguration boş.

GÜNCELLEME: ben daha stacktrace kadar bir göz vardı ve özgün istisna yararlı olabileceğini biraz daha fazla bilgi olduğunu bulmuşlardır. Yani burada:

1.) Ben bahar ek açıklamaları kullanarak güvenli hale getirildi benim CustomUserDetailsService içinde Kullanıcı Hizmetleri kullandı:

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

Ben @Autowired alışık değilim: Ben şöyle Benim yeni SecurityConfiguration görünüyor güvenlik kontrolleri (sadece sahiptir gerekli yöntem loadUserbyUsername)

2.) olmadan, sadece CustomUserDetailsService için ayrı bir hizmet yarattı configure (...) yöntemi - bu işe yarar mı (yani, bu yöntem aktive ediliyor)? [Docs] (http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html#authenticationManager) şunu belirtir: Yapılandırma (...) yöntemi çağrılırsa, yetkili yönetici yapılandırılacaktır. –

+0

Yapılandırma yöntemiyle ilgili kısa bir test işlemi tamamlandı: Ben @ Autowired işlevi yerine '@ Geçersiz Kıl 'ile değiştirdim ve ek açıklama yok: hepsi de yukarıdaki hatayı alıyorum. Ancak, yöntemin başında bir system.out eklediğimde, yöntemin çağrılmadığını da fark ettim – bwright

cevap

0

Tamam ben bu çözmek başardı.

@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(); 
} 
İlgili konular