-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor/#48 jwt refresh 토큰 쿠키에 전송 및 swagger ui 변경 #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
src/main/java/com/moplus/moplus_server/domain/auth/controller/AuthControllerDocs.java
This file contains hidden or 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,81 @@ | ||
| package com.moplus.moplus_server.domain.auth.controller; | ||
|
|
||
| import com.moplus.moplus_server.domain.auth.dto.request.AdminLoginRequest; | ||
| import com.moplus.moplus_server.domain.auth.dto.response.AccessTokenResponse; | ||
| import com.moplus.moplus_server.global.error.ErrorResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.headers.Header; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.ResponseEntity; | ||
|
|
||
| @Tag(name = "인증", description = "인증 관련 API") | ||
| public interface AuthControllerDocs { | ||
|
|
||
| @Operation( | ||
| summary = "어드민 로그인", | ||
| description = "이메일과 비밀번호로 로그인하여 액세스 토큰을 발급받고 리프레시 토큰을 쿠키에 설정합니다.", | ||
| responses = { | ||
| @ApiResponse( | ||
| responseCode = "200", | ||
| description = "로그인 성공", | ||
| content = @Content( | ||
| mediaType = "application/json", | ||
| schema = @Schema(implementation = AccessTokenResponse.class) | ||
| ), | ||
| headers = @Header( | ||
| name = "Set-Cookie", | ||
| description = "리프레시 토큰이 담긴 HTTP Only 쿠키", | ||
| schema = @Schema( | ||
| type = "string", | ||
| example = "refreshToken=xxx; Path=/; HttpOnly; Secure; SameSite=None" | ||
| ) | ||
| ) | ||
| ), | ||
| @ApiResponse( | ||
| responseCode = "401", | ||
| description = "인증 실패 (잘못된 이메일 또는 비밀번호)", | ||
| content = @Content(schema = @Schema(implementation = ErrorResponse.class)) | ||
| ) | ||
| } | ||
| ) | ||
| ResponseEntity<AccessTokenResponse> adminLogin(AdminLoginRequest request); | ||
|
|
||
| @Operation( | ||
| summary = "토큰 재발급", | ||
| description = "리프레시 토큰을 통해 새로운 액세스 토큰을 발급하고 새로운 리프레시 토큰을 쿠키에 설정합니다.", | ||
| responses = { | ||
| @ApiResponse( | ||
| responseCode = "200", | ||
| description = "토큰 재발급 성공", | ||
| content = @Content( | ||
| mediaType = "application/json", | ||
| schema = @Schema(implementation = AccessTokenResponse.class) | ||
| ), | ||
| headers = @Header( | ||
| name = "Set-Cookie", | ||
| description = "새로운 리프레시 토큰이 담긴 HTTP Only 쿠키", | ||
| schema = @Schema( | ||
| type = "string", | ||
| example = "refreshToken=xxx; Path=/; HttpOnly; Secure; SameSite=None" | ||
| ) | ||
| ) | ||
| ), | ||
| @ApiResponse( | ||
| responseCode = "401", | ||
| description = "유효하지 않은 리프레시 토큰", | ||
| content = @Content(schema = @Schema(implementation = ErrorResponse.class)) | ||
| ), | ||
| @ApiResponse( | ||
| responseCode = "404", | ||
| description = "리프레시 토큰 쿠키 없음", | ||
| content = @Content(schema = @Schema(implementation = ErrorResponse.class)) | ||
| ) | ||
| } | ||
| ) | ||
| ResponseEntity<AccessTokenResponse> reissueToken(HttpServletRequest request, HttpServletResponse response); | ||
| } |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/moplus/moplus_server/domain/auth/dto/request/AdminLoginRequest.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| package com.moplus.moplus_server.domain.auth.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record AdminLoginRequest( | ||
| @NotNull(message = "이메일을 입력해주세요.") | ||
| String email, | ||
| @NotNull(message = "비밀번호를 입력해주세요.") | ||
| String password | ||
| ) { | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/moplus/moplus_server/domain/auth/dto/response/AccessTokenResponse.java
This file contains hidden or 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 @@ | ||
| package com.moplus.moplus_server.domain.auth.dto.response; | ||
|
|
||
| public record AccessTokenResponse( | ||
| String accessToken | ||
| ) {} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/moplus/moplus_server/domain/auth/service/AuthService.java
This file contains hidden or 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,65 @@ | ||
| package com.moplus.moplus_server.domain.auth.service; | ||
|
|
||
| import com.moplus.moplus_server.domain.auth.dto.response.TokenResponse; | ||
| import com.moplus.moplus_server.domain.member.domain.Member; | ||
| import com.moplus.moplus_server.domain.member.service.MemberService; | ||
| import com.moplus.moplus_server.global.error.exception.ErrorCode; | ||
| import com.moplus.moplus_server.global.error.exception.InvalidValueException; | ||
| import com.moplus.moplus_server.global.security.exception.JwtInvalidException; | ||
| import com.moplus.moplus_server.global.security.utils.JwtUtil; | ||
| import io.jsonwebtoken.Claims; | ||
| import io.jsonwebtoken.ExpiredJwtException; | ||
| import io.jsonwebtoken.MalformedJwtException; | ||
| import io.jsonwebtoken.security.SignatureException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.authentication.BadCredentialsException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AuthService { | ||
|
|
||
| private final JwtUtil jwtUtil; | ||
| private final MemberService memberService; | ||
|
|
||
| @Transactional | ||
| public TokenResponse reissueToken(String refreshToken) { | ||
| if (refreshToken == null) { | ||
| throw new InvalidValueException(ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
|
|
||
| Claims claims = getClaims(refreshToken); | ||
| final Member member = getMemberById(claims.getSubject()); | ||
|
|
||
| // 새로운 액세스 토큰과 리프레시 토큰 생성 | ||
| String newAccessToken = jwtUtil.generateAccessToken(member); | ||
| String newRefreshToken = jwtUtil.generateRefreshToken(member); | ||
|
|
||
| return new TokenResponse(newAccessToken, newRefreshToken); | ||
| } | ||
|
|
||
| private Claims getClaims(String refreshToken) { | ||
| Claims claims; | ||
| try { | ||
| claims = jwtUtil.getRefreshTokenClaims(refreshToken); | ||
| } catch (ExpiredJwtException expiredJwtException) { | ||
| throw new JwtInvalidException(ErrorCode.EXPIRED_TOKEN.getMessage()); | ||
| } catch (SignatureException signatureException) { | ||
| throw new JwtInvalidException(ErrorCode.WRONG_TYPE_TOKEN.getMessage()); | ||
| } catch (MalformedJwtException malformedJwtException) { | ||
| throw new JwtInvalidException(ErrorCode.UNSUPPORTED_TOKEN.getMessage()); | ||
| } catch (IllegalArgumentException illegalArgumentException) { | ||
| throw new JwtInvalidException(ErrorCode.UNKNOWN_ERROR.getMessage()); | ||
| } | ||
| return claims; | ||
| } | ||
|
|
||
| private Member getMemberById(String id) { | ||
| try { | ||
| return memberService.getMemberById(Long.parseLong(id)); | ||
| } catch (Exception e) { | ||
| throw new BadCredentialsException(ErrorCode.BAD_CREDENTIALS.getMessage()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
3 changes: 3 additions & 0 deletions
3
src/main/java/com/moplus/moplus_server/domain/concept/dto/response/ConceptTagResponse.java
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쿠키 방식은 처음 보는데, 함수가 깔끔하게 존재하는군요. 고생하셨습니다!