-
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.
[feature] refreshToken을 이용한 accessToken 재발급 API #56
[feature] refreshToken을 이용한 accessToken 재발급 API
- Loading branch information
Showing
5 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/sanbosillok/sanbosillokserver/api/auth/controller/TokenController.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,23 @@ | ||
package com.sanbosillok.sanbosillokserver.api.auth.controller; | ||
|
||
import com.sanbosillok.sanbosillokserver.api.auth.dto.AccessTokenResponse; | ||
import com.sanbosillok.sanbosillokserver.api.auth.dto.RefreshTokenRequest; | ||
import com.sanbosillok.sanbosillokserver.api.auth.service.TokenService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/token") | ||
public class TokenController { | ||
private final TokenService tokenService; | ||
|
||
@GetMapping("/refresh") | ||
public AccessTokenResponse getNewToken(@RequestBody @Valid RefreshTokenRequest refreshTokenRequest) { | ||
return tokenService.reIssueAccessToken(refreshTokenRequest.getRefreshToken()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/sanbosillok/sanbosillokserver/api/auth/service/TokenService.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,34 @@ | ||
package com.sanbosillok.sanbosillokserver.api.auth.service; | ||
|
||
import com.sanbosillok.sanbosillokserver.api.auth.dto.AccessTokenResponse; | ||
import com.sanbosillok.sanbosillokserver.api.member.domain.Member; | ||
import com.sanbosillok.sanbosillokserver.api.member.repository.MemberRepository; | ||
import com.sanbosillok.sanbosillokserver.config.jwt.JwtTokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TokenService { | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final MemberRepository memberRepository; | ||
|
||
public AccessTokenResponse reIssueAccessToken(String refreshToken) { | ||
|
||
if (refreshToken == null) { | ||
throw new IllegalArgumentException("토큰이 비어있습니다."); | ||
} | ||
|
||
if (jwtTokenProvider.isExpired(refreshToken)) { | ||
throw new IllegalArgumentException("만료된 refresh token 입니다."); | ||
} | ||
|
||
Long userId = jwtTokenProvider.getUserId(refreshToken); | ||
Member member = memberRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("잘못된 token 형식입니다.")); | ||
|
||
String username = member.getUsername(); | ||
String role = member.getRole().toString(); | ||
|
||
return new AccessTokenResponse(jwtTokenProvider.createAccessToken(username, role)); | ||
} | ||
} |
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
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