sonFactory在GoogleIdTokenVerifier

sonFactory在GoogleIdTokenVerifier

本文介绍了安卓:什么是运输和jsonFactory在GoogleIdTokenVerifier.Builder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在打击code,什么是交通 jsonFactory ? (我不明白)

https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library

 进口com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
进口com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
进口com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;...GoogleIdTokenVerifier验证=新GoogleIdTokenVerifier.Builder(运输/ ** **在这里/,jsonFactory / ** **这里/)
.setAudience(Arrays.asList(CLIENT_ID))
//如果使用播放服务API 8.3或更新版本的检索Android上的标记,设置
//发行人https://accounts.google.com。否则,该发行者设定为
//accounts.google.com。如果您需要从多个来源验证令牌,建
//一个GoogleIdTokenVerifier为每间发行机构,并尝试他们俩。
.setIssuer(https://accounts.google.com)
。建立();//(通过HTTPS POST接收idTokenString)GoogleIdToken idToken = verifier.verify(idTokenString);
如果(idToken!= NULL){
  负荷负荷= idToken.getPayload();  //打印用户标识符
  字符串userid = payload.getSubject();
  的System.out.println(用户ID:+用户id);  //从有效载荷配置文件信息
  字符串email = payload.getEmail();
  布尔emailVerified =通过Boolean.valueOf(payload.getEmailVerified());
  字符串名称=(字符串)payload.get(名称);
  字符串pictureUrl =(字符串)payload.get(图片);
  string地区=(字符串)payload.get(区域设置);
  字符串familyName =(字符串)payload.get(FAMILY_NAME);
  字符串给定名称=(字符串)payload.get(GIVEN_NAME);  //使用或存储的个人资料信息
  // ...}其他{
  的System.out.println(无效的ID令牌。);
}


解决方案

的 GoogleIdTokenVerifier.Builder 返回GoogleIdTokenVerifier,这将使到 tokeninfo端点与运输你给它,并使用来创建一个解析器解析响应。

下面是一个使用GoogleIdTokenVerifier.Builder云端点项目的认证的例子

 公共类GoogleAuthenticator实现验证器{    私有静态最后记录器记录= Logger.getLogger(GoogleAuthenticator.class.getName());
    私有静态最终JacksonFactory jacksonFactory =新JacksonFactory();    //来源: https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library
    //如果使用播放服务API 8.3或更新版本的检索Android上的标记,设置
    //发行人https://accounts.google.com。否则,该发行者设定为
    //accounts.google.com。如果您需要从多个来源验证令牌,建
    //一个GoogleIdTokenVerifier为每间发行机构,并尝试他们俩。    GoogleIdTokenVerifier verifierForNewAndroidClients =新GoogleIdTokenVerifier.Builder(UrlFetchTransport.getDefaultInstance(),jacksonFactory)
            .setAudience(Arrays.asList(CRLConstants.IOS_CLIENT_ID,CRLConstants.ANDROID_CLIENT_ID_RELEASE,CRLConstants.ANDROID_CLIENT_ID_DEBUG))
            .setIssuer(https://accounts.google.com)
            。建立();    GoogleIdTokenVerifier verifierForOtherClients =新GoogleIdTokenVerifier.Builder(UrlFetchTransport.getDefaultInstance(),jacksonFactory)
            .setAudience(Arrays.asList(CRLConstants.IOS_CLIENT_ID,CRLConstants.ANDROID_CLIENT_ID_RELEASE,CRLConstants.ANDROID_CLIENT_ID_DEBUG))
            .setIssuer(accounts.google.com)
            。建立();    //自定义验证器类用于验证谷歌帐户
    @覆盖
    公众用户进行身份验证(HttpServletRequest的请求){        字符串标记= request.getHeader(google_id_token);
        如果(令牌!= NULL){            GoogleIdToken idToken = NULL;
            尝试{
                idToken = verifierForNewAndroidClients.verify(令牌);
                如果(idToken == NULL)idToken = verifierForOtherClients.verify(令牌);                如果(idToken!= NULL){                    GoogleIdToken.Payload有效载荷= idToken.getPayload();                    //从有效载荷配置文件信息
                    字符串userid = payload.getSubject();
                    字符串email = payload.getEmail();                    返回新GoogleUser(用户ID,电子邮件);                }其他{
                    log.warning(无效的谷歌ID令牌。);
                }            }赶上(GeneralSecurityException E){
                log.warning(e.getLocalizedMessage());
            }赶上(IOException异常五){
                log.warning(e.getLocalizedMessage());
            }        }        返回null;
    }}

in the blow code, whats is transport and jsonFactory ? (I do not understand)

https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;

...

GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport /**Here**/, jsonFactory /**Here**/)
.setAudience(Arrays.asList(CLIENT_ID))
// If you retrieved the token on Android using the Play Services 8.3 API or newer, set
// the issuer to "https://accounts.google.com". Otherwise, set the issuer to
// "accounts.google.com". If you need to verify tokens from multiple sources, build
// a GoogleIdTokenVerifier for each issuer and try them both.
.setIssuer("https://accounts.google.com")
.build();

// (Receive idTokenString by HTTPS POST)

GoogleIdToken idToken = verifier.verify(idTokenString);
if (idToken != null) {
  Payload payload = idToken.getPayload();

  // Print user identifier
  String userId = payload.getSubject();
  System.out.println("User ID: " + userId);

  // Get profile information from payload
  String email = payload.getEmail();
  boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
  String name = (String) payload.get("name");
  String pictureUrl = (String) payload.get("picture");
  String locale = (String) payload.get("locale");
  String familyName = (String) payload.get("family_name");
  String givenName = (String) payload.get("given_name");

  // Use or store profile information
  // ...

} else {
  System.out.println("Invalid ID token.");
}
解决方案

The GoogleIdTokenVerifier.Builder returns a GoogleIdTokenVerifier that will make a request to the tokeninfo endpoint with the transport you give it and use the JSONFactory to create a parser to parse the response.

Here is an example of an authenticator for a Cloud Endpoints project that uses the GoogleIdTokenVerifier.Builder

public class GoogleAuthenticator implements Authenticator {

    private static final Logger log = Logger.getLogger(GoogleAuthenticator.class.getName());
    private static final JacksonFactory jacksonFactory = new JacksonFactory();

    // From: https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library
    // If you retrieved the token on Android using the Play Services 8.3 API or newer, set
    // the issuer to "https://accounts.google.com". Otherwise, set the issuer to
    // "accounts.google.com". If you need to verify tokens from multiple sources, build
    // a GoogleIdTokenVerifier for each issuer and try them both.

    GoogleIdTokenVerifier verifierForNewAndroidClients = new GoogleIdTokenVerifier.Builder(UrlFetchTransport.getDefaultInstance(), jacksonFactory)
            .setAudience(Arrays.asList(CRLConstants.IOS_CLIENT_ID, CRLConstants.ANDROID_CLIENT_ID_RELEASE, CRLConstants.ANDROID_CLIENT_ID_DEBUG))
            .setIssuer("https://accounts.google.com")
            .build();

    GoogleIdTokenVerifier verifierForOtherClients = new GoogleIdTokenVerifier.Builder(UrlFetchTransport.getDefaultInstance(), jacksonFactory)
            .setAudience(Arrays.asList(CRLConstants.IOS_CLIENT_ID, CRLConstants.ANDROID_CLIENT_ID_RELEASE, CRLConstants.ANDROID_CLIENT_ID_DEBUG))
            .setIssuer("accounts.google.com")
            .build();

    // Custom Authenticator class for authenticating google accounts
    @Override
    public User authenticate(HttpServletRequest request) {

        String token = request.getHeader("google_id_token");
        if (token != null) {

            GoogleIdToken idToken = null;
            try {
                idToken = verifierForNewAndroidClients.verify(token);
                if(idToken == null) idToken = verifierForOtherClients.verify(token);

                if (idToken != null) {

                    GoogleIdToken.Payload payload = idToken.getPayload();

                    // Get profile information from payload
                    String userId = payload.getSubject();
                    String email = payload.getEmail();

                    return new GoogleUser(userId, email);

                } else {
                    log.warning("Invalid Google ID token.");
                }

            } catch (GeneralSecurityException e) {
                log.warning(e.getLocalizedMessage());
            } catch (IOException e) {
                log.warning(e.getLocalizedMessage());
            }

        }

        return null;
    }

}

这篇关于安卓:什么是运输和jsonFactory在GoogleIdTokenVerifier.Builder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:04