正在從Spring Security xml配置轉移到Spring Security中的Java Config。Spring Security,彈簧安全覆蓋UserDetailsService
在我的類SecurityConfiguration擴展WebSecurityConfigurerAdapter。但是,問題在於userDetailsService沒有被安全過濾器特別是UsernamePasswordAuthenticationFilter使用。我查看了啓動,看起來這不是在Spring引導創建默認的InMemoryUserDetailsManager之前創建的。
@Configuration
@EnableWebMvcSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http)
throws Exception {
http.userDetailsService(userDetailsService);
}
}
我也曾嘗試使用注射ApplicationUserDetailsService定製覆蓋userDetailsServiceBean和UserDetailsService的這一類。
@Bean(name="myUserDetailsBean")
@Override
public UserDetailsService userDetailsServiceBean() {
return userDetailsService;
}
@Override
public UserDetailsService userDetailsService() {
return userDetailsService;
}
然而,當我嘗試重寫authenticationManagerBean它看起來像它會調用我的配置春天開機初始化配置之前,但它拋出一個錯誤(如下圖),有一個循環引用初始化UsernamePasswordAuthenticationFilter時。我是否真的需要重寫authenticationManagerBean,因爲我需要定義進入UsernamePasswordAuthenticationFilter的內容。
@Bean(name="myAuthenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
..
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter]: Circular reference involving containing bean 'securityBeansConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'usernamePasswordAuthenticationFilter' threw exception; nested exception is java.lang.IllegalArgumentException: successHandler cannot be null
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
... 70 common frames omitted
想法?