[Spring Boot] Google OAuth 2.0 登入後傳送Token到Server驗證

 先打開Spring Boot專案下的pom.xml,新增依賴(Maven ):

<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>7.1.1</version>
</dependency>

備註:如果你是用Gradle,那就是:

dependencies {
implementation 'com.google.firebase:firebase-admin:7.1.0'
}

然後點選右上角的按鈕讓他下載和安裝依賴套件


新增一個類別,貼上程式碼
package com.ruyut.demo;

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;
import com.google.api.client.http.javanet.NetHttpTransport;

import com.google.api.client.json.gson.GsonFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.GeneralSecurityException;

@Service
public class RuyutGoogleService {
GoogleIdTokenVerifier verifier =
new GoogleIdTokenVerifier(new NetHttpTransport(), new GsonFactory());

public void getDataByToken(String token) {

GoogleIdToken idToken = null;
try {
idToken = verifier.verify(token);
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


if (token != null) {
Payload payload = idToken.getPayload();


String iss = (String) payload.get("iss");
String sub = (String) payload.get("sub");
String azp = (String) payload.get("azp");
String aud = (String) payload.get("aud");
Long iat = (Long) payload.get("iat");
Long exp = (Long) payload.get("exp");

System.out.println("iss:" + iss);
System.out.println("sub:" + sub);
System.out.println("azp:" + azp);//授權的主持人
System.out.println("aud:" + aud);//ID令牌的目標受眾
System.out.println("iat:" + iat);
System.out.println("exp:" + exp);


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

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("email:" + email);
System.out.println("emailVerified:" + emailVerified);
System.out.println("name:" + name);
System.out.println("pictureUrl:" + pictureUrl);
System.out.println("locale:" + locale);
System.out.println("familyName:" + familyName);
System.out.println("givenName:" + givenName);

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


}

}

然後你在透過神奇的方式把資料傳入並呼叫這個程式碼就可以了,就看你們怎麼寫

這裡用測試案例來教大家怎麼使用

新增一個測試類別

抱歉阿大家,請自己生Token出來,這裡不能給你們..

package com.ruyut.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RuyutGoogleServiceTest {

@Autowired
RuyutGoogleService ruyutGoogleService;

@Test
public void getTest(){
String testToken = "你的token";
ruyutGoogleService.getDataByToken(testToken);
}
}

測試案例截圖:


也可以到這個網站直接讀取Token

至於Token每個參數詳細的資訊Google在這裡也有說明

留言