-
Notifications
You must be signed in to change notification settings - Fork 8
fix: 인증과 관련되어 리졸버와 컨트롤러 인자에서 발생한 이슈 해결 #201
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
nayonsoso
merged 4 commits into
solid-connection:main
from
nayonsoso:fix/200-controller-auth-parameter-require
Feb 14, 2025
Merged
Changes from all commits
Commits
Show all changes
4 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
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
1 change: 1 addition & 0 deletions
1
...om/example/solidconnection/custom/security/authentication/ExpiredTokenAuthentication.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,5 +1,6 @@ | ||
| package com.example.solidconnection.custom.security.authentication; | ||
|
|
||
| // todo: 사용되지 않음, 다른 PR에서 삭제하고 더 효율적인 구조를 고민해봐야 함 | ||
| public class ExpiredTokenAuthentication extends JwtAuthentication { | ||
|
Comment on lines
+3
to
4
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 좀 고민이 필요할 것 같아서.. 😭 일단 fix PR만 먼저 올립니다! |
||
|
|
||
| public ExpiredTokenAuthentication(String token) { | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.example.solidconnection.custom.resolver; | ||
|
|
||
|
|
||
| import com.example.solidconnection.custom.exception.CustomException; | ||
| import com.example.solidconnection.custom.security.authentication.SiteUserAuthentication; | ||
| import com.example.solidconnection.custom.security.userdetails.SiteUserDetails; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
|
|
@@ -11,11 +12,18 @@ | |
| import com.example.solidconnection.type.Role; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
|
|
||
| import static com.example.solidconnection.custom.exception.ErrorCode.AUTHENTICATION_FAILED; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatCode; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| @TestContainerSpringBootTest | ||
| @DisplayName("인증된 사용자 argument resolver 테스트") | ||
|
|
@@ -33,28 +41,58 @@ void setUp() { | |
| } | ||
|
|
||
| @Test | ||
| void security_context_에_저장된_인증된_사용자를_반환한다() throws Exception { | ||
| void security_context_에_저장된_인증된_사용자를_반환한다() { | ||
| // given | ||
| SiteUser siteUser = siteUserRepository.save(createSiteUser()); | ||
| SiteUserDetails userDetails = new SiteUserDetails(siteUser); | ||
| SiteUserAuthentication authentication = new SiteUserAuthentication("token", userDetails); | ||
| SiteUser siteUser = createAndSaveSiteUser(); | ||
| Authentication authentication = createAuthenticationWithUser(siteUser); | ||
| SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
|
||
| MethodParameter parameter = mock(MethodParameter.class); | ||
| AuthorizedUser authorizedUser = mock(AuthorizedUser.class); | ||
| given(parameter.getParameterAnnotation(AuthorizedUser.class)).willReturn(authorizedUser); | ||
| given(authorizedUser.required()).willReturn(false); | ||
|
|
||
| // when | ||
| SiteUser resolveSiteUser = (SiteUser) authorizedUserResolver.resolveArgument(null, null, null, null); | ||
| SiteUser resolveSiteUser = (SiteUser) authorizedUserResolver.resolveArgument(parameter, null, null, null); | ||
|
|
||
| // then | ||
| assertThat(resolveSiteUser).isEqualTo(siteUser); | ||
| } | ||
|
|
||
| @Test | ||
| void security_context_에_저장된_사용자가_없으면_null_을_반환한다() throws Exception { | ||
| // when, then | ||
| assertThat(authorizedUserResolver.resolveArgument(null, null, null, null)).isNull(); | ||
| @Nested | ||
| class security_context_에_저장된_사용자가_없는_경우 { | ||
|
|
||
| @Test | ||
| void required_가_true_이면_예외_응답을_반환한다() { | ||
| // given | ||
| MethodParameter parameter = mock(MethodParameter.class); | ||
| AuthorizedUser authorizedUser = mock(AuthorizedUser.class); | ||
| given(parameter.getParameterAnnotation(AuthorizedUser.class)).willReturn(authorizedUser); | ||
| given(authorizedUser.required()).willReturn(true); | ||
|
|
||
| // when, then | ||
| assertThatCode(() -> authorizedUserResolver.resolveArgument(parameter, null, null, null)) | ||
| .isInstanceOf(CustomException.class) | ||
| .hasMessageContaining(AUTHENTICATION_FAILED.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void required_가_false_이면_null_을_반환한다() { | ||
| // given | ||
| MethodParameter parameter = mock(MethodParameter.class); | ||
| AuthorizedUser authorizedUser = mock(AuthorizedUser.class); | ||
| given(parameter.getParameterAnnotation(AuthorizedUser.class)).willReturn(authorizedUser); | ||
| given(authorizedUser.required()).willReturn(false); | ||
|
|
||
| // when, then | ||
| assertThat( | ||
| authorizedUserResolver.resolveArgument(parameter, null, null, null) | ||
| ).isNull(); | ||
| } | ||
| } | ||
|
|
||
| private SiteUser createSiteUser() { | ||
| return new SiteUser( | ||
| private SiteUser createAndSaveSiteUser() { | ||
| SiteUser siteUser = new SiteUser( | ||
| "[email protected]", | ||
| "nickname", | ||
| "profileImageUrl", | ||
|
|
@@ -63,5 +101,11 @@ private SiteUser createSiteUser() { | |
| Role.MENTEE, | ||
| Gender.MALE | ||
| ); | ||
| return siteUserRepository.save(siteUser); | ||
| } | ||
|
|
||
| private SiteUserAuthentication createAuthenticationWithUser(SiteUser siteUser) { | ||
| SiteUserDetails userDetails = new SiteUserDetails(siteUser); | ||
| return new SiteUserAuthentication("token", userDetails); | ||
| } | ||
| } | ||
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.
JwtAuthetication 에서 credential 로 토큰 자체를 저장하게 해서
컨트롤러에서 인자로 쉽게 가져올 수 있었습니다~