日常记录-GoogleAuth 认证

 

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);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值