-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: 현재 로그인한 멤버 조회하는 유틸리티 구현 #41
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 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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/gdschongik/gdsc/global/util/MemberUtil.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,41 @@ | ||
package com.gdschongik.gdsc.global.util; | ||
|
||
import com.gdschongik.gdsc.domain.member.dao.MemberRepository; | ||
import com.gdschongik.gdsc.domain.member.domain.Member; | ||
import com.gdschongik.gdsc.global.exception.CustomException; | ||
import com.gdschongik.gdsc.global.exception.ErrorCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MemberUtil { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public Long getCurrentMemberId() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
validateAuthenticationNotNull(authentication); | ||
|
||
try { | ||
return Long.parseLong(authentication.getName()); | ||
} catch (NumberFormatException e) { | ||
throw new CustomException(ErrorCode.AUTH_NOT_PARSABLE); | ||
} | ||
} | ||
|
||
private void validateAuthenticationNotNull(Authentication authentication) { | ||
if (authentication == null) { | ||
throw new CustomException(ErrorCode.AUTH_NOT_EXIST); | ||
} | ||
} | ||
|
||
public Member getCurrentMember() { | ||
return memberRepository | ||
.findById(getCurrentMemberId()) | ||
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); | ||
} | ||
} |
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.
아래 한 줄에서 발생할 수 있는 예외들은 어떤 것들이 있을까요?
따로 검증하지 않고, Exception으로 한번에 처리하는 이유는 예상하지 못한 예외에 대비하기 위함일까요?
아니면, 여러 예외 상황을 한번에 처리하기 위함일까요?
제가 시큐리티를 잘 몰라서 그런지 따로 검증하지 않으니,
저 한줄에서 어떤 예외가 발생할 수 있는지 궁금한데, 추측하기 어렵네요.
(예외도 Exception으로 되어 있어서)
제가 생각나는 경우는 아래 두가지 정도인데, 재현님이 생각하는 경우는 다르거나 더 있을 것 같아요
읽는 사람도 어떤 예외가 발생할 수도 있는지 예상해볼 수 있도록,
먼저 검증해보는 것은 어떤지 의견 여쭈어 봅니다.
예시
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.
Authentication
이 null인 경우 -> 서비스 레이어에서는 내부적으로MemberUtil
을 사용하는데, 이 서비스 레이어를 테스트할 때 실수로SecurityContext
에Authentication
을 set 해주지 않은 경우 NPE가 발생할 수도 있습니다.파싱 실패하는 경우 ->
AnonymouseAuthentication
이 set 된 경우getName()
이memberId
가 아닌anonymousUser
라는 String을 리턴하기 때문에 파싱에 실패합니다.말씀하신대로 두 가지 케이스르 분리할 수 있을 것 같습니다. 좋은 의견 감사해요~