-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Lunawood/master
fix: JWT 토큰 추가 및 에러 핸들러 추가
- Loading branch information
Showing
27 changed files
with
651 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
docker-compose down | ||
docker rmi docker-test-server-server:latest | ||
docker-compose up -d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,8 @@ CREATE DATABASE IF NOT EXISTS toothFairy; | |
USE toothFairy; | ||
|
||
CREATE TABLE user_tb ( | ||
email VARCHAR(255) PRIMARY KEY, | ||
pet_name VARCHAR(50), | ||
pet_weight INT, | ||
access_token VARCHAR(255), | ||
refresh_token VARCHAR(255) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; | ||
|
||
INSERT INTO user_tb (email, pet_name, pet_weight, access_token, refresh_token) | ||
VALUES ('[email protected]', 'Charlie', 12, 'test_access_token', 'test_refresh_token'); | ||
id BIGINT PRIMARY KEY, | ||
pet_name VARCHAR(50) NOT NULL, | ||
pet_weight INT NOT NULL, | ||
random_id VARCHAR(255) NOT NULL | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
docker-test-server/src/main/java/com/example/server/controller/RefreshTokenController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.example.server.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.example.server.error.CErrorResponse; | ||
import com.example.server.error.CException; | ||
import com.example.server.error.ErrorCode; | ||
import com.example.server.jwt.JwtTokenService; | ||
import com.example.server.model.Token; | ||
import com.example.server.service.RefreshTokenService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/refresh") | ||
public class RefreshTokenController { | ||
private final RefreshTokenService refreshTokenService; | ||
private final JwtTokenService jwtTokenService; | ||
|
||
// RefreshToken으로 AccessToken과 RefreshToken 새로 발급 | ||
@GetMapping("") | ||
public ResponseEntity<?> refreshAccessToken (@RequestHeader("RefreshToken") String RefreshToken) { | ||
// 1. RefreshToken Valid? | ||
try { | ||
if(jwtTokenService.validateRefreshToken(RefreshToken) == false) { | ||
throw new CException(ErrorCode.INVALID_TOKEN); | ||
} | ||
} catch (Exception e) { | ||
throw new CException(ErrorCode.INVALID_TOKEN); | ||
} | ||
|
||
// 2. Get userId & radomId from RefreshToken | ||
Long userId = null; | ||
String randomId = null; | ||
try { | ||
userId = jwtTokenService.extractIdFromRefreshToken(RefreshToken); | ||
randomId = jwtTokenService.extractRandomIdFromRefreshToken(RefreshToken); | ||
} catch (Exception e) { | ||
throw new CException(ErrorCode.INVALID_TOKEN); | ||
} | ||
|
||
if(userId == null || randomId == null) | ||
throw new CException(ErrorCode.INVALID_TOKEN); | ||
|
||
// 3. Check userId & radomId | ||
try { | ||
if(refreshTokenService.checkIdAndRandomId(userId, randomId) == false) { | ||
throw new CException(ErrorCode.INVALID_TOKEN); | ||
} | ||
} catch (Exception e) { | ||
throw new CException(ErrorCode.INVALID_TOKEN); | ||
} | ||
|
||
// 4. JWT AccessToken, RefreshToken 토큰 발급 | ||
Token token = new Token(); | ||
String new_randomId = jwtTokenService.generateRandomId(); | ||
try { | ||
String accessToken = jwtTokenService.createAccessToken(userId); | ||
String refreshToken = jwtTokenService.createRefreshToken(userId, new_randomId); | ||
token.setAccessToken(accessToken); | ||
token.setRefreshToken(refreshToken); | ||
} catch(Exception e) { | ||
throw new CException(ErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
// 5. RefreshToken RandomID Database에 수정 | ||
try { | ||
refreshTokenService.updateRandomIdByUserId(userId, new_randomId); | ||
} catch(Exception e) { | ||
throw new CException(ErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
return ResponseEntity | ||
.status(ErrorCode.SUCCESS.getStatus()) | ||
.body(CErrorResponse.builder() | ||
.status(ErrorCode.SUCCESS.getStatus()) | ||
.message(token) | ||
.build() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.