我正在尝试创建一个委托(delegate)身份验证提供程序来执行逻辑,然后再根据一些任意逻辑决定选择哪个身份验证提供程序;为了这个例子,如果用户名以前缀开头。
我当前的 SecurityConfig 将一次尝试一个身份验证提供程序:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final MyCustomCredentialAuthProvider myAuthProvider;
...
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth
.ldapAuthentication().configuration(...).here(...).etc(...).and() // ldapAuthenticationProvider is created here
.authenticationProvider(myAuthProvider).and()
// more authentication providers to be added in the future
}
}
根据用户名,我想选择是否要尝试使用提供程序,因此如果用户名不以特定前缀(“ldap”、“custom”、 “广告”、“等等”……),所以:
@Component
public class DelegatingProvider implements AuthenticationProvider {
// Problem: How do I create this ldapAuthenticationProvider bean?
private final LdapAuthenticationProvider ldapAuthenticationProvider;
private final MyCustomCredentialAuthProvider myAuthProvider;
...
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
if (authentication.getName() == null) {
throw new BadCredentialsException("No username provided");
} else if (authentication.getName().startsWith("ldapPlease") }
return ldapAuthProvider.authenticate(authentication);
// } else if (...) { ...
// } else if (...) { ...
} else {
return myAuthProvider.authenticate(authentication);
}
}
@Override
public boolean supports(final Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);;
}
}
我似乎无法以这种方式连接 LdapProvider,因为它是由 SecurityConfig 创建的 - 当 LdapProvider bean 之前由 SecurityConfig 中的 AuthBuilder 处理时,我如何创建和连接 LdapProvider bean?
最佳答案
@Bean
public LdapAuthenticationProvider ldapAuthentication() {
return new LdapAuthenticationProviderConfigurer().configure(...).here(...).etc(...).build();
}
.....................................
@Component
public class DelegatingProvider implements AuthenticationProvider {
@Autowired
private LdapAuthenticationProvider ldapAuthenticationProvider;
@Autowired
private final MyCustomCredentialAuthProvider myAuthProvider;
...
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
if (authentication.getName() == null) {
throw new BadCredentialsException("No username provided");
} else if (authentication.getName().startsWith("ldapPlease") }
return ldapAuthProvider.authenticate(authentication);
// } else if (...) { ...
// } else if (...) { ...
} else {
return myAuthProvider.authenticate(authentication);
}
}
@Override
public boolean supports(final Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);;
}
}
正如@NatFar 指定的那样
@Autowired
private DelegatingProvider delegatingProviderBean;
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth
.authenticationProvider(delegatingProviderBean).and()
// more authentication providers to be added in the future
}
关于java - 创建委托(delegate)身份验证提供程序 (Spring Security),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828640/