TotpAuthenticationProvider.java 文件源码

java
阅读 23 收藏 0 点赞 0 评论 0

项目:springsecuritytotp 作者:
@Override
protected void additionalAuthenticationChecks(UserDetails userDetails,
        UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {

    super.additionalAuthenticationChecks(userDetails, authentication);

    if (authentication.getDetails() instanceof TotpWebAuthenticationDetails) {
        String secret = ((JpaUserDetails) userDetails).getSecret();

        if (StringUtils.hasText(secret)) {
            Integer totpKey = ((TotpWebAuthenticationDetails) authentication
                    .getDetails()).getTotpKey();
            if (totpKey != null) {
                try {
                    if (!TotpAuthenticatorUtil.verifyCode(secret, totpKey, 2)) {
                        throw new BadCredentialsException(
                                "Google Authenticator Code is not valid");
                    }
                }
                catch (InvalidKeyException | NoSuchAlgorithmException e) {
                    throw new InternalAuthenticationServiceException(
                            "Google Authenticator Code verify failed", e);
                }

            }
            else {
                throw new MissingTotpKeyAuthenticatorException(
                        "Google Authenticator Code is mandatory");
            }

        }
    }

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号