-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code Refactoring: Token 관련 로직과 컨트롤러 분리, 별도 예외 클래스 생성
- Loading branch information
1 parent
0695dde
commit d5aa617
Showing
8 changed files
with
148 additions
and
113 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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/aisip/OnO/backend/exception/ExpiredTokenException.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,11 @@ | ||
package com.aisip.OnO.backend.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.UNAUTHORIZED) // 401 Unauthorized | ||
public class ExpiredTokenException extends TokenException { | ||
public ExpiredTokenException(String message) { | ||
super(message); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/aisip/OnO/backend/exception/InvalidTokenException.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,11 @@ | ||
package com.aisip.OnO.backend.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.UNAUTHORIZED) // 401 Unauthorized | ||
public class InvalidTokenException extends TokenException { | ||
public InvalidTokenException(String message) { | ||
super(message); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/aisip/OnO/backend/exception/MissingTokenException.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,11 @@ | ||
package com.aisip.OnO.backend.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) // 400 Bad Request | ||
public class MissingTokenException extends TokenException { | ||
public MissingTokenException(String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/aisip/OnO/backend/exception/TokenException.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,8 @@ | ||
package com.aisip.OnO.backend.exception; | ||
|
||
|
||
public class TokenException extends RuntimeException { | ||
public TokenException(String message) { | ||
super(message); | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
src/main/java/com/aisip/OnO/backend/service/JwtTokenService.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,62 @@ | ||
package com.aisip.OnO.backend.service; | ||
|
||
import com.aisip.OnO.backend.Auth.JwtTokenProvider; | ||
import com.aisip.OnO.backend.Dto.Token.TokenResponseDto; | ||
import com.aisip.OnO.backend.Dto.User.UserResponseDto; | ||
import com.aisip.OnO.backend.exception.ExpiredTokenException; | ||
import com.aisip.OnO.backend.exception.InvalidTokenException; | ||
import com.aisip.OnO.backend.exception.MissingTokenException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class JwtTokenService { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
/** | ||
* ✅ 유저 정보를 기반으로 새로운 액세스/리프레시 토큰 생성 | ||
*/ | ||
public TokenResponseDto generateTokens(UserResponseDto user) { | ||
String accessToken = jwtTokenProvider.createAccessToken(user.getUserId()); | ||
String refreshToken = jwtTokenProvider.createRefreshToken(user.getUserId()); | ||
|
||
log.info("AccessToken: {}", accessToken); | ||
log.info("RefreshToken: {}", refreshToken); | ||
log.info("User {} (ID: {}) has logged in", user.getUserName(), user.getUserId()); | ||
|
||
return new TokenResponseDto(accessToken, refreshToken); | ||
} | ||
|
||
/** | ||
* ✅ 액세스 토큰 검증 | ||
*/ | ||
public void verifyAccessToken(String authorizationHeader) { | ||
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { | ||
throw new MissingTokenException("Authorization header missing or invalid"); | ||
} | ||
|
||
String accessToken = authorizationHeader.substring(7); | ||
if (!jwtTokenProvider.validateToken(accessToken)) { | ||
throw new InvalidTokenException("Invalid or expired access token"); | ||
} | ||
} | ||
|
||
/** | ||
* ✅ 리프레시 토큰을 이용한 액세스 토큰 갱신 | ||
*/ | ||
public TokenResponseDto refreshAccessToken(String refreshToken) { | ||
if (!jwtTokenProvider.validateToken(refreshToken)) { | ||
throw new ExpiredTokenException("Invalid or expired refresh token"); | ||
} | ||
|
||
Long userId = Long.parseLong(jwtTokenProvider.getSubjectFromToken(refreshToken)); | ||
String newAccessToken = jwtTokenProvider.createAccessToken(userId); | ||
String newRefreshToken = jwtTokenProvider.createRefreshToken(userId); | ||
|
||
return new TokenResponseDto(newAccessToken, newRefreshToken); | ||
} | ||
} |