0

我正在我的网络应用程序上开发谷歌登录。我已经在我的服务器上发送了 ID 令牌,然后我想验证令牌的完整性,但在身份验证中它在 GoogleIdTokenVerifier 中给出 SSL 异常错误。我该如何解决?

public class VerifyController {
         public static final String CLIENT_ID = "";
         private static final String APPLICATION_NAME = "";
         public static GoogleIdTokenVerifier verifier ;
         public static GoogleIdToken token;
         private static NetHttpTransport transport;
         private static JsonFactory mJFactory;

        public Result validate(@PathParam("id") String idtoken) {
            try{
            // TODO Auto-generated method stub
            System.out.println("IN validate");
            System.out.println(idtoken);
            transport = new NetHttpTransport();
            mJFactory = new GsonFactory();
            verifier = new GoogleIdTokenVerifier.Builder(transport, mJFactory)
                    .setAudience(Arrays.asList(CLIENT_ID))
                    .build();
            token = GoogleIdToken.parse(mJFactory, idtoken);
             GoogleIdToken token = GoogleIdToken.parse(mJFactory, idtoken);
                if (verifier.verify(token)) {
                  Payload payload = token.getPayload();
                  System.out.println(payload);
                  if (payload.getHostedDomain().equals(APPLICATION_NAME)
                      // If multiple clients access the backend server:
                       {
                    System.out.println("User ID: " + payload.getSubject());
                  } else {
                    System.out.println("Invalid Domain.");
                  }
                } else {
                  System.out.println("null ID token.");
                }
                return null;
            }catch(Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
4

1 回答 1

1

您需要在创建 GoogleIdTokenVerifier 对象时设置Issuer

verifier = new GoogleIdTokenVerifier.Builder(transport, mJFactory)
                .setAudience(Arrays.asList(CLIENT_ID))
                .build(); //instead of this use below code

verifier = new GoogleIdTokenVerifier.Builder(transport, mJFactory)
                .setAudience(Arrays.asList(CLIENT_ID))
                .setIssuer("accounts.google.com")
                .build();
于 2016-04-27T06:10:01.007 回答