-
Notifications
You must be signed in to change notification settings - Fork 1
[refactor] 리졸버 로직 개선, swagger에서 userId 필수 파라미터 입력인곳 제거 #169
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac36f55
[test] 테스트코드에서 리뷰 등록 시 리뷰 카운트 증가 타입 변경
seung-in-Yoo ce61e36
[refactor] 리졸버에서 토큰으로 꺼내도록 변경
seung-in-Yoo b44069f
[refactor] swagger에서 필수 파라미터 userId 입력 삭제
seung-in-Yoo 47b3255
[feat] Auth (리졸버) 관련 에러케이스 추가
seung-in-Yoo 3faefa3
[refactor] 리졸버 관련 예외처리 변경
seung-in-Yoo 67a5faa
[refactor] 코드리뷰 반영
seung-in-Yoo 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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/wayble/server/auth/exception/AuthErrorCase.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,15 @@ | ||
| package com.wayble.server.auth.exception; | ||
|
|
||
| import com.wayble.server.common.exception.ErrorCase; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum AuthErrorCase implements ErrorCase { | ||
| UNAUTHORIZED(401, 1001, "인증 정보가 없거나 userId를 추출할 수 없습니다."); | ||
|
|
||
| private final Integer httpStatusCode; | ||
| private final Integer errorCode; | ||
| private final String message; | ||
| } |
52 changes: 34 additions & 18 deletions
52
src/main/java/com/wayble/server/auth/resolver/CurrentUserArgumentResolver.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,45 +1,61 @@ | ||
| package com.wayble.server.auth.resolver; | ||
|
|
||
| import com.wayble.server.auth.exception.AuthErrorCase; | ||
| import com.wayble.server.common.config.security.jwt.JwtTokenProvider; | ||
| import com.wayble.server.common.exception.ApplicationException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.bind.support.WebDataBinderFactory; | ||
| import org.springframework.web.context.request.NativeWebRequest; | ||
| import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
| import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CurrentUserArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
|
||
| private final JwtTokenProvider jwtTokenProvider; | ||
|
|
||
| @Override | ||
| public boolean supportsParameter(MethodParameter parameter) { | ||
| return parameter.hasParameterAnnotation(CurrentUser.class) | ||
| && Long.class.equals(parameter.getParameterType()); | ||
| } | ||
|
|
||
| @Override | ||
| public Object resolveArgument(MethodParameter parameter, | ||
| ModelAndViewContainer mav, | ||
| NativeWebRequest webRequest, | ||
| WebDataBinderFactory binderFactory) { | ||
| public Object resolveArgument( | ||
| MethodParameter parameter, | ||
| ModelAndViewContainer mav, | ||
| NativeWebRequest webRequest, | ||
| WebDataBinderFactory binderFactory | ||
| ) { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| if (auth == null) { | ||
| throw new IllegalStateException("인증 정보가 없습니다."); | ||
| } | ||
|
|
||
| Object principal = auth.getPrincipal(); | ||
| if (principal instanceof Long l) return l; | ||
| if (principal instanceof Integer i) return i.longValue(); | ||
| if (principal instanceof String s) { | ||
| try { | ||
| if (auth != null) { | ||
| Object principal = auth.getPrincipal(); | ||
| if (principal instanceof Long l) { return l; } | ||
| if (principal instanceof Integer i) { return i.longValue(); } | ||
| if (principal instanceof String s && s.chars().allMatch(Character::isDigit)) { | ||
| return Long.parseLong(s); | ||
| } catch (NumberFormatException ignored) {} | ||
| } | ||
| String name = auth.getName(); | ||
| if (name != null && name.chars().allMatch(Character::isDigit)) { | ||
| return Long.parseLong(name); | ||
| } | ||
| } | ||
| try { | ||
| return Long.parseLong(auth.getName()); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("userId를 추출할 수 없습니다.", e); | ||
|
|
||
| HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); | ||
| String authz = request != null ? request.getHeader("Authorization") : null; | ||
| if (StringUtils.hasText(authz) && authz.startsWith("Bearer ")) { | ||
| String token = authz.substring(7); | ||
| Long userId = jwtTokenProvider.getUserId(token); | ||
| if (userId != null) { return userId; } | ||
| } | ||
|
|
||
| throw new ApplicationException(AuthErrorCase.UNAUTHORIZED); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.