세상에서 제일가는 코어스에러를 해결한 설이 지원이에게 박수 !!! 짝짝짝
cors 에러는 스프링에서 corsconfig를 설정 다 해주고
nginx는 큰 엔진엑스에서 백으로 부분만 처리해줘도 가능하다.
server {
listen 80;
listen [::]:80;
server_name k7a307.p.ssafy.io;
location / {
rewrite ^(.*) https://k7a307.p.ssafy.io:443$1 permanent;
}
}
server{
listen 443 ssl;
listen [::]:443 ssl;
server_name k7a307.p.ssafy.io;
# root /home/ubuntu/test/dist;
#ssl config
ssl_certificate /etc/letsencrypt/live/k7a307.p.ssafy.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/k7a307.p.ssafy.io/privkey.pem;
location / {
proxy_pass <http://k7a307.p.ssafy.io:3000>;
proxy_hide_header Access-Control-Allow-Origin;
proxy_pass_request_headers on;
}
location /api/ {
proxy_pass <http://k7a307.p.ssafy.io:8080/>;
proxy_hide_header Access-Control-Allow-Origin;
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
}
}
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
뇌피셜… $http_origin
은 들어오는 주소를 허용하고… always
는 http status code 200이 아니여도 처리해주는 걸로 … 인터넷에서 찾았다.. 힝
프론트에서는
headers: {
"Content-Type": "application/json",
},
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
.allowCredentials(true);
}
Credential 과 allowOrigin은 같이 쓸 수 없다…
.allowedOriginPatterns("*")
.allowCredentials(true);
이렇게 사용해주자.