Skip to content

Spring JWT

snanoh edited this page Jul 10, 2022 · 1 revision

Spring + Jwt

Introduction

Spring + Security + Jwt + H2 Database로 간단한 인증 구현

JWT 인증 FLOW

image

access_token : 접근 제어 토큰 (만료 기준 30분) refresh_token : Access_token 만료시 재발급용 Token (만료 기준 7일)

정상 요청

  1. 로그인 할 때 Access_token과 Refresh_token 발급
  2. Api 요청 시 Header에 Authorization Bearer 담에서 요청
  3. 정상 Response

Access Token 만료

  1. Api 요청 시 Header에 Authorization Bearer 담에서 요청
  2. 401 Unauthorized 에러로 Response
  3. /auth/reissue에 Request Body로 Access_token과 Refresh_token을 담아서 요청
  4. 새로운 Access_token과 Refresh_token 발급하여 Response

Refresh Token 만료

  1. /auth/reissue 요청 시 401Unauthorized 에러 Response
  2. 로그인 페이지로 이동하여 로그인

API 명세서

로그인

명칭 description
기능 사용자 로그인
URI /auth/login
Http Method POST

Request example

{
    "email" : "[email protected]",
    "password" : "1234"
}

Response example

{
    "grantType": "bearer",
    "accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfVVNFUiIsImV4cCI6MTY1NzE4OTk2N30.Mya_Jy7c3p7fUOVE0wmWj0cz_t6iXrFYkCNPL58Mn0k",
    "refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NTc3OTQxNjd9.JUb9pTbHs4OUqdA2bXnkdaZbxsMN7iIYGC9_aoJ3uF0",
    "accessTokenExpiresIn": 1657189967878
}

회원가입

명칭 description
기능 사용자 회원가입
URI /auth/signup
Http Method POST

Request example

{
    "email" : "[email protected]",
    "password" : "1234"
}

Response example

    "ok"

Token 재발급

명칭 description
기능 Access token 재발급
URI /auth/reissue
Http Method POST

Request example

{
    "accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfVVNFUiIsImV4cCI6MTY1NzE4OTk2N30.Mya_Jy7c3p7fUOVE0wmWj0cz_t6iXrFYkCNPL58Mn0k",
    "refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NTc3OTQxOTh9.NqcP3FNk-Uhy9MVSHi1iVRifch-j63Xt4t2DRtTEF_Q"
}

Response example

{
    "grantType": "bearer",
    "accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfVVNFUiIsImV4cCI6MTY1NzE5MDAxOX0.dm-Fk5nPQA6L5p5lLnjkUsRypOLivRf8ovY0Oa45G-k",
    "refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NTc3OTQyMTl9.gRSviAowlxMK0Z4WwVFQ9YrI7V8RMcag5pp5vB2aZow",
    "accessTokenExpiresIn": 1657190019762
}

User 조회 (테스트용)

명칭 description
기능 User email 조회
URI /email
Http Method GET

Request example

Response example

Admin User 조회 (테스트용)

명칭 description
기능 Admin User email 조회
URI /admin/get-info
Http Method GET

Response example

admin

H2 Database Data Query

insert into member (id, authority, password, email)
values (1, 'ROLE_USER' , '$2a$10$3rMLv8cHccukZl8XAwJ.1.fX895FHAa186Hw3iESKJUdATWxALd.6', '[email protected]');
insert into member (id, authority, password, email)
values (1, 'ROLE_ADMIN' , '$2a$10$3rMLv8cHccukZl8XAwJ.1.fX895FHAa186Hw3iESKJUdATWxALd.6', '[email protected]');