0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SpringBoot + FirebaseAdminSDK で FirebaseAuthentication のIDトークンを検証する

Last updated at Posted at 2022-07-04

手順

Firebaseプロジェクトの暗号鍵を生成する

Firebaseのプロジェクト設定から、秘密鍵を生成する。
ボタン押下で、JSONファイルが出力されるのでそれを保持する。

image.png

Java側のコード

※gradle or maven の設定は ココ

mainメソッド内で、FirebaseAppの宣言とかしてたりしてるけど、この辺の宣言する場所は良しなにすること。

@SpringBootApplication
@RestController
public class SpringbootApplication {

  public static void main(String[] args) throws IOException {

    FileInputStream serviceAccount = new FileInputStream("ダウンロードしたJSONファイルへのファイルパス");
    FirebaseOptions options = FirebaseOptions.builder()
        .setCredentials(GoogleCredentials.fromStream(serviceAccount))
        .build();
    FirebaseApp.initializeApp(options);

    SpringApplication.run(SpringbootApplication.class, args);
  }

  @GetMapping("/")
  public String hello() throws FirebaseAuthException {
    String idToken = "検証したいIDトークン";

    // エラー確認用のIDトークン ※よそ(example.com)で生成されたIDトークンが拒否されるの確認用
    // String idToken = "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogImlzcyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ4Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiAibi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDEzMTEyODA5NzAsCiAibmFtZSI6ICJKYW5lIERvZSIsCiAiZ2l2ZW5fbmFtZSI6ICJKYW5lIiwKICJmYW1pbHlfbmFtZSI6ICJEb2UiLAogImdlbmRlciI6ICJmZW1hbGUiLAogImJpcnRoZGF0ZSI6ICIwMDAwLTEwLTMxIiwKICJlbWFpbCI6ICJqYW5lZG9lQGV4YW1wbGUuY29tIiwKICJwaWN0dXJlIjogImh0dHA6Ly9leGFtcGxlLmNvbS9qYW5lZG9lL21lLmpwZyIKfQ.rHQjEmBqn9Jre0OLykYNnspA10Qql2rvx4FsD00jwlB0Sym4NzpgvPKsDjn_wMkHxcp6CilPcoKrWHcipR2iAjzLvDNAReF97zoJqq880ZD1bwY82JDauCXELVR9O6_B0w3K-E7yM2macAAgNCUwtik6SjoSUZRcf-O5lygIyLENx882p6MtmwaL1hd6qn5RZOQ0TLrOYu0532g9Exxcm-ChymrB4xLykpDj3lUivJt63eEGGN6DH5K6o33TcxkIjNrCD4XB1CKKumZvCedgHHF3IAK4dVEDSUoGlH9z4pP_eWYNXvqQOjGs-rDaQzUHl6cQQWNiDpWOl_lxXjQEvQ";

    FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdToken(idToken);

    return "Hello world!!!" + decodedToken.getUid();
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?