Trouble Shooting

cors μ—λŸ¬ ν•΄κ²°κΈ°

sebarii 2024. 7. 30. 01:58

1. https://λ„λ©”μΈμ£Όμ†Œ 치면 http://ipμ£Όμ†Œλ‘œ μ΄λ™λ˜λŠ” 문제 λ•Œλ¬Έμ— 계속 였λ₯˜λ‚¨

-->  mixed content 였λ₯˜, https μ—μ„œ http둜 μš”μ²­μ„ 보내면 λ°œμƒν•˜λŠ” 였λ₯˜μΈλ° κ΅¬λ§€ν•œ 도메인과 ssl μΈμ¦μ„œ μ μš©κΉŒμ§€ λ‹€ 해쀬고 ν”„λ‘μ‹œ μ„€μ •κΉŒμ§€ ν–ˆλŠ”λ° 더 이상 뭘 μˆ˜μ •ν•΄μ•Ό 될 μ§€ 감도 μ•ˆ 옴

λ°±μ—”λ“œμ—μ„œλŠ” 잘λͺ»λœ 뢀뢄이 μ—†μ—ˆκ³  ν”„λ‘ νŠΈμ—μ„œ 뭘 κ³ μ³€λŠ” μ§€ κ²°κ΅­μ—” 해결이 됨

--> https://λ„λ©”μΈμ£Όμ†Œ/api 둜 μš”μ²­λ³΄λ‚΄λ©΄ λ°±μ—”λ“œ μ„œλ²„λ‘œ 잘 λ„˜μ–΄κ°

 

2. κ·Έλž¬λ”λ‹ˆλ§Œ 이제 corsμ—λŸ¬ λ°œμƒ

--> spring security μ½”λ“œμ—μ„œλ„ cors μ„€μ • λΆ„λͺ… λ‹€ ν•΄μ€¬λŠ”λ° 였λ₯˜λ‚¨.. 

--> κ²€μƒ‰ν•΄λ³΄λ‹ˆ nginx μ—μ„œλ„ 섀정을 ν•΄μ€˜μ•Ό 됐음

--> nμ‹œκ°„λ™μ•ˆ μ‚½μ§ˆν•˜λ©΄μ„œ conf파일 μˆ˜μ •ν•˜κ³  μž¬μ‹€ν–‰ν•˜κ³  λ¬΄ν•œλ°˜λ³΅ ν–ˆλŠ”λ°λ„ 였λ₯˜ 해결이 μ•ˆ 됨 .. ν•˜.. 뭐지?

--> μ΄μœ λŠ” ν”„λ‘μ‹œ 섀정이 nginxκ°€ μ•„λ‹ˆλΌ vercel을 ν†΅ν•΄μ„œ 되고 μžˆμ—ˆμŒ γ…‹γ…Žγ…‹γ…Ž 

λΆ„λͺ… κ°€λΉ„μ•„μ—μ„œ dns μ„€μ •μœΌλ‘œ 우리 ipμ£Όμ†Œλ₯Ό 섀정해놨고 그에 맞게 nginx 섀정도 ν–ˆλŠ”λ° 이후에 ν”„λ‘ νŠΈ κ°œλ°œμžλΆ„μ΄ dns 섀정을 vercel μ£Όμ†Œλ‘œ λ°”κΏ”λ†“μœΌμ…¨λ˜ 것 ...

어쩐지 nginx.conf 파일 아무리 μˆ˜μ •ν•΄λ„ λ°”λ€ŒλŠ” 게 없더라 ...

μ € Remote  Address μ£Όμ†Œμ— μžˆλŠ” ipμ£Όμ†ŒλŠ” λ­”κ°€ μ‹Άμ—ˆκ³  vercel  μ£Όμ†ŒμΈκ°€ μ‹Άμ–΄μ„œ

nslookup 을 ν•΄μ€¬λ”λ‹ˆ μ—­μ‹œ 버셀 μ£Όμ†Œμ˜€κ΅°

 

밑에 μ„œλ²„κ°€ vercel 둜 뜨고 Remote Address도 vercel ipμ£Όμ†Œλ‘œ λœ¨λŠ” 것을 확인 ,,

 

cors μ„€μ • μ•ˆ λ¨ΉλŠ” 것에 λŒ€ν•΄ μ˜μ‹¬κ°”λ˜ λΆ€λΆ„

 

κ·Έλž˜μ„œ κ°€λΉ„μ•„μ—μ„œ dns 섀정을 λ‹€μ‹œ μ„œλ²„ ipμ£Όμ†Œλ‘œ λ°”κΏ”λ†“μŒ

 

그러고 λ°± μ„œλ²„λ‘œ api μš”μ²­ ν–ˆλ”λ‹ˆ κ·Έλž˜λ„ corsμ—λŸ¬ λ°œμƒ (ν•˜ 제발 ...)

 

3. λ°œμƒν•œ cors μ—λŸ¬

Access to XMLHttpRequest at 'https://λ„λ©”μΈμ£Όμ†Œ/api/main/list' from origin 'https://ν”„λ‘ νŠΈμ£Όμ†Œ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

