簡體   English   中英

使用 Java 驗證從 Google OAuth 2.0 API 服務器端收到的訪問令牌

[英]Verifying the access token received from Google OAuth 2.0 API server-side with Java

我目前正在探索讓我們的用戶使用 Google 身份平台進行簽名的可能性。

我在 React 中開發的客戶端加載 Javascript 庫並使用我在 Google Cloud 中創建的客戶端 ID 接收訪問令牌。 可以在這里看到代碼:

    <script src="https://accounts.google.com/gsi/client" async defer></script>
    <div id="g_id_onload"
         data-client_id="CLIENT ID HERE"
         data-login_uri="http://localhost:8080/test"
         data-prompt_parent_id="g_id_onload"
         style="position: absolute; top: 200px; left: 550px;
            width: 0; height: 0; z-index: 1001;"
         data-context="signup">
    </div>

單擊此庫生成的登錄按鈕后,訪問令牌將發送到我的后端端點 (http://localhost:8080/test) 進行驗證並從 JWT 中提取有效負載。 可以在這里看到代碼:

    @PostMapping("/test")
    public ResponseEntity<?> test(HttpServletRequest req, @CookieValue("g_csrf_token") String cookie) throws GeneralSecurityException, IOException {

        GooglePublicKeysManager manager = new GooglePublicKeysManager(new NetHttpTransport(), new GsonFactory());

        GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(manager)
                // Specify the CLIENT_ID of the app that accesses the backend:
                .setAudience(Collections.singletonList("CLIENT ID HERE"))
                // Or, if multiple clients access the backend:
                //.setAudience(Arrays.asList(CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3))
                .build();

        if(req.getParameter("g_csrf_token") == null){
            return ResponseEntity.badRequest().body("No CSRF token in body.");
        }

        if(cookie == null){
            return ResponseEntity.badRequest().body("No CSRF token in header.");
        }

        if(!Objects.equals(req.getParameter("g_csrf_token"), cookie)){
            return ResponseEntity.badRequest().body("Failed to verify double submit cookie.");
        }

        // this is the JWT token received from the client-side
        String credentials = req.getParameter("credential");

        GoogleIdToken idToken = verifier.verify(credentials);
        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");

            System.out.println(name);

        } else {
            System.out.println("Invalid ID token.");
        }

        return ResponseEntity.ok("Login Success!");
    }
}

誰能告訴我這是否是實現這一點的正確和安全的方式? 此設置正在運行,並且驗證了令牌並提取了有效負載,但我擔心安全性。 我發現谷歌文​​檔非常混亂,而且我對使用他們的 API 很陌生。

驗證訪問令牌的唯一方法是使用它。 向您選擇的 api 發送請求,如果您無權訪問,它會告訴您。

Id 令牌來自打開的 id 連接和對用戶登錄調用的響應。 它需要一個 id 令牌。 您只能在 id 令牌上獲取有效負載,因為它是 jwt。 我的訪問令牌不是 jwt,它沒有用戶聲稱可以驗證。

有效載荷有效載荷 = idToken.getPayload();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM