Github 开启 2FA 验证了,如果不配置,后续会限制登录Github, 用代码实现配置
获取密钥 setup key
点击 setup key 链接,获取密钥
引入相关算法的 jar
<dependency>
<groupId>com.amdelamar</groupId>
<artifactId>jotp</artifactId>
<version>1.3.0</version>
</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, 运行结果如下