Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBootとNext.jsで認証機能を作成する #24

Open
2 tasks
healthybamboo opened this issue May 31, 2024 · 1 comment
Open
2 tasks

SpringBootとNext.jsで認証機能を作成する #24

healthybamboo opened this issue May 31, 2024 · 1 comment
Labels
新規 新しい投稿につけるラベル

Comments

@healthybamboo
Copy link
Owner

healthybamboo commented May 31, 2024

ポイント

  • SpringBootでJWT認証のやりかた
  • SpringBootでパスベースの認可制御のやりかた
  • Next.jsでトークンの格納と取り出し処理
  • 認可の可否による表示ページの切り替え

やること

  • SpringBootでRestAPIサーバーを構築してjwt認証ができるようにする
  • Next.jsでの認可制御

Design

jwt

payload

{
  "iat": "<トークン発行者>",
  "sub" : "<トークンの発行者>"
  "exp" : "<トークンの期限>"
}

SpringBoot

POST: /api/login

送信データ

{
   "username": "<ユーザー名>",
   "password": "<パスワード>"
}

返信データ(ログイン成功時)

{
  "token": "<JWTのトークン>"
}

返信データ(ログイン失敗時)

{
  "error" : "failed login"
}

GET: /api/public/hello

認証の必要のないアクセス

受信データ

{ 
  "message" : "hello public"
}

GET: /api/private/hello

認証の必要が必要なアクセス

受信データ(認証成功時)

{
  "message" : "hello private"
}

受信データ(認証失敗時)

{
  "error" : "invalid token"
}

Next.js

/login

初期

  • ログイン用のフォームが表示される
    • id=usernameのテキストボックス
    • id=passwordのテキストボックス

フォームの送信

  • 入力されたデータを/api/authへPOSTメソッドで送信する

フォーム送信後

  • ログインに成功した(200)場合は/publicへ遷移する
  • ログインに失敗した(401)場合は再び/loginページを表示し、上部にエラーメッセージを表示する

/private

  • アクセストークンが格納されている場合に表示される
    • ない場合は/publicへ遷移される
  • アクセストークンを利用して、/api/private/helloへGETリクエストでmessageを要求する
  • 結果を中央へ表示する
    • 取得に成功した場合は、通常の文字でメッセージの内容を表示する
    • 取得に失敗した場合は、赤文字でエラーを表示する

/public

  • /api/public/privateへGETリクエストでmessageを要求する
  • 結果を中央へ表示する
    • 取得に成功した場合は、通常の文字でメッセージの内容を表示する
    • 取得に失敗した場合は、赤文字でエラーを表示する

作業場所

https://github.com/healthybamboo/auth_demo

参考文献

@healthybamboo healthybamboo added the 新規 新しい投稿につけるラベル label May 31, 2024
@healthybamboo healthybamboo changed the title SpringBootとNext.jsで SpringBootとNext.jsで認証機能を作成する May 31, 2024
@healthybamboo healthybamboo self-assigned this Aug 30, 2024
Copy link
Contributor

Branch new/#24_create_authentication_feature_with_springboot_and_nextjs has been created.

@healthybamboo healthybamboo removed their assignment Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
新規 新しい投稿につけるラベル
Projects
None yet
Development

No branches or pull requests

1 participant