(๊ฐ๋ ๊ณผ ๋์ ์๋ฆฌ๋ฅผ ์ ํํ ๋ชฐ๋ผ์) ์๋ง์ ์ต๊น๋ฅผ ๋นํ๊ณ
๊ตฌ๊ธ๋ง์ผ๋ก Mdn ๋ฌธ์๋ฅผ ์ฐพ์ ์ดํดํ ํ ํด๊ฒฐํจ (์ญ์ ์ ์๋๋ก ๊ฐ์ผ ํจ)
๋ฐ๊ฒฌํ๋ cors ํต์ฌ ๊ฐ๋ ์ ์ ์ด๋ณด๊ณ ์ ํ๋ค
CORS๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์์ ์ ์ถ์ฒ๊ฐ ์๋ ๋ค๋ฅธ ์ด๋ค ์ถ์ฒ๋ก๋ถํฐ ์์์ ๋ก๋ฉํ๋ ๊ฒ์ ํ์ฉํ๋๋ก ์๋ฒ๊ฐ ํ๊ฐ ํด์ฃผ๋ HTTP ํค๋ ๊ธฐ๋ฐ ๋ฉ์ปค๋์ฆ์ด๋ค.
Preflight Request ์์ฒญ์์ด ์๋ฒ์ ์งํ์ผ๋ก ๋ณธ ์์ฒญ์ ๋ณด๋ธ ํ, ์๋ฒ๊ฐ ์ด์ ๋ํ ์๋ต์ ํค๋์ Access-Control-Allow-Origin ํค๋๋ฅผ ๋ณด๋ด์ฃผ๋ฉด ๋ธ๋ผ์ฐ์ ๊ฐ CORS ์ ์ฑ ์๋ฐ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ค.
1) ์์ฒญ์ ๋ฉ์๋๋ GET, HEAD, POST ์ค ํ๋๋ค
2) Accept, Accept--Language, Content-Language, Content-Type, DPR, Downlink, Save-Data, Viewport-Width, Width ํค๋์ผ ๊ฒฝ์ฐ์๋ง ์ ์ฉ๋๋ค
3) Content-Type ํค๋๊ฐ applicaiton/x-www-form-urlencoded, multipart/form-data, text/plain ์ค ํ๋์ฌ์ผ ํ๋ค. ์๋ ๊ฒฝ์ฐ ์๋น ์์ฒญ์ผ๋ก ๋์๋๋ค
4) ์์ฒญ์ ReadableStream ๊ฐ์ฒด๊ฐ ์ฌ์ฉ๋์ง ์๋๋ค
| ๋ธ๋ผ์ฐ์ => ์๋ฒ Origin : A |
์๋ฒ => ๋ธ๋ผ์ฐ์ Access-Control-Allow-Origin : * |
๋ธ๋ผ์ฐ์ ๋ ์๋ฒ์ ์๋น ์์ฒญ์ ๋ณด๋ด ์ ํต์ ๋๋์ง ํ์ธํ ํ ๋ณธ ์์ฒญ์ ๋ณด๋ธ๋ค
๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฒ์ ์๋น ์์ฒญ์ ๋ณด๋ด๋ ๊ฒ์ Preflight๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ, ์ด ์๋น ์์ฒญ์ HTTP ๋ฉ์๋๋ OPTIONS ๊ฐ ์ฌ์ฉ๋๋ค.
>> Access-Control-Max-Age ํค๋์ ์บ์๋ ์๊ฐ์ ๋ช ์ํ์ฌ Preflight ์์ฒญ์ ์บ์ฑํ์ฌ ์ต์ ํ๋ฅผ ์์ผ์ค ์ ์๋ค
ํด๋ผ์ด์ธํธ์์ ์๋ฒ์๊ฒ ์๊ฒฉ ์ธ์ฆ ์ ๋ณด(Credential)์ ์ค์ด ์์ฒญํ ๋ ์ฌ์ฉ๋๋ ์์ฒญ์ด๋ค
same-origin : ๊ฐ์ ์ถ์ฒ ๊ฐ ์์ฒญ์๋ง ์ธ์ฆ ์ ๋ณด๋ฅผ ๋ด์ ์ ์๋ค
include : ๋ชจ๋ ์์ฒญ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ๋ด์ ์ ์๋ค
omit : ๋ชจ๋ ์์ฒญ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ๋ด์ง ์๋๋ค
Access-Control-Allow-Credentials๋ฅผ true๋ก ์ค์ ํ๊ฒ ๋๋ฉด,
Access-Control-Allow-Origin
Access-Control-Allow-Methods
Access-Control-Allow-Headers ๊ฐ์ * ๋ฅผ ์ฌ์ฉํ ์ ์๋ค
[๋ฌธ์] CORS ๊ฐ๋ ๊ณผ ์ ๊ทผ ์ ์ด ์๋๋ฆฌ์ค
https://developer.mozilla.org/ko/docs/Web/HTTP/CORS
๊ต์ฐจ ์ถ์ฒ ๋ฆฌ์์ค ๊ณต์ (CORS) - HTTP | MDN
๊ต์ฐจ ์ถ์ฒ ๋ฆฌ์์ค ๊ณต์ (Cross-Origin Resource Sharing, CORS)๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์์ ์ ์ถ์ฒ๊ฐ ์๋ ๋ค๋ฅธ ์ด๋ค ์ถ์ฒ(๋๋ฉ์ธ, ์คํด ํน์ ํฌํธ)๋ก๋ถํฐ ์์์ ๋ก๋ฉํ๋ ๊ฒ์ ํ์ฉํ๋๋ก ์๋ฒ๊ฐ ํ๊ฐ ํด์ฃผ๋ HTTP
developer.mozilla.org
Access-Control-Allow-Origin : ์๋ฒ๊ฐ ํ์ฉํ๋ ์ถ์ฒ(origin)์ ์ ์ํ๋ค
Access-Control-Allow-Methods : ์๋ฒ๊ฐ ํ์ฉํ๋ HTTP ๋ฉ์๋๋ฅผ ์ ์ํ๋ค (GET, POST, PUT ๋ฑ)
Access-Control-Allow-Headers : ์๋ฒ๊ฐ ํ์ฉํ๋ ์์ฒญ ํค๋๋ฅผ ์ ์ํ๋ค (Authorization, Content-Type)
Access-Control-Allow-Credentials : cross-origin์๊ฒ ์๊ฒฉ ์ฆ๋ช (์ฟ ํค, ์ธ์ฆ ํค๋ ๋ฑ)์ ํฌํจํ๋ ์์ฒญ์ ํ์ฉํ ์ง ์ค์ ํ๋ค
Access-Control-Expose-Headers : ์๋ต์์ ์ฌ์ฉํ ์ ์๋ ์ถ๊ฐ์ ์ธ ํค๋๋ฅผ ์ ์ํ๋ค
Access-Control-Max-Age : ํ๋ฆฌํ๋ผ์ดํธ ์์ฒญ์ ์บ์ ์๊ฐ์ ์ค์ ํ๋ค. ๋ธ๋ผ์ฐ์ ๋ ์ด ์๊ฐ ๋์ ๋์ผํ CORS ์์ฒญ์ ๋ํด ํ๋ฆฌํ๋ผ์ดํธ ์์ฒญ์ ๋ณด๋ด์ง ์๋๋ค
๊ธฐ๋ณธ์ ์ผ๋ก CORS-safelisted ์๋ต ํค๋(Cache-Control, Content-Type, ... )๋ง ๋ ธ์ถ๋๋ค
[๋ฌธ์] Access-Control-Expose-Headers
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
Access-Control-Expose-Headers - HTTP | MDN
The HTTP Access-Control-Expose-Headers response header allows a server to indicate which response headers should be made available to scripts running in the browser in response to a cross-origin request.
developer.mozilla.org
[๋ฌธ์] CORS-safelisted ์๋ต ํค๋
https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header
CORS-safelisted response header - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
A CORS-safelisted response header (also known as "simple response header") is an HTTP header in a CORS response that it is considered safe to expose to client scripts. Only safelisted response headers are made available to web pages.
developer.mozilla.org
Access-Control-Allow-Credentials : true ๊ฐ ์ค์ ๋์ง ์์ผ๋ฉด, credentials : include๊ฐ ์๋ํ์ง ์๋๋ค
[๋ฌธ์] Access-Control-Allow-Credentials
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
Access-Control-Allow-Credentials - HTTP | MDN
The HTTP Access-Control-Allow-Credentials response header tells browsers whether the server allows credentials to be included in cross-origin HTTP requests.
developer.mozilla.org
Reason: CORS header 'Access-Control-Allow-Origin' does not match 'xyz'
Access-Control-Allow-Origin 'xyz' ์ถ๊ฐ
Reason: Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*'
๋์ ์ค์ ๋ถ๊ฐ
Reason: expected 'true' in CORS header 'Access-Control-Allow-Credentials'
Access-Control-Allow-Credentials true ์ค์
Reason: invalid token 'xyz' in CORS header 'Access-Control-Allow-Headers'
preflight ์์ฒญ์ผ๋ก Access-Control-Allow-Headers ํค๋๊ฐ ์๋ฒ์ ์๋ต์ผ๋ก ์ ๊ณต๋๋ค. ์ด๋, ์ ํจํ์ง ์์ ๊ฐ์ด Access-Controll-Allow-Headers์ ํฌํจ๋๋ฉด ํด๋น ์๋ฌ๊ฐ ๋ฐ์ํ๋ค
>> Preflight ์์ฒญ, ์์ผ๋ ์นด๋ ์ฌ์ฉ ๋ถ๊ฐ ๋ด์ฉ์ ๋ชฐ๋ผ ๊ณ ์์ ํ์๋ค
Nginx ์ค์ ์์
location / {
if ($request_method = 'OPTIONS') { // preflight ์์ฒญ ์ฒ๋ฆฌ
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain';
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
}
Spring Boot ์ค์ ์์
@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("https://www.vvue.site", "http://localhost:3000") // Development environment
.exposedHeaders("Authorization", "refresh-token")
.allowCredentials(true); // true๋ก ์ค์ ํ์ผ๋ฏ๋ก origin, method, headers ๋ฅผ * ๋ก ์ค์ ๋ชปํจ
}
}
allowedMethods๋ฅผ ์ค์ ์ํ๋ฉด GET, HEAD ๋ง ํ์ฉ๋๋ค
