谷歌Google authenticator 整合到JAVA项目

前言:

     最近项目中,需要使用到谷歌的验证码,就采用了这种.....
     其实还可以使用reCaptcha来做,不过移动端还是采用authenticator 会方便点,

    如果想了解reCaptcha,移步到这里:https://blog.csdn.net/baidu_38990811/article/details/86530350

原理:

1.客户端每30秒使用密钥『DPI45HKISEXU6HG7』和时间戳通过一种『算法』生成一个6位数字的一次性密码

2.用户登陆时输入一次性密码『684060』。

3.服务器端使用保存在数据库中的密钥『DPI45HKISEXU6HG7』和时间戳通过同一种『算法』生成一个6位数字的一次性密码。大家都懂控制变量法,如果算法相同、密钥相同,又是同一个时间(时间戳相同),那么客户端和服务器计算出的一次性密码是一样的。服务器验证时如果一样,就登录成功了。

思路:

1.手机下载Google authenticator 验证器

2.在app界面上,无论是扫码还是手动添加都行,我是直接手动添加,
测试的话,账号随便输入,正式的话,我都是采用用户手机号,秘钥是16位,生成方法,在下面,

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是 Google AuthenticatorJava 代码: ```java import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Arrays; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base32; public class GoogleAuthenticator { // These fields are required for generating codes. private static final int SECRET_SIZE = 10; private static final int INTERVAL = 30; // These fields are required for validating codes. private static final int WINDOW_SIZE = 3; private static final Base32 base32 = new Base32(); /** * Generate a new secret key. */ public static String generateSecretKey() { SecureRandom random = new SecureRandom(); byte[] bytes = new byte[SECRET_SIZE]; random.nextBytes(bytes); return base32.encodeToString(bytes); } /** * Generate a code for the given secret key and time. */ public static int generateCode(String secret, long time) throws NoSuchAlgorithmException, InvalidKeyException { byte[] key = base32.decode(secret); byte[] data = new byte[8]; long value = time / INTERVAL; for (int i = 7; i >= 0; i--) { data[i] = (byte) (value & 0xff); value >>= 8; } SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] hash = mac.doFinal(data); int offset = hash[hash.length - 1] & 0xf; int truncatedHash = 0; for (int i = 0; i < 4; i++) { truncatedHash <<= 8; truncatedHash |= (hash[offset + i] & 0xff); } truncatedHash &= 0x7fffffff; truncatedHash %= 1000000; return truncatedHash; } /** * Validate a code for the given secret key and time. */ public static boolean validateCode(String secret, int code, long time) throws NoSuchAlgorithmException, InvalidKeyException { for (int i = -WINDOW_SIZE; i <= WINDOW_SIZE; i++) { long t = time + i * INTERVAL; int c = generateCode(secret, t); if (c == code) { return true; } } return false; } public static void main(String[] args) throws Exception { // Generate a new secret key. String secret = generateSecretKey(); System.out.println("Secret key: " + secret); // Get the current time. long time = System.currentTimeMillis(); // Generate a code for the current time. int code = generateCode(secret, time); System.out.println("Code: " + code); // Validate the code for the current time. boolean valid = validateCode(secret, code, time); System.out.println("Valid: " + valid); } } ``` 这个 Java 类包含了三个方法: - `generateSecretKey()`:生成一个新的密钥。 - `generateCode(secret, time)`:使用给定的密钥和时间生成一个验证码。 - `validateCode(secret, code, time)`:使用给定的密钥、验证码和时间验证代码是否有效。 这三个方法都使用Google Authenticator 协议,可以用于生成和验证 Google Authenticator 代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆_小茗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值