Skip to content

Commit

Permalink
Merge pull request #147 from twenty-three-23/feature/TT-210-token-to-…
Browse files Browse the repository at this point in the history
…header

TT-210 token을 header의 Authorization에서 조회
  • Loading branch information
snacktime81 authored Jul 8, 2024
2 parents 7118585 + 9d1ea20 commit f57e3bb
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.twentythree.peech.auth.dto.LoginUserId;
import com.twentythree.peech.auth.dto.UserIdDTO;
import com.twentythree.peech.common.exception.UserAlreadyExistException;
import com.twentythree.peech.common.utils.JWTUtils;
import io.jsonwebtoken.JwtException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -30,23 +30,26 @@ public boolean supportsParameter(MethodParameter parameter) {
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
try {

final String BEARER = "Bearer ";

HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

Cookie[] cookies = request.getCookies();
if (cookies == null) {
throw new IllegalArgumentException("로그인을 다시 해주세요");
String token = request.getHeader("Authorization");

if (token.isEmpty()) {
throw new UserAlreadyExistException("로그인을 다시 해주세요");
}

Long userId = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals("LoginToken")) {
String token = cookie.getValue();
String credential;

userId = Long.parseLong(jwtUtils.parseJWT(token).getPayload().get("userId").toString());
}
if (token.startsWith(BEARER)) {
credential = token.substring(BEARER.length());
} else {
throw new IllegalArgumentException("token의 type이 올바르지 않습니다.");
}

Long userId = Long.parseLong(jwtUtils.parseJWT(credential).getPayload().get("userId").toString());

if (userId == null) {
throw new IllegalArgumentException("cookie의 userId가 잘 못 되었습니다");
}
Expand Down

0 comments on commit f57e3bb

Please sign in to comment.