【发布时间】:2020-05-12 01:17:54
【问题描述】:
我想将带有 TOTP 软令牌的多因素身份验证添加到 Angular 和 Spring 应用程序,同时使所有内容尽可能接近 Spring Boot Security Starter 的默认值。
令牌验证在本地进行(使用 aerogear-otp-java 库),没有第三方 API 提供程序。
为用户设置令牌是可行的,但通过利用 Spring Security Authentication Manager/Providers 来验证它们却不行。
TL;DR
- 将额外的 AuthenticationProvider 集成到 Spring Boot Security Starter 配置的系统中的官方方法是什么?
- 有哪些推荐的方法来防止重放攻击?
加长版
API 有一个端点/auth/token,前端可以通过提供用户名和密码从该端点获取 JWT 令牌。响应还包括身份验证状态,可以是 AUTHENTICATED 或 PRE_AUTHENTICATED_MFA_REQUIRED。
如果用户需要 MFA,则颁发令牌的单一授权权限为PRE_AUTHENTICATED_MFA_REQUIRED,过期时间为 5 分钟。这允许用户访问端点/auth/mfa-token,在那里他们可以从他们的 Authenticator 应用程序中提供 TOTP 代码并获取完全经过身份验证的令牌以访问该站点。
提供者和令牌
我创建了我的自定义MfaAuthenticationProvider,它实现了AuthenticationProvider:
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// validate the OTP code
}
@Override
public boolean supports(Class<?> authentication) {
return OneTimePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
还有一个 OneTimePasswordAuthenticationToken 扩展 AbstractAuthenticationToken 以保存用户名(取自已签名的 JWT)和 OTP 代码。
配置
我有我的自定义WebSecurityConfigurerAdapter,我通过http.authenticationProvider() 添加我的自定义AuthenticationProvider。根据 JavaDoc,这似乎是正确的地方:
允许添加要使用的附加 AuthenticationProvider
我的SecurityConfig 的相关部分是这样的。
@Configuration
@EnableWebSecurity
@EnableJpaAuditing(auditorAwareRef = "appSecurityAuditorAware")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final TokenProvider tokenProvider;
public SecurityConfig(TokenProvider tokenProvider) {
this.tokenProvider = tokenProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authenticationProvider(new MfaAuthenticationProvider());
http.authorizeRequests()
// Public endpoints, HTML, Assets, Error Pages and Login
.antMatchers("/", "favicon.ico", "/asset/**", "/pages/**", "/api/auth/token").permitAll()
// MFA auth endpoint
.antMatchers("/api/auth/mfa-token").hasAuthority(ROLE_PRE_AUTH_MFA_REQUIRED)
// much more config
控制器
AuthController 注入了 AuthenticationManagerBuilder,并将它们拉到一起。
@RestController
@RequestMapping(AUTH)
public class AuthController {
private final TokenProvider tokenProvider;
private final AuthenticationManagerBuilder authenticationManagerBuilder;
public AuthController(TokenProvider tokenProvider, AuthenticationManagerBuilder authenticationManagerBuilder) {
this.tokenProvider = tokenProvider;
this.authenticationManagerBuilder = authenticationManagerBuilder;
}
@PostMapping("/mfa-token")
public ResponseEntity<Token> mfaToken(@Valid @RequestBody OneTimePassword oneTimePassword) {
var username = SecurityUtils.getCurrentUserLogin().orElse("");
var authenticationToken = new OneTimePasswordAuthenticationToken(username, oneTimePassword.getCode());
var authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
// rest of class
但是,针对/auth/mfa-token 发帖会导致此错误:
"error": "Forbidden",
"message": "Access Denied",
"trace": "org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for de.....OneTimePasswordAuthenticationToken
为什么 Spring Security 没有选择我的 Authentication Provider?调试控制器显示DaoAuthenticationProvider 是AuthenticationProviderManager 中唯一的身份验证提供程序。
如果我将我的MfaAuthenticationProvider 公开为 bean,它是 only 注册的提供程序,所以我得到相反的结果:
No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken.
那么,我如何获得两者?
我的问题
将额外的AuthenticationProvider 集成到配置的Spring Boot Security Starter 系统中的推荐方法是什么,以便我同时获得DaoAuthenticationProvider 和我自己的自定义MfaAuthenticationProvider?我想保留 Spring Boot Scurity Starter 的默认值并另外拥有自己的 Provider。
防止重放攻击
我知道 OTP 算法本身并不能在代码有效的时间片内防止重放攻击; RFC 6238 明确了这一点
在对第一个 OTP 发出成功验证后,验证者不得接受 OTP 的第二次尝试,这确保一次性使用 OTP。
我想知道是否有推荐的方法来实施保护。由于 OTP 令牌是基于时间的,我正在考虑将最后一次成功登录存储在用户模型上,并确保每 30 秒时间片只有一次成功登录。这当然意味着用户模型上的同步。有更好的方法吗?
谢谢。
--
PS:由于这是一个关于安全的问题,我正在寻找来自可靠和/或官方来源的答案。谢谢你。
【问题讨论】:
标签: spring-boot authentication spring-security multi-factor-authentication totp