Skip to content

Commit

Permalink
refactor(ArgumentResolver): 쿠키에서 토큰을 추출하도록 코드 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
seokmyungham committed Jul 31, 2024
1 parent 58ca5a2 commit d10d68b
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package kr.momo.controller.auth;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;
import kr.momo.exception.MomoException;
import kr.momo.exception.code.AuthErrorCode;
import kr.momo.service.auth.JwtManager;
Expand All @@ -16,8 +19,7 @@
@RequiredArgsConstructor
public class AuthArgumentResolver implements HandlerMethodArgumentResolver {

private static final String AUTHORIZATION = "Authorization";
private static final String BEARER = "Bearer ";
private static final String ACCESS_TOKEN = "ACCESS_TOKEN";

private final JwtManager jwtManager;

Expand All @@ -34,15 +36,26 @@ public Long resolveArgument(
@NonNull NativeWebRequest webRequest,
WebDataBinderFactory binderFactory
) {
String token = getToken(webRequest);
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
Cookie[] cookies = request.getCookies();
String token = getCookieValue(cookies);

if (token == null) {
throw new MomoException(AuthErrorCode.NOT_FOUND_TOKEN);
}

return jwtManager.extract(token);
}

private String getToken(NativeWebRequest webRequest) {
String header = webRequest.getHeader(AUTHORIZATION);
if (header == null) {
throw new MomoException(AuthErrorCode.NOT_FOUND_TOKEN);
private String getCookieValue(Cookie[] cookies) {
if (cookies == null) {
return null;
}
return header.replaceFirst(BEARER, "");

return Arrays.stream(cookies)
.filter(cookie -> ACCESS_TOKEN.equals(cookie.getName()))
.map(Cookie::getValue)
.findFirst()
.orElse(null);
}
}

0 comments on commit d10d68b

Please sign in to comment.