Github 开启 2FA 验证了,如果不配置,后续会限制登录Github, 用代码实现配置
点击 setup key 链接,获取密钥
<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, 运行结果如下