-->아무리 nginxμ—μ„œ cors 섀정을 μˆ˜μ •ν•˜κ³  또 μˆ˜μ •ν•΄λ„ 계속  corsμ—λŸ¬ ....  λŒ€μ‹  희망점을 발견 ν•œ 게 cors μ—λŸ¬λŠ” λœ¨μ§€λ§Œ λ°±μ—”λ“œ μ„œλ²„ 응닡 responseλŠ” 200 ok κ°€ λ–΄λ‹€,, 희망을 μ°Ύμ•˜μ–΄ (κ·Έ μ „μ—λŠ” 308μ—λŸ¬μΈκ°€ μ΄μƒν•œ μ—λŸ¬λœΈ)

 

계속 conf 파일 μˆ˜μ •ν•˜κ³  μž¬μ‹€ν–‰ν•˜κ³  λ¬΄ν•œ 반볡 ν•˜λ‹€κ°€ ν•΄κ²°ν•œ 방법은 

 map $http_origin $allowed_origin {
                default "";
                "http://localhost:5173/" $http_origin;
                "https://도메인1/" $http_origin;
                "https://도메인2/" $http_origin;
        }

 

add_header 'Access-Control-Allow-Origin' $allowed_origin always;

 

이런 μ‹μœΌλ‘œ μ—¬λŸ¬ 도메인에 λŒ€ν•΄ cross origin 섀정을 ν•΄μ£Όκ³  μžˆμ—ˆλŠ”λ° 이게 λ­”κ°€ μ œλŒ€λ‘œ 적용이 μ•ˆ λ˜λŠ” λŠλ‚Œμ΄μ—ˆμŒ 

 

add_header 'Access-Control-Allow-Origin' 'https://배포된 ν”„λ‘ νŠΈ vercelμ£Όμ†Œ/' always;

 

걍 μ΄λ ‡κ²Œ λƒ…λ‹€ 박아버림

(μ—¬λŸ¬ 도메인 ν—ˆμš©ν•˜λŠ” 건 더 μ°Ύμ•„λ³΄λ©΄μ„œ 계속 μˆ˜μ •ν•΄λ³΄κ³  ν…ŒμŠ€νŠΈ 해봐야할듯)

 

κ·Έλž¬λ”λ‹ˆ  μ•„κΉŒλž‘ λ‹€λ₯Έ cors μ—λŸ¬ λ°œμƒ

 

4. λ°œμƒν•œ cors μ—λŸ¬

Access to XMLHttpRequest at 'https://μš”μ²­ν•œ λ°±μ—”λ“œ api μ£Όμ†Œ' from origin 'https://vercel ν”„λ‘ νŠΈ μ£Όμ†Œ' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://
vercel ν”„λ‘ νŠΈ μ£Όμ†Œ/' that is not equal to the supplied origin.

 

gptν•œν…Œ 도움 μš”μ²­ν•¨

이 였λ₯˜λŠ”
Access-Control-Allow-Origin
헀더에 μ„€μ •λœ 값이 μ‹€μ œ μš”μ²­μ˜ Origin 헀더와 μΌμΉ˜ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ°œμƒν•©λ‹ˆλ‹€. κ΅¬μ²΄μ μœΌλ‘œλŠ”
https://vercel ν”„λ‘ νŠΈ μ£Όμ†Œ/
(μŠ¬λž˜μ‹œ 포함)와
https://vercel ν”„λ‘ νŠΈ μ£Όμ†Œ
(μŠ¬λž˜μ‹œ μ—†μŒ) κ°„μ˜ λΆˆμΌμΉ˜κ°€ 문제λ₯Ό μΌμœΌν‚€λŠ” κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€.

 

μ•Œκ³ λ΄€λ”λ‹ˆ ν—ˆμš©ν•  도메인 μ μœΌλ©΄μ„œ 뒀에 /(μŠ¬λž˜μ‹œ) λ₯Ό ν•˜λ‚˜ 더 뢙인 문제 ,,,

add_header 'Access-Control-Allow-Origin' 'https://배포된 ν”„λ‘ νŠΈ vercelμ£Όμ†Œ/' always;

 μ΄κ±°μ—μ„œ

add_header 'Access-Control-Allow-Origin' 'https://배포된 ν”„λ‘ νŠΈ vercelμ£Όμ†Œ' always;

이걸둜 μˆ˜μ • ν•΄μ€Œ

 

κ·Έλž¬λ”λ‹ˆ.... 또 λ‹€λ₯Έ cors μ—λŸ¬ λ°œμƒ γ…‹γ…‹γ…‹ ν•˜ μ œλ°œμš”

 

5. λ°œμƒν•œ cors μ—λŸ¬

auth:1 Access to XMLHttpRequest at 'https://μš”μ²­ν•œ λ°±μ„œλ²„ api μ£Όμ†Œ' from origin 'https://vercel ν”„λ‘ νŠΈ μ£Όμ†Œ' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is 'true, true' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

 

gptν•œν…Œ 도움 μš”μ²­ν•¨ 


