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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

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

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

生成密鑰

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

一些臨時代碼(使用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);

現在我們要提取對應于密鑰的字節,并使用Base32編碼對其進行編碼。 我正在使用Apache Common Codec庫來獲取編解碼器實現:

// 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中

您可以手動將密鑰加載到Google Authenticator中,或生成QR條碼以使應用程序從中加載密鑰。 如果要使用Google服務生成QR條碼,則可以使用以下代碼生成相應的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); }

驗證碼

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

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

結論

現在,您可以使用Google Authenticator應用程序,并使用它為您的用戶生成基于時間的密碼,并根據您自己的身份驗證服務器進行身份驗證。

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

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

相關文章 :

  • Android Google Maps教程
  • Google地圖的開放替代品
  • Java中的Google ClientLogin實用程序
  • JBoss 4.2.x Spring 3 JPA Hibernate教程
  • Spring MVC3 Hibernate CRUD示例應用程序
  • GWT Spring和Hibernate進入數據網格世界
  • GWT 2 Spring 3 JPA 2 Hibernate 3.5教程

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。