在这里,我的方案有点类似于Gmail的双因素身份验证.当用户成功登录(SMS代码发送给用户)时,他将被另一页面挑战以输入SMS代码.如果用户正确获取SMS代码,则会显示安全页面(如Gmail收件箱).
我对此做了一些研究,建议不是在登录时给ROLE_USER,给他PRE_AUTH_USER并显示他输入SMS代码的第二页; 成功后给他们ROLE_USER.
但是,我的问题是Spring有InsufficientAuthenticationException,在这种情况下我们不会使用它.在我的场景中是否还有其他更好的方法来实现双因素身份验证?
PS我有一些定制的弹簧安全配置.在我的登录页面,除了用户名和密码,我也有Recaptcha验证,我的authenticationProviderm authenticationSuccessHandler,logoutSuccessHandler,accessDeniedHandler都是自定义的.
SMS代码验证成功后,您可以按如下方式授予ROLE_USER权限.
private void grantAuthority() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(auth.getAuthorities()); authorities.add(new SimpleGrantedAuthority("ROLE_USER")); Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), authorities); SecurityContextHolder.getContext().setAuthentication(newAuth); }
代码来自博客文章和已实施双因素身份验证的示例应用程序.如果我早点发现它会节省很多时间!