Skip to content

Commit

Permalink
fix: JWT 재발급 오류 해결 (#138)
Browse files Browse the repository at this point in the history
* fix: 토큰 재발급 에러 수정 (#132)

* fix: 토큰 재발급 에러 수정 (#132)

* test: 토큰 재발급 에러 수정에 따른 테스트 수정 (#132)

* test: SecurityConfig permitAll url 중복 제거 (#132)
  • Loading branch information
choidongkuen authored Jan 22, 2024
1 parent a306b8e commit 05dcd4c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/main/java/net/teumteum/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ private void checkRefreshTokenMatch(User user, String refreshToken) {


private TokenResponse issueNewToken(User user) {
return new TokenResponse(jwtService.createAccessToken(user.getOauth().getOauthId()),
jwtService.createRefreshToken());
return jwtService.createServiceToken(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.teumteum.core.property.JwtProperty;
import net.teumteum.user.domain.User;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.oauth2.jwt.JwtException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

Expand Down Expand Up @@ -60,12 +59,14 @@ public String extractRefreshToken(HttpServletRequest request) {

public Long getUserIdFromToken(String token) {
try {
return Long.valueOf(getClaims(token).get("id", String.class));
} catch (Exception exception) {
throw new JwtException("Access Token is not valid");
Claims claims = getClaims(token);
return claims.get("id", Long.class);
} catch (ExpiredJwtException exception) {
return Long.valueOf(exception.getClaims().get("id").toString());
}
}


public TokenResponse createServiceToken(User users) {
String accessToken = createAccessToken(users.getId().toString());
String refreshToken = createRefreshToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
@RequiredArgsConstructor
public class RedisService {

private final RedisTemplate<String, Object> redisTemplate;
private final RedisTemplate<String, String> redisTemplate;

public String getData(String key) {
return (String) redisTemplate.opsForValue().get(key);
return redisTemplate.opsForValue().get(key);
}

public void setData(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.teumteum.unit.auth.service;

import static net.teumteum.core.security.Authenticated.네이버;
import static net.teumteum.unit.auth.common.SecurityValue.INVALID_ACCESS_TOKEN;
import static net.teumteum.unit.auth.common.SecurityValue.VALID_REFRESH_TOKEN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -55,17 +57,16 @@ void Return_new_jwt_if_access_and_refresh_is_exist() {

HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);

given(jwtService.extractAccessToken(any(HttpServletRequest.class))).willReturn("access token");
given(jwtService.extractAccessToken(any(HttpServletRequest.class))).willReturn(INVALID_ACCESS_TOKEN);

given(jwtService.extractRefreshToken(any(HttpServletRequest.class))).willReturn("refresh token");
given(jwtService.extractRefreshToken(any(HttpServletRequest.class))).willReturn(VALID_REFRESH_TOKEN);

given(jwtService.getUserIdFromToken(anyString())).willReturn(1L);

given(jwtService.createAccessToken(anyString())).willReturn("new access token");

given(jwtService.createRefreshToken()).willReturn("new refresh token");
given(jwtService.createServiceToken(any(User.class))).willReturn(
TokenResponse.builder().accessToken("access token").refreshToken("refresh token").build());

given(redisService.getData(anyString())).willReturn("refresh token");
given(redisService.getData(anyString())).willReturn(VALID_REFRESH_TOKEN);

given(userConnector.findUserById(anyLong())).willReturn(user);

Expand All @@ -76,8 +77,8 @@ void Return_new_jwt_if_access_and_refresh_is_exist() {

// then
assertThat(response).isNotNull();
assertThat(response.getAccessToken()).isEqualTo("new access token");
assertThat(response.getRefreshToken()).isEqualTo("new refresh token");
assertThat(response.getAccessToken()).isEqualTo("access token");
assertThat(response.getRefreshToken()).isEqualTo("refresh token");
verify(userConnector, times(1)).findUserById(anyLong());
verify(jwtService, times(1)).validateToken(any());
}
Expand Down

0 comments on commit 05dcd4c

Please sign in to comment.