JWT를 이용해서 로그인을 개발하고, 인증/인가 인터셉터를 개발하였는데 axios를 이용해서 요청을 보내니 이런 에러가 계속해서 발생했다. (postman을 사용했을 땐 아마 문제가 없었던 것으로 기억했다..)
분명히 CORS 설정을 미리 해줬는데 왜 이런현상이..??
알고보니 CORS 문제가 아니라 preflight의 헤더에 Authorization 토큰 값이 없기 때문에 계속 발생하는 것이었다..
해결법은 아주 간단했다.
구현한 인터셉터에서 preflight의 경우 토큰 검증을 하지 않으면 된다!
preflight는 OPTIONS 요청이므로, HTTP Method가 OPTIONS일 경우 인증되었다고 넘기면 된다.
// Preflight면 무시
if (HttpMethod.OPTIONS.name().equals(request.getMethod())){
return true;
}
HandlerInterceptor 구현한 인증 인터셉터에서 preHandle 함수 맨처음에 위 코드를 넣어주었더니 바로 문제없이 작동했다ㅎㅎ
'Study' 카테고리의 다른 글
EC2에 SonarQube 설치하고 Jenkins 연동해 코드 분석하기 (1) | 2023.10.02 |
---|