Skip to content

Commit

Permalink
fix: jwtService 의 parse 시 Long 으로 변환이 되지 않는 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ghkdgus29 committed Apr 20, 2024
1 parent 6b7c9d5 commit 5e80943
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 63 deletions.
8 changes: 5 additions & 3 deletions src/main/java/site/youtogether/jwt/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ public String issue(Long userId, Duration expiry) {
.setIssuer(jwtProperties.getIssuer())
.setIssuedAt(now)
.setExpiration(expiredAt)
.claim("userId", userId)
.claim(USER_ID, userId)
.signWith(SignatureAlgorithm.HS256, jwtProperties.getSecretKey())
.compact();
}

public Claims parse(String authorizationHeader) {
public Long parse(String authorizationHeader) {
validationAuthorizationHeader(authorizationHeader);
String token = extract(authorizationHeader);
try {
return Jwts.parser()
Claims claims = Jwts.parser()
.setSigningKey(jwtProperties.getSecretKey())
.parseClaimsJws(token)
.getBody();

return claims.get(USER_ID, Long.class);
} catch (ExpiredJwtException e) {
throw new IllegalArgumentException("토큰 시간 만료");
} catch (UnsupportedJwtException | MalformedJwtException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package site.youtogether.util.interceptor;

import static site.youtogether.util.AppConstants.*;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -38,8 +35,8 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}

Claims claims = jwtService.parse(authorizationHeader);
if (userTrackingStorage.exists((Long)claims.get(USER_ID))) {
Long userId = jwtService.parse(authorizationHeader);
if (userTrackingStorage.exists(userId)) {
throw new SingleRoomParticipationViolationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -37,11 +36,11 @@ public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse res
ServletServerHttpRequest req = (ServletServerHttpRequest)request;
HttpServletRequest servletRequest = req.getServletRequest();

Claims claims = jwtService.parse(servletRequest.getParameter(HttpHeaders.AUTHORIZATION));
if (!userTrackingStorage.exists((Long)claims.get(USER_ID))) {
Long userId = jwtService.parse(servletRequest.getParameter(HttpHeaders.AUTHORIZATION));
if (!userTrackingStorage.exists(userId)) {
throw new InvalidTokenException();
}
attributes.put(USER_ID, Long.parseLong(claims.getId()));
attributes.put(USER_ID, userId);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import site.youtogether.exception.jwt.InvalidTokenException;
import site.youtogether.jwt.JwtService;
import site.youtogether.user.infrastructure.UserTrackingStorage;
import site.youtogether.util.AppConstants;

@Component
@RequiredArgsConstructor
Expand All @@ -37,8 +35,7 @@ public Long resolveArgument(MethodParameter parameter, ModelAndViewContainer mav
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);

assert request != null;
Claims claims = jwtService.parse(request.getHeader(HttpHeaders.AUTHORIZATION));
Long userId = (Long)claims.get(AppConstants.USER_ID);
Long userId = jwtService.parse(request.getHeader(HttpHeaders.AUTHORIZATION));

if (!userTrackingStorage.exists(userId)) {
throw new InvalidTokenException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.JsonFieldType;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import site.youtogether.RestDocsSupport;
import site.youtogether.exception.room.PasswordNotMatchException;
import site.youtogether.exception.room.RoomCapacityExceededException;
Expand Down Expand Up @@ -231,10 +229,8 @@ void createRoomFail_SingleRoomParticipantViolation() throws Exception {
RoomSettings roomSettings = RoomSettings.builder().capacity(10).title("재밌는 쇼츠 같이 보기").password(null).build();
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxMjM0NSJ9.XJHPNpgWMty0iKr1FQKCBeOapvlqk1RjcPQUzT2dFlA";

Claims claims = Jwts.claims();
claims.put(USER_ID, 1L);
given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);

given(userTrackingStorage.exists(anyLong())).willReturn(true);

Expand Down Expand Up @@ -382,10 +378,8 @@ void enterRoomFail() throws Exception {
String roomCode = "1e7050f7d7";
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxMjM0NSJ9.XJHPNpgWMty0iKr1FQKCBeOapvlqk1RjcPQUzT2dFlA";

Claims claims = Jwts.claims();
claims.put(USER_ID, 1L);
given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);

given(userTrackingStorage.exists(anyLong()))
.willReturn(true);
Expand Down Expand Up @@ -601,10 +595,8 @@ void updateRoomTitle() throws Exception {
given(roomService.changeRoomTitle(eq(userId), eq(roomCode), eq(updateTitle)))
.willReturn(new UpdatedRoomTitle(roomCode, updateTitle));

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);
given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(userId);

given(userTrackingStorage.exists(anyLong()))
.willReturn(true);
Expand Down Expand Up @@ -649,10 +641,8 @@ void updateRoomTitleFailForm() throws Exception {
String updateTitle = " ";
RoomTitleChangeForm form = new RoomTitleChangeForm(roomCode, updateTitle);

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);
given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);

given(userTrackingStorage.exists(anyLong()))
.willReturn(true);
Expand Down Expand Up @@ -701,11 +691,9 @@ void updateRoomTitleFailNotHost() throws Exception {
given(roomService.changeRoomTitle(eq(userId), eq(roomCode), eq(updateTitle)))
.willThrow(new ChangeRoomTitleDeniedException());

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxMjM0NSJ9.XJHPNpgWMty0iKr1FQKCBeOapvlqk1RjcPQUzT2dFlA";
given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(userId);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.JsonFieldType;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import site.youtogether.RestDocsSupport;
import site.youtogether.exception.ErrorType;
import site.youtogether.exception.user.HigherOrEqualRoleChangeException;
Expand Down Expand Up @@ -45,11 +43,8 @@ void updateNickname() throws Exception {
given(userService.updateUserNickname(eq(userId), eq(updateNickname), eq(roomCode)))
.willReturn(userInfo);

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down Expand Up @@ -99,11 +94,8 @@ void updateNicknameFail() throws Exception {
given(userService.updateUserNickname(eq(userId), eq(updateNickname), eq(roomCode)))
.willReturn(userInfo);

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down Expand Up @@ -150,11 +142,8 @@ void changeRole() throws Exception {
given(userService.changeUserRole(eq(hostId), any(UserRoleChangeForm.class)))
.willReturn(userInfo);

Claims claims = Jwts.claims();
claims.put(USER_ID, hostId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down Expand Up @@ -202,11 +191,8 @@ void selfRoleChangeFail() throws Exception {
given(userService.changeUserRole(eq(hostId), any(UserRoleChangeForm.class)))
.willThrow(new SelfRoleChangeException());

Claims claims = Jwts.claims();
claims.put(USER_ID, hostId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down Expand Up @@ -255,11 +241,8 @@ void HigherOrEqualUserRoleChangeFail() throws Exception {
given(userService.changeUserRole(eq(userId), any(UserRoleChangeForm.class)))
.willThrow(new HigherOrEqualRoleUserChangeException());

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down Expand Up @@ -308,11 +291,8 @@ void HigherOrEqualRoleChangeFail() throws Exception {
given(userService.changeUserRole(eq(userId), any(UserRoleChangeForm.class)))
.willThrow(new HigherOrEqualRoleChangeException());

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down Expand Up @@ -361,11 +341,8 @@ void notManageableUserRoleChangeFail() throws Exception {
given(userService.changeUserRole(eq(userId), any(UserRoleChangeForm.class)))
.willThrow(new NotManageableUserException());

Claims claims = Jwts.claims();
claims.put(USER_ID, userId);

given(jwtService.parse(anyString()))
.willReturn(claims);
.willReturn(1L);
given(userTrackingStorage.exists(anyLong()))
.willReturn(true);

Expand Down

0 comments on commit 5e80943

Please sign in to comment.