Java/Spring Boot
[Spring Boot] CORS
hh_lin
2022. 4. 3. 18:42
1. SOP과 CORS
- Cross-Origin Resource Sharing
- Single-Origin Policy를 우회하기 위한 표준 기술
(SOP, CORS 모두 웹 브라우저가 지원하는 기술) - SOP는 같은 Origin에만 요청을 보낼 수 있고
CORS는 서로 다른 Origin끼리 리소스를 공유할 수 있는 방법을 제공하는 표준
(기본적으로는 SOP가 적용되어 있어서 Origin이 다른 경우 호출하지 못함)
ex) 어떤 REST API를 제공하는 서버가 http://localhost:8080에서 제공이 되고 있고,
해당 REST API를 http://localhost:8443 사용하는 어플리케이션이 호출하지 못함
-> SOP 위반
2. Origin
: 스키마, hostname, 포트를 조합한 것
- URI 스키마 (http, https)
- hostname (whiteship.me, localhost)
- 포트 (8080, 18080)
3. 스프링 MVC @CrossOrigin
- @Controller나 @RequestMapping에 추가하거나 -> ①
- WebMvcConfigurer 사용해서 글로벌 설정 -> ②
① @Controller나 @RequestMapping에 추가
@SpringBootApplication
@RestController
public class SpringCorsServerApplication {
@CrossOrigin(origins = "http://localhost:8443")
@GetMapping("/hello")
public String hello() {
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(SpringCorsServerApplication.class, args);
}
}
② WebMvcConfigurer 사용해서 글로벌 설정
: 여러 컨트롤러에 걸쳐서 설정해야 하는 경우에는 WebConfig 파일을 만들기
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
// /hello 매핑을 8443에 공개하겠다
registry.addMapping("/hello")
.allowedOrigins("http://localhost:8443");
}
}
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard
스프링 부트 개념과 활용 - 인프런 | 강의
스프링 부트의 원리 및 여러 기능을 코딩을 통해 쉽게 이해하고 보다 적극적으로 사용할 수 있는 방법을 학습합니다., - 강의 소개 | 인프런...
www.inflearn.com
Web on Servlet Stack
This part of the reference documentation covers support for Servlet stack, WebSocket messaging that includes raw WebSocket interactions, WebSocket emulation via SockJS, and pub-sub messaging via STOMP as a sub-protocol over WebSocket. 4.1. Introduction The
docs.spring.io