双因素认证 - TwoFactorAuth

TwoFactorAuth也称为两步认证,以服务器端代码为例说明GoogleAuthenticator。

依赖:

<dependency>
 <groupId>com.warrenstrange</groupId>
 <artifactId>googleauth</artifactId>
 <version>1.4.0</version>
</dependency>

生成密钥:

public String getKey() {
	GoogleAuthenticator gAuth = new GoogleAuthenticator();
	GoogleAuthenticatorKey key = gAuth.createCredentials();
	return key.getKey();
}

说明:将生成的key保存在数据库中,并填写到(app)GoogleAuthenticator或Authy

GoogleAuthenticator gAuth = new GoogleAuthenticator();
boolean isVali= gAuth.authorize(key, code);

说明:code为GoogleAuthenticator或Authy生成的动态码。