【发布时间】:2021-08-24 18:40:32
【问题描述】:
我正在尝试在 Spring 身份验证服务器(Spring Security
根据“认证”方法documentation:
返回:一个完全经过身份验证的对象,包括凭据。可能 如果 AuthenticationProvider 无法支持,则返回 null 传递的 Authentication 对象的身份验证。在这种情况下, 下一个 AuthenticationProvider 支持所呈现的 将尝试身份验证类。
抛出:AuthenticationException - 如果身份验证失败。
基于此,我在主提供者上实现了如下身份验证方法(我将省略 SecondaryAuthProvider 实现):
//PrimaryAuthProvider.class
public Authentication authenticate(Authentication authentication) {
var user = authServices.getLdapUser(authentication.getName());
//log and let the next provider handle it
if (user == null) {
logServices.userNotFound(new LogServices.AuthFailure(authentication.getName()));
return null;
}
if (passwordMatches(authentication.getCredentials(), user.getStringPassword())) {
return authenticatedToken(user);
} else {
logServices.authFailure(new LogServices.AuthFailure(authentication.getName()));
throw new BadCredentialsException("Invalid password");
}
}
在 WebSecurity 中,我还注入了我的提供程序:
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(primaryAuthProvider);
auth.authenticationProvider(secondaryAuthProvider);
}
如果满足以下条件,这将正确处理:
- 用户告知正确的登录名/密码,无论提供商如何。
- 无论密码是否正确,都无法在主要提供商上找到用户。
如果在主要提供者上找到用户并且他的密码错误,则会抛出 BadCredentialsException,但服务器仍将委托给辅助提供者,最终消息将是“找不到用户”,这具有误导性。
我认为 BadCredentialsException 会完成身份验证链并报告给客户端/用户,但事实并非如此。
我错过了什么吗?
【问题讨论】:
标签: spring spring-boot authentication spring-security