【发布时间】:2020-05-18 12:56:26
【问题描述】:
我已经使用以下方式配置了 Spring Security:
@Configuration
@Import(SecurityProblemSupport.class)
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private RestAuthenticationProvider authenticationProvider;
@Autowired
private CustomUserDetailService customUserDetailService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
auth.userDetailsService(customUserDetailService);
super.configure(auth);
}
一切都按预期工作。 Rest 身份验证提供程序是一个实现 AuthenticationProvider 的组件:
@Component
public class RestAuthenticationProvider implements AuthenticationProvider {
问题是,如果我在类路径中添加另一个扩展 AbstractUserDetailAuthenticationProvider 的 @Component,则不再使用 RestAuthenticationProvider; 相反,spring 使用一个默认提供程序,使用配置中指定的 UserDetailService。 此 CustomAuthenticationProvider 没有其他链接。它只是在同一个包中。 如果我在配置中使用 CustomProvider 会发生同样的事情,但其余的是用组件注释而不使用其他任何东西。
@Component // <- if I remove this, everything works fine.
public class CustomUserAuthenticationProvider extends
AbstractUserDetailsAuthenticationProvider {
我想根据一些配置在 AuthenticationProvider(S) 之间切换。 任何帮助表示赞赏。
【问题讨论】:
-
为什么要切换?
-
它并不是要在运行时切换;对于不同的安装,我想使用一种或另一种身份验证。
-
我明白了,但你还没回答为什么?
标签: spring spring-boot authentication spring-security dependency-injection