이 였λ₯˜λŠ” Access-Control-Allow-Credentials ν—€λ”μ˜ 값이 잘λͺ» μ„€μ •λ˜μ—ˆκΈ° λ•Œλ¬Έμ— λ°œμƒν•©λ‹ˆλ‹€. Access-Control-Allow-Credentials ν—€λ”μ˜ 값은 λ°˜λ“œμ‹œ 'true'μ—¬μ•Ό ν•˜λ©°, λ‹€λ₯Έ 값이 ν¬ν•¨λ˜μ–΄μ„œλŠ” μ•ˆ λ©λ‹ˆλ‹€.
Access-Control-Allow-Credentials 헀더가 잘λͺ»λœ 값을 κ°€μ§€κ³  μžˆλŠ” μ΄μœ λŠ” Nginx μ„€μ •μ΄λ‚˜ λ°±μ—”λ“œ μ„œλ²„ μ„€μ •μ—μ„œ μ€‘λ³΅μœΌλ‘œ 이 헀더λ₯Ό μΆ”κ°€ν•˜κ³  있기 λ•Œλ¬ΈμΌ 수 μžˆμŠ΅λ‹ˆλ‹€. 이 문제λ₯Ό ν•΄κ²°ν•˜λ €λ©΄ Nginx 섀정을 κ²€ν† ν•˜κ³  μˆ˜μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.

 

μ‹€μ œλ‘œ 응닡값을 λ³΄λ‹ˆ Access-Control-Allow-Credentials : true μ—¬κΈ°κ°€ λ‘λ²ˆ 찍히고 μžˆμ—ˆμŒ

λ­”κ°€ μ˜μ‹¬κ°€λŠ” 뢀뢄이

λ°±μ—”λ“œ μ½”λ“œ μžμ²΄μ—μ„œ Spring Securityλ₯Ό 톡해 Access-Control-Allow-Credentials 섀정을 ν•΄μ£ΌλŠ”λ° nginxμ—μ„œ ν•œλ²ˆ 더 μ„€μ •ν•˜λ €κ³  ν•΄μ„œ 쀑볡 적용이 λ˜λŠ” 건가 μ‹Άμ—ˆμŒ

 

    // CORS μ„€μ • λ©”μ„œλ“œ
    private CorsConfigurationSource corsConfigurationSource(){
        return request -> {
            CorsConfiguration config = new CorsConfiguration();
            //config.setAllowedMethods(List.of(SwaggerPatterns));
            config.setAllowedOrigins(allowedOriginPaths);
            config.setAllowedMethods(List.of(allowedMethods));
            config.setAllowedHeaders(List.of(HttpHeaders.AUTHORIZATION, HttpHeaders.CONTENT_TYPE, HttpHeaders.SET_COOKIE, HttpHeaders.ACCEPT, HttpHeaders.ACCEPT_LANGUAGE, HttpHeaders.CONTENT_LANGUAGE));
            config.setAllowCredentials(true);
            config.setMaxAge(3600L);
            return config;
        };
    }

<security μ—μ„œ cors μ„€μ •ν•΄μ£ΌλŠ” λΆ€λΆ„>

 

 

κ·Έλž˜μ„œ 일단 nginx에 μ μš©λ˜μ–΄ 있던 Access-Control-Allow-Credentials 뢀뢄을 μ£Όμ„μ²˜λ¦¬ ν–ˆμŒ

#       add_header 'Access-Control-Allow-Credentials' 'true' always;

conf νŒŒμΌμ€ #ν•˜λ‚˜λ§Œ λΆ™μ—¬μ£Όλ©΄ 주석이 λ©λ‹ˆλ‹Ή

 

그러고 μž¬μ‹€ν–‰ ν–ˆλ”λ‹ˆ 성곡 !!!

이제 corsμ—λŸ¬ μ•ˆ λœ¬λ‹€ γ… γ… γ… γ… 

 

자 이제 μ—¬λŸ¬ 도메인에 λŒ€ν•΄ cross origin 적용만 μ‹œν‚€λ©΄ 돼 ....

λŒ€μΆ© 80ν”„λ‘œλŠ” λλ‚œ κ±° κ°™λ‹€... γ… γ……γ… 

 

 


 

<μ°Έκ³ ν•œ λΈ”λ‘œκ·Έ>

https://medium.com/@hee98.09.14/nginx-cors-https-%EC%84%A4%EC%A0%95-%EB%AC%B8%EC%A0%9C-%ED%95%B4%EA%B2%B0%ED%95%98%EA%B8%B0-5af71812f4a1

 

Nginx CORS + https μ„€μ • 문제 ν•΄κ²°ν•˜κΈ°

Nginx κΈ°μ‘΄ μ„€μ • νŒŒμΌμ€ μ•„λž˜μ™€ κ°™λ‹€.

medium.com

 

(μ§„μ§œ ꡬ글에 μ„œμΉ˜λ˜λŠ” cors κ΄€λ ¨ μ—λŸ¬ 글듀은 λ‚΄κ°€ λ‹€ λˆŒλŸ¬λ³Έλ“― γ…‹...)