1. 依赖
<!--googleauth-->
<dependency>
<groupId>com.warrenstrange</groupId>
<artifactId>googleauth</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
2.工具类
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.warrenstrange.googleauth.*;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* GoogleAuth 工具类
*/
public class GoogleAuthUtil {
/**
* 生成GoogleAuth_key
*
* @return coogleAuth_key
*/
public static String createGoogleAuthKey() {
GoogleAuthenticator gAuth = new GoogleAuthenticator();
GoogleAuthenticatorKey credentials = gAuth.createCredentials();
return credentials.getKey();
}
/**
* 令牌验证
*
* @param googleAuthKey GoogleAuth_key
* @param secretCode 验证码
*/
public static void auth(String googleAuthKey, String secretCode) {
GoogleAuthenticator gAuth = new GoogleAuthenticator();
boolean authorize = gAuth.authorize(googleAuthKey, Integer.parseInt(secretCode));
if (!authorize) {
throw new IllegalArgumentException("googleAuth authentication failed!");
}
}
/**
* 生成二维码
*
* @param account 账户信息(展示在Google Authenticator App中的)
* @param secretKey 密钥
* @param title 标题 (展示在Google Authenticator App中的)
**/
public static String createBase64QRCode(String account, String secretKey, String title) {
try {
BitMatrix matrix = new MultiFormatWriter().encode(concatenationScanQRCodeString(account, secretKey, title), BarcodeFormat.QR_CODE, 104, 104);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
ByteArrayOutputStream bof = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", bof);
String base64 = imageToBase64(bof.toByteArray());
return base64;
} catch (WriterException e) {
throw new IllegalArgumentException("create QR code failed!");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("create QR code failed!");
} catch (IOException e) {
throw new IllegalArgumentException("create QR code failed!");
}
}
/**
* 生成绑定二维码(字符串)
*
* @param account 账户信息(展示在Google Authenticator App中的)
* @param secretKey 密钥
* @param title 标题 (展示在Google Authenticator App中的)
* @return 绑定二维码字符串
*/
public static String concatenationScanQRCodeString(String account, String secretKey, String title) {
String url = "otpauth://totp/%s?secret=%s&issuer=%s";
return String.format(url, title + "%3A" + account, secretKey, title);
}
/**
* 将图片文件转换成base64字符串,参数为该图片的路径
*
* @param dataBytes
* @return java.lang.String
*/
private static String imageToBase64(byte[] dataBytes) {
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
if (dataBytes != null) {
return "data:image/jpeg;base64," + encoder.encode(dataBytes);// 返回Base64编码过的字节数组字符串
}
return null;
}
//测试用例
public static void main(String args[]) {
//生成一次密钥即可
//String savedSecret = GoogleAuthUtil.createGoogleAuthKey();
//System.out.println("秘钥:" + savedSecret);
//校验
/* Boolean auth = GoogleAuthUtil.authSecretCode("6KJKN3WZYUVWV55W", 557180);
System.out.println(auth);*/
String base64QRCode = createBase64QRCode("sstest05", "MEC4Z57GW57VU67Q", "sssss");
System.err.println(base64QRCode);
}
}
标签:return,String,GoogleASuth,param,认证,日常,new,import,com From: https://blog.csdn.net/CSDN20230104/article/details/140531780