不久前我开发了一个Spring Boot应用程序,并使用本地数据库进行用户身份验证。
现在,由于应用程序的使用越来越多,我还想通过我公司的活动目录启用身份验证。
我仍然想保留本地用户表中的所有用户以从其他表中引用它们,但检查某些用户的活动目录的用户名和密码。用户是活动目录用户还是本地用户保留在用户表中。
在伪代码中,它看起来像这样:
if(user.isAdUser()) {
checkCredentialsAgainstAD();
} else {
checkCredentialsAgainstLocalDb();
}
我当前的验证码如下所示:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDAO userDao;
@Override
protected void configure(HttpSecurity http) throws Exception {
/* http.authorizeRequests()... */
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(new CustomUserDetailsService(userDao)).passwordEncoder(new BCryptPasswordEncoder());
}
}
并通过覆盖的loadUserByUsername(String username)
方法中的user道
从数据库加载用户信息:
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userDao.readUserByLogin(username, true);
if(user == null){
log.info(username + " not found");
throw new UsernameNotFoundException(username + " not found");
}
User userObj = user.clone();
userObj.password = null;
return new CustomUserDetails(user.loginName, user.password, getAuthorities(user), userObj);
}
我已经发现我可以像这样添加一个ActiveDirectoryLdapAuthenticationProvider
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new ActiveDirectoryLdapAuthenticationProvider("DOMAIN", "ldap://example.com")).userDetailsService(new CustomUserDetailsService(userDao)).passwordEncoder(new BCryptPasswordEncoder());
})
但这将始终使用活动目录。
如何具体选择将使用哪个身份验证提供程序?
我不想按照JavaSpring Security配置中建议的顺序测试所有提供程序-多个身份验证提供程序,但只有正确的一个,这取决于我从数据库中获得的标志。
根据您的需求,我认为最简单的解决方案可能是创建您的自定义AuthenticationProvider
,它在内部包装了DaoAuthenticationProvider
和ActiveDirectoryLdapAuthenticationProvider
。
只有这个自定义的AuthenticationProvider
将被注册。在身份验证过程中,您首先从DB获取用户以确定他们的“身份验证模式”,并相应地委托给DaoAuthenticationProvider
或ActiveDirectoryLdapAuthenticationProvider
进行进一步的身份验证。
如下所示,请随时修改:
@Service
public class MyCustomAuthenticationProvider implements AuthenticationProvider{
@Autowired
private DaoAuthenticationProvider dbAuthProvider;
@Autowired
private ActiveDirectoryLdapAuthenticationProvider adAuthProvider;
@Autowired
UserDAO userDao;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException{
// Determine username
String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED": authentication.getName();
User user = userDao.getUserByName(username);
if(user == null){
throw new AuthenticationException("Sorry. No this user.");
}
if(user.isAdUser()) {
return adAuthProvider.authenticate(authentication);
} else {
return dbAuthProvider.authenticate(authentication);
}
}
@Override
public boolean supports(Class<?> authentication){
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
}
但是如何通过注释配置连接它们是另一个需要跟进的故事……