Github 开启 2FA-无需下载软件实现

Github 开启 2FA-无需下载软件实现_第1张图片
Github 开启 2FA 验证了,如果不配置,后续会限制登录Github, 用代码实现配置

获取密钥 setup key

Github 开启 2FA-无需下载软件实现_第2张图片
点击 setup key 链接,获取密钥

引入相关算法的 jar

		<dependency>
			<groupId>com.amdelamargroupId>
			<artifactId>jotpartifactId>
			<version>1.3.0version>
		dependency>

生成验证码


import com.amdelamar.jotp.type.Type;
import org.junit.jupiter.api.Test;
import com.amdelamar.jotp.OTP;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class OtpTest {

    @Test
    public void testGenOtp() throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        // Generate a Time-based OTP from the secret, using Unix-time
        // rounded down to the nearest 30 seconds.
        String hexTime = OTP.timeInHex(System.currentTimeMillis(), 30);
        String secret = "xxxxx";
        String code = OTP.create(secret, hexTime, 6, Type.TOTP);
        System.out.println(code);
    }
}

secret 填写第一步获取到的 setup key, 运行结果如下
Github 开启 2FA-无需下载软件实现_第3张图片

你可能感兴趣的:(#,Github,github,2FA)