日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Google Authenticator:将其与您自己的Java身份验证服务器配合使用

發(fā)布時(shí)間:2023/12/3 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Google Authenticator:将其与您自己的Java身份验证服务器配合使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用于移動(dòng)設(shè)備的Google Authenticator應(yīng)用程序是一個(gè)非常方便的應(yīng)用程序,它實(shí)現(xiàn)了TOTP算法(在RFC 6238中指定)。 使用Google Authenticator,您可以生成時(shí)間密碼,該密碼可用于在共享請(qǐng)求用戶密鑰的身份驗(yàn)證服務(wù)器中授權(quán)用戶。

Google Authenticator主要用于通過兩因素身份驗(yàn)證訪問Google服務(wù)。 但是,您可以利用Google Authenticator生成基于時(shí)間的密碼,以供您的服務(wù)器進(jìn)行認(rèn)證。 這樣的服務(wù)器的實(shí)現(xiàn)在Java中非常簡(jiǎn)單,您可以從Google Authenticator PAM模塊的源代碼中獲得啟發(fā)。 在這篇博客文章中,我們將在Java類中簡(jiǎn)單介紹一下TOTP算法。

生成密鑰

  • 為了生成密鑰,我們將使用隨機(jī)數(shù)生成器填充所需大小的字節(jié)數(shù)組。 在這種情況下,我們想要:
  • 16個(gè)字符的Base32編碼密鑰:由于x字節(jié)的Base32編碼生成8x / 5個(gè)字符,因此我們將10個(gè)字節(jié)用作密鑰。

一些臨時(shí)代碼(使用Google的行話)。

// Allocating the buffer byte[] buffer =new byte[secretSize + numOfScratchCodes * scratchCodeSie];// Filling the buffer with random numbers. // Notice: you want to reuse the same random generator // while generating larger random number sequences. new Random().nextBytes(buffer);

現(xiàn)在我們要提取對(duì)應(yīng)于密鑰的字節(jié),并使用Base32編碼對(duì)其進(jìn)行編碼。 我正在使用Apache Common Codec庫(kù)來(lái)獲取編解碼器實(shí)現(xiàn):

// Getting the key and converting it to Base32 Base32 codec = new Base32(); byte[] secretKey = Arrays.copyOf(buffer, secretSize); byte[] bEncodedKey = codec.encode(secretKey); String encodedKey = new String(bEncodedKey);

將密鑰加載到Google Authenticator中

您可以手動(dòng)將密鑰加載到Google Authenticator中,或生成QR條碼以使應(yīng)用程序從中加載密鑰。 如果要使用Google服務(wù)生成QR條碼,則可以使用以下代碼生成相應(yīng)的URL:

public static String getQRBarcodeURL(String user,String host,String secret) {String format = "https://www.google.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth://totp/%s@%s%%3Fsecret%%3D%s";return String.format(format, user, host, secret); }

驗(yàn)證碼

現(xiàn)在,我們已經(jīng)生成了密鑰,并且我們的用戶可以將其加載到他們的Google Authenticator應(yīng)用程序中,我們需要驗(yàn)證生成的驗(yàn)證碼所需的代碼。 這是RFC 6238中指定的算法的Java實(shí)現(xiàn):

private static boolean check_code(String secret,long code,long t)throws NoSuchAlgorithmException,InvalidKeyException {Base32 codec = new Base32();byte[] decodedKey = codec.decode(secret);// Window is used to check codes generated in the near past.// You can use this value to tune how far you're willing to go. int window = 3;for (int i = -window; i <= window; ++i) {long hash = verify_code(decodedKey, t + i);if (hash == code) {return true;}}// The validation code is invalid.return false; }private static int verify_code(byte[] key,long t)throws NoSuchAlgorithmException,InvalidKeyException {byte[] data = new byte[8];long value = t;for (int i = 8; i-- > 0; value >>>= 8) {data[i] = (byte) value;}SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");Mac mac = Mac.getInstance("HmacSHA1");mac.init(signKey);byte[] hash = mac.doFinal(data);int offset = hash[20 - 1] & 0xF;// We're using a long because Java hasn't got unsigned int.long truncatedHash = 0;for (int i = 0; i < 4; ++i) {truncatedHash <<= 8;// We are dealing with signed bytes:// we just keep the first byte.truncatedHash |= (hash[offset + i] & 0xFF);}truncatedHash &= 0x7FFFFFFF;truncatedHash %= 1000000;return (int) truncatedHash; }

結(jié)論

現(xiàn)在,您可以使用Google Authenticator應(yīng)用程序,并使用它為您的用戶生成基于時(shí)間的密碼,并根據(jù)您自己的身份驗(yàn)證服務(wù)器進(jìn)行身份驗(yàn)證。

如您所見,所需的代碼非常簡(jiǎn)單,并且所有必需的加密功能均由運(yùn)行時(shí)本身提供。 唯一的麻煩是處理Java中的簽名類型。 請(qǐng)享用!

參考: Google身份驗(yàn)證器:在The Gray Blog上與我們的JCG合作伙伴 Enrico Crisostomo一起在您自己的Java身份驗(yàn)證服務(wù)器上使用 。

相關(guān)文章 :

  • Android Google Maps教程
  • Google地圖的開放替代品
  • Java中的Google ClientLogin實(shí)用程序
  • JBoss 4.2.x Spring 3 JPA Hibernate教程
  • Spring MVC3 Hibernate CRUD示例應(yīng)用程序
  • GWT Spring和Hibernate進(jìn)入數(shù)據(jù)網(wǎng)格世界
  • GWT 2 Spring 3 JPA 2 Hibernate 3.5教程

翻譯自: https://www.javacodegeeks.com/2011/12/google-authenticator-using-it-with-your.html

總結(jié)

以上是生活随笔為你收集整理的Google Authenticator:将其与您自己的Java身份验证服务器配合使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。