Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend HTTP 요청 시, Cors Preflight Error로 인해 API 연동이 실패하는 현상 #10

Open
daily1313 opened this issue Dec 6, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@daily1313
Copy link
Collaborator

Summary

  • 로그인, 회원가입 로직을 제외하고, Client의 HTTP 요청시 Cors Preflight Error 발생

Describe

  • Error Log
Access to XMLHttpRequest at 'http://(deploy ip)/api/allergy' from origin 
'http://localhost:3000' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

To do

  • OPTIONS 메서드로 요청이 들어올 때, CorsFilter 내의 doFilterInternal 메서드 내에서 HTTP_OK를 반환하도록 로직 수정
public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
                                    final FilterChain chain) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, PATCH");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type, Authorization");

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            return;
        }

        chain.doFilter(request, response);
    }
}

Etc

@daily1313 daily1313 added the bug Something isn't working label Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants