물론입니다! 웹 개발을 하다 보면 자주 마주치는 CORS 오류는 다음과 같은 의미입니다:
**CORS(Cross-Origin Resource Sharing)**는 다른 출처(Origin) 간에 리소스를 요청할 때 발생하는 보안 정책입니다.
http://localhost:3000에서 실행 중인데, 백엔드는 http://localhost:8080에 있다면 서로 다른 Origin으로 간주됩니다.브라우저 콘솔에 이런 식으로 뜹니다:
Access to fetch at 'http://localhost:8080/api/data' from origin 'http://localhost:3000' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
이 오류는:
@CrossOrigin(origins = "http://localhost:3000") // 프론트 주소
@RestController
public class MyApiController {
// ...
}
혹은 전역 설정:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000") // 허용할 프론트 주소
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowCredentials(true);
}
}
| 용어 | 의미 |
|---|---|
| CORS | Cross-Origin Resource Sharing |
| 오류 발생 원인 | 브라우저가 다른 출처의 요청을 차단 |
| 해결 방법 | 백엔드 서버가 Access-Control-Allow-Origin 헤더를 설정 |
원하시면, Node.js, Nginx, Express, Flask 등의 환경에서도 CORS 해결 방법을 알려드릴 수 있어요.
어떤 환경에서 오류가 발생했는지 알려주실 수 있나요?