GoogleIdTokenVerifier.verify始终返回null

时间:2020-10-29 22:54:26

标签: google-oauth google-oauth-java-client google-openidconnect

我正在尝试验证Google发送给我的应用程序的JWT令牌。我正在使用以下代码来验证JWT令牌,但是即使令牌有效,verifier.verify(token)始终返回null。我已经在另一个NodeJS代码中测试了令牌,并且在NodeJS中可以正常工作,但是在下面的Java代码中,我无法验证令牌。让我知道为什么verifier.verify(token)返回null的任何帮助都将受到赞赏。

public static boolean isRequestFromGoogle(String audience, String token) {

    
         //HttpTransport httpTransport = checkNotNull(Utils.getDefaultTransport());
         //JsonFactory jsonFactory = checkNotNull(Utils.getDefaultJsonFactory());
         
         HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
        GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(httpTransport,
                jsonFactory).setAudience(Collections.singletonList(audience)).build();

        try {
            System.out.println("token is " + token);
            System.out.println("verifier is " + verifier.toString());
            GoogleIdToken idToken = verifier.verify(token);
            if (idToken == null) {
                System.out.println("idToken is null");
                return false;
            }
            Payload payload = idToken.getPayload();
            String issuer = (String) payload.get("iss");
            String proj = (String) payload.get("aud");
            System.out.println("Issuer is" + issuer);
            System.out.println("Project is" + proj);

            return issuer.equals("https://accounts.google.com");
        } catch (GeneralSecurityException e) {
            
            e.printStackTrace();
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return false;

    }

0 个答案:

没有答案