From 16cbd30ca75236f395b01316a635abdd537e8f52 Mon Sep 17 00:00:00 2001 From: PgmJun Date: Thu, 25 Jan 2024 13:56:04 +0900 Subject: [PATCH] =?UTF-8?q?bugfix:=20=ED=86=A0=ED=81=B0=20=EC=9E=AC?= =?UTF-8?q?=EB=B0=9C=EA=B8=89=20=EC=8B=9C,=20accessToken=20=EB=A7=8C?= =?UTF-8?q?=EB=A3=8C=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20#8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../petudio/common/auth/handler/AuthCheckHandler.java | 8 ++++---- .../java/com/nice/petudio/common/auth/jwt/JwtUtils.java | 9 ++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/nice/petudio/common/auth/handler/AuthCheckHandler.java b/src/main/java/com/nice/petudio/common/auth/handler/AuthCheckHandler.java index dc5076f..fe3f08f 100644 --- a/src/main/java/com/nice/petudio/common/auth/handler/AuthCheckHandler.java +++ b/src/main/java/com/nice/petudio/common/auth/handler/AuthCheckHandler.java @@ -26,7 +26,7 @@ public class AuthCheckHandler { public Long validateAuthority(HttpServletRequest request, List requiredRoles) { - String jwtAccessToken = getJwtAccessTokenFromHttpCookie(request); + String jwtAccessToken = getJwtAccessTokenFromHttpHeader(request); if (hasAuthority(jwtAccessToken, requiredRoles)) { return memberId; } @@ -34,7 +34,7 @@ public Long validateAuthority(HttpServletRequest request, List requi String.format("memberId(%d)의 접근 권한이 없어, 요청이 수행되지 않았습니다.", memberId)); } - private String getJwtAccessTokenFromHttpCookie(HttpServletRequest request) { + private String getJwtAccessTokenFromHttpHeader(HttpServletRequest request) { String bearerToken = request.getHeader("Authorization"); if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { return bearerToken.substring("Bearer ".length()); @@ -52,10 +52,10 @@ public boolean hasAuthority(String jwtAccessToken, List requiredRole return isRoleMatch(member, requiredRoles); } throw new ValidationException(ErrorCode.INVALID_JWT_TOKEN_EXCEPTION, - String.format("JWT AccessToken 내에 MemberId가 존재하지 않습니다.")); + "JWT AccessToken 내에 MemberId가 존재하지 않습니다."); } throw new UnAuthorizedException(ErrorCode.UNAUTHORIZED_JWT_EXCEPTION, - ErrorCode.UNAUTHORIZED_JWT_EXCEPTION.getMessage()); + String.format("입력받은 JWT 토큰이 유효하지 않습니다. (ACCESS_TOKEN: %s)", jwtAccessToken)); } private static boolean isRoleMatch(Member member, List requiredRoles) { diff --git a/src/main/java/com/nice/petudio/common/auth/jwt/JwtUtils.java b/src/main/java/com/nice/petudio/common/auth/jwt/JwtUtils.java index dbdba15..77aa69a 100644 --- a/src/main/java/com/nice/petudio/common/auth/jwt/JwtUtils.java +++ b/src/main/java/com/nice/petudio/common/auth/jwt/JwtUtils.java @@ -13,7 +13,6 @@ import io.jsonwebtoken.io.Decoders; import io.jsonwebtoken.io.DecodingException; import io.jsonwebtoken.security.Keys; -import io.jsonwebtoken.security.SignatureException; import java.util.Date; import java.util.List; import java.util.Optional; @@ -76,16 +75,12 @@ public Optional parseMemberId(String accessToken) { private Claims parseClaims(String accessToken) { try { - validateAccessToken(accessToken); return Jwts.parserBuilder().setSigningKey(secretKey).build() .parseClaimsJws(accessToken).getBody(); } catch (ExpiredJwtException exception) { return exception.getClaims(); - } - } - - private void validateAccessToken(String accessToken) { - if (!validateToken(accessToken)) { + } catch (io.jsonwebtoken.security.SecurityException | MalformedJwtException | DecodingException | + UnsupportedJwtException | IllegalArgumentException e) { throw new UnAuthorizedException(ErrorCode.UNAUTHORIZED_JWT_EXCEPTION, String.format("입력받은 JWT 토큰이 유효하지 않습니다. (ACCESS_TOKEN: %s)", accessToken)); }