Spring 春季安全4 2FA
因此,我正在尝试保护我使用SpringMVC和安全性构建的web应用程序。我目前拥有普通自定义登录页面中的基本用户名和密码,该页面使用自定义身份验证提供程序提供已填充的身份验证对象,该对象通过数据库进行验证。我想知道的是如何实现使用TOTP的第二阶段登录?我可以让TOTP发布和验证正常工作,但我不确定如何修改spring安全性,以便通过在我指定的登录页面之外的页面上提交令牌来接受对授权的更改。因此,基本上我最终做的是使用authy api()来执行TOTP交付和验证。初次登录后,我授予他们角色\u PRE\u AUTH,然后将他们发送到受保护的页面以处理TOTP。然后我用Spring 春季安全4 2FA,spring,security,spring-mvc,spring-security,Spring,Security,Spring Mvc,Spring Security,因此,我正在尝试保护我使用SpringMVC和安全性构建的web应用程序。我目前拥有普通自定义登录页面中的基本用户名和密码,该页面使用自定义身份验证提供程序提供已填充的身份验证对象,该对象通过数据库进行验证。我想知道的是如何实现使用TOTP的第二阶段登录?我可以让TOTP发布和验证正常工作,但我不确定如何修改spring安全性,以便通过在我指定的登录页面之外的页面上提交令牌来接受对授权的更改。因此,基本上我最终做的是使用authy api()来执行TOTP交付和验证。初次登录后,我授予他们角色\
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);
Authentication auth=SecurityContextHolder.getContext().getAuthentication();
List authorities=new ArrayList(auth.getAuthorities());
添加(新的SimpleGrantedAuthority(“角色用户”);
Authentication newAuth=新用户名PasswordAuthenticationToken(auth.getPrincipal(),auth.getCredentials(),authorities);
SecurityContextHolder.getContext().setAuthentication(newAuth);
一旦我验证了用户具有有效的TOTP,就可以为用户更新角色。有很多方法可以做到这一点。最简单的方法是:a。只允许匿名访问登录页面;B仅允许认证用户访问OTP代码输入页面;C仅允许具有特定角色的经过身份验证的用户访问所有其他页面。然后,在成功登录时将用户重定向到OTP代码输入页面。成功输入OTP后,将强制角色授予用户。有关此策略的示例实现,请参见。这里是有关此策略的文档链接