-
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(#45): redis 모듈 추가 #49
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
15ac500
feat(#45): refresh 임시 구현 - Entity 아직 사용 X 수정 예정
jusung-c bbc3b20
(#45)-RefreshToken 엔티티 & RefreshTokenRepository 구현
jusung-c 855b88b
Merge branch 'refactor(#46)-refreshToken-state-redis' into feat(#45)-…
jusung-c 1343c08
refactor(#45): Refresh Token 구현 수정
jusung-c 3123204
refactor(#46): generateAccessToken 메소드명 변경에 따른 수정
jusung-c 99e38a2
refactor(#45): generateAccessToken 메소드명 변경에 따른 수정
jusung-c 4dcd0bb
test(#45): RefreshTokenRepository 테스트
jusung-c ec944db
feat(#45): Logout 구현 - Logout 시 Redis에서 RefreshToken 삭제
jusung-c c9a0b67
test(#45): RefreshTokenService 테스트
jusung-c 0ac592c
Merge remote-tracking branch 'upstream/feat(#45)-heachi-domain-redis'…
jusung-c e76e51f
Merge branch 'dev' into feat(#45)-heachi-domain-redis
ghdcksgml1 c4c5801
refactor(#45): refersh 예외 JwtException으로 처리
jusung-c cc51c39
Merge remote-tracking branch 'upstream/feat(#45)-heachi-domain-redis'…
jusung-c b3a4c77
refactor(#45):
jusung-c 56b0c57
refactor(#45): 빌드 에러 수정
jusung-c 6504974
refactor(#45): 헤더에서 토큰 추출시 생길 수 있는 OutOfBound 에러 방지
jusung-c da2175b
feat(#45): AccessToken 재발급 일부 구현
jusung-c 713cda3
refactor(#45): AccessToken 재발급 수정 & 테스트
jusung-c 6a0ceee
refactor(#45): AccessToken 재발급 수정 & 테스트
jusung-c 27c64a8
refactor(#45): 피드백 부분 수정
jusung-c 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
16 changes: 16 additions & 0 deletions
16
...c/main/java/com/heachi/auth/api/controller/token/response/ReissueAccessTokenResponse.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,16 @@ | ||
package com.heachi.auth.api.controller.token.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ReissueAccessTokenResponse { | ||
private String accessToken; | ||
private String refreshToken; | ||
|
||
@Builder | ||
public ReissueAccessTokenResponse(String accessToken, String refreshToken) { | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti | |
|
||
return http.build(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
package com.heachi.auth.api.service.token; | ||
|
||
import com.heachi.admin.common.exception.ExceptionMessage; | ||
import com.heachi.auth.api.controller.auth.AuthController; | ||
import com.heachi.auth.api.controller.token.response.ReissueAccessTokenResponse; | ||
import com.heachi.auth.api.service.auth.AuthService; | ||
import com.heachi.auth.api.service.jwt.JwtService; | ||
import com.heachi.auth.api.service.oauth.OAuthService; | ||
import com.heachi.mysql.define.user.User; | ||
import com.heachi.mysql.define.user.constant.UserRole; | ||
import com.heachi.mysql.define.user.repository.UserRepository; | ||
import com.heachi.redis.define.refreshToken.RefreshToken; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.HashMap; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
@@ -31,18 +39,23 @@ public class AccessTokenReissueTest { | |
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private RefreshTokenService refreshTokenService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private JwtService jwtService; | ||
|
||
@Autowired | ||
private RefreshTokenService refreshTokenService; | ||
@AfterEach | ||
void tearDown() { | ||
userRepository.deleteAllInBatch(); | ||
} | ||
|
||
@Test | ||
@DisplayName("AccessToken 만료시 재발급 테스트") | ||
void redisRefreshTokenGenerate() throws Exception { | ||
@DisplayName("AccessToken 재발급 성공 테스트") | ||
void reissueSuccess() throws Exception { | ||
// given | ||
User user = User.builder() | ||
.name("김민수") | ||
|
@@ -56,22 +69,62 @@ void redisRefreshTokenGenerate() throws Exception { | |
map.put("role", savedUser.getRole().name()); | ||
map.put("name", savedUser.getName()); | ||
map.put("profileImageUrl", savedUser.getProfileImageUrl()); | ||
|
||
String expiredAccessToken = jwtService.generateExpiredAccessToken(map, savedUser); | ||
String accessToken = jwtService.generateAccessToken(map, savedUser); | ||
String refreshToken = jwtService.generateRefreshToken(map, savedUser); | ||
|
||
refreshTokenService.saveRefreshToken(RefreshToken.builder().refreshToken(refreshToken).email(user.getEmail()).build()); | ||
|
||
|
||
// when | ||
mockMvc.perform(get("/auth/info") | ||
mockMvc.perform(post("/auth/reissue") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", "Bearer " + expiredAccessToken + " " + refreshToken)) | ||
.header("Authorization", "Bearer " + accessToken + " " + refreshToken)) | ||
|
||
// then | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.resCode").value(200)) | ||
.andExpect(jsonPath("$.resObj.role").value("USER")) | ||
.andExpect(jsonPath("$.resObj.name").value("김민수")) | ||
.andExpect(jsonPath("$.resObj.email").value("[email protected]")) | ||
.andExpect(jsonPath("$.resObj.profileImageUrl").value("https://google.com")); | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@DisplayName("잘못된 헤더로 재발급 요청시 JWT_INVALID_HEADER 예외가 터져야 한다.") | ||
void reissueFailWithInvalidHeader() throws Exception { | ||
mockMvc.perform(post("/auth/reissue") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", "aa bb cc dd")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.resCode").value(400)) | ||
.andExpect(jsonPath("$.resMsg").value(ExceptionMessage.JWT_INVALID_HEADER.getText())); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 리프레시 토큰으로 재발급 요청시 JWT_NOT_EXIST_RTK 예외가 터져야 한다.") | ||
void reissueFailWithNotExistRtk() throws Exception { | ||
|
||
// given | ||
User user = User.builder() | ||
.name("김민수") | ||
.role(UserRole.USER) | ||
.email("[email protected]") | ||
.profileImageUrl("https://google.com") | ||
.build(); | ||
User savedUser = userRepository.save(user); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("role", savedUser.getRole().name()); | ||
map.put("name", savedUser.getName()); | ||
map.put("profileImageUrl", savedUser.getProfileImageUrl()); | ||
String accessToken = jwtService.generateAccessToken(map, savedUser); | ||
String refreshToken = jwtService.generateRefreshToken(map, savedUser); | ||
|
||
// 레디스 저장 부분 주석처리 | ||
// refreshTokenService.saveRefreshToken(RefreshToken.builder().refreshToken(refreshToken).email(user.getEmail()).build()); | ||
|
||
mockMvc.perform(post("/auth/reissue") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", "aa " + accessToken + " " + refreshToken)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.resCode").value(400)) | ||
.andExpect(jsonPath("$.resMsg").value(ExceptionMessage.JWT_NOT_EXIST_RTK.getText())); | ||
} | ||
} |
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.
기존에 모든 Optional이 Null일때 orElseThrow를 썼는데 통일하는게 좋아보입니다. ㅎ.ㅎ