1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package utils;
import com.warrenstrange.googleauth.GoogleAuthenticator; import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
import java.io.UnsupportedEncodingException; import java.net.URLEncoder;
public class GoogleAuthUtil {
public static String generaKey(){ GoogleAuthenticator gAuth = new GoogleAuthenticator(); GoogleAuthenticatorKey key = gAuth.createCredentials(); return key.getKey(); }
public static String createGoogleAuthQRCodeData(String key, String account,String issuer) throws UnsupportedEncodingException { return String.format("otpauth://totp/%s?secret=%s&issuer=%s", URLEncoder.encode(issuer + ":" + account, "UTF-8").replace("+", "%20"), URLEncoder.encode(key, "UTF-8").replace("+", "%20"), URLEncoder.encode(issuer, "UTF-8").replace("+", "%20")); }
public static void main(String[] args) throws UnsupportedEncodingException { String s = generaKey(); System.out.println(s); String googleAuthQRCodeData = createGoogleAuthQRCodeData(s, "anthony", "V16"); System.out.println(googleAuthQRCodeData); }
public static boolean check(String key,Integer code){ GoogleAuthenticator gAuth = new GoogleAuthenticator(); boolean isCodeValid = gAuth.authorize(key, code); return isCodeValid; } }
|