-
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 #135 from studio-recoding/feat-refresh-token
[🚀feat] refresh token 로직 구현
- Loading branch information
Showing
17 changed files
with
188 additions
and
81 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/Ness/Backend/domain/auth/AuthController.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,31 @@ | ||
package Ness.Backend.domain.auth; | ||
|
||
import Ness.Backend.domain.auth.dto.request.PostRefreshTokenDto; | ||
import Ness.Backend.domain.auth.dto.response.GetJwtTokenDto; | ||
import Ness.Backend.domain.member.entity.Member; | ||
import Ness.Backend.global.auth.AuthUser; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(value = "/auth") | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
@PostMapping("/logout") | ||
@Operation(summary = "로그아웃 요청", description = "로그아웃 요청 API 입니다.") | ||
public void logout(@AuthUser Member member, @RequestBody PostRefreshTokenDto postRefreshTokenDto) { | ||
authService.logout(member, postRefreshTokenDto); | ||
} | ||
|
||
@PostMapping("/reIssuance") | ||
@Operation(summary = "JWT access 토큰 재발급 요청", description = "JWT access 토큰 재발급 요청 API 입니다.") | ||
public GetJwtTokenDto reIssuance(@AuthUser Member member, @RequestBody PostRefreshTokenDto postRefreshTokenDto) { | ||
return authService.reIssuance(member, postRefreshTokenDto); | ||
} | ||
} |
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,53 @@ | ||
package Ness.Backend.domain.auth; | ||
|
||
import Ness.Backend.domain.auth.dto.request.PostRefreshTokenDto; | ||
import Ness.Backend.domain.auth.dto.response.GetJwtTokenDto; | ||
import Ness.Backend.domain.auth.inmemory.RefreshTokenRepository; | ||
import Ness.Backend.domain.auth.jwt.JwtTokenProvider; | ||
import Ness.Backend.domain.member.entity.Member; | ||
import Ness.Backend.global.error.ErrorCode; | ||
import Ness.Backend.global.error.exception.UnauthorizedException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Date; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
@Transactional | ||
public void logout(Member member, PostRefreshTokenDto postRefreshTokenDto) { | ||
/* refreshToken 만료 여부 확인 */ | ||
if(refreshTokenRepository.findRefreshTokenByJwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken()).isEmpty()){ | ||
throw new UnauthorizedException(ErrorCode.INVALID_REFRESH_TOKEN); | ||
} | ||
|
||
refreshTokenRepository.deleteRefreshTokenByJwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken()); | ||
SecurityContextHolder.clearContext(); | ||
} | ||
|
||
@Transactional | ||
public GetJwtTokenDto reIssuance(Member member, PostRefreshTokenDto postRefreshTokenDto) { | ||
/* refreshToken 유효성 확인 */ | ||
if (!jwtTokenProvider.validRefreshToken(postRefreshTokenDto.getJwtRefreshToken())) { | ||
throw new UnauthorizedException(ErrorCode.INVALID_TOKEN); | ||
} | ||
|
||
/* refreshToken 만료 여부 확인 */ | ||
if(refreshTokenRepository.findRefreshTokenByJwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken()).isEmpty()){ | ||
throw new UnauthorizedException(ErrorCode.INVALID_REFRESH_TOKEN); | ||
} | ||
|
||
final GetJwtTokenDto generateToken = GetJwtTokenDto.builder() | ||
.jwtAccessToken(jwtTokenProvider.generateAccessToken(member.getEmail(), new Date())) | ||
.jwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken()) | ||
.build(); | ||
|
||
return generateToken; | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
src/main/java/Ness/Backend/domain/auth/dto/LoginRequestDto.java
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/main/java/Ness/Backend/domain/auth/dto/RegisterRequestDto.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
src/main/java/Ness/Backend/domain/auth/dto/ResourceDto.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/Ness/Backend/domain/auth/dto/request/PostRefreshTokenDto.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,12 @@ | ||
package Ness.Backend.domain.auth.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
public class PostRefreshTokenDto { | ||
@NotNull | ||
private String jwtRefreshToken; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/Ness/Backend/domain/auth/dto/response/GetJwtTokenDto.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,21 @@ | ||
package Ness.Backend.domain.auth.dto.response; | ||
|
||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
@NoArgsConstructor | ||
public class GetJwtTokenDto { | ||
private String jwtAccessToken; | ||
private String jwtRefreshToken; | ||
|
||
@Builder | ||
public GetJwtTokenDto(String jwtAccessToken, String jwtRefreshToken) { | ||
this.jwtAccessToken = jwtAccessToken; | ||
this.jwtRefreshToken = jwtRefreshToken; | ||
} | ||
} |
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
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
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
17 changes: 17 additions & 0 deletions
17
src/main/java/Ness/Backend/global/error/exception/UnauthorizedException.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,17 @@ | ||
package Ness.Backend.global.error.exception; | ||
|
||
import Ness.Backend.global.error.ErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class UnauthorizedException extends BaseException { | ||
public UnauthorizedException() { | ||
super(ErrorCode.UNAUTHORIZED_ACCESS, ErrorCode.UNAUTHORIZED_ACCESS.getMessage()); | ||
} | ||
public UnauthorizedException(String message) { | ||
super(ErrorCode.UNAUTHORIZED_ACCESS, message); | ||
} | ||
public UnauthorizedException(ErrorCode errorCode) { | ||
super(errorCode, errorCode.getMessage()); | ||
} | ||
} |
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