๋ฐ์ํ
2022.07.06 - 2์ฐจ์ ์ผ๋ก ์ถ๊ฐ๋ ๋ด์ฉ์ด ์์ต๋๋ค.
https://lucete-stellae.tistory.com/98
SpringBoot 2.7+ CORS ์ด์ ๋ฐ ํด๊ฒฐ๋ฐฉ๋ฒ
๊ฐ๋จํ๊ฒ ๋ง๋ API ์๋ฒ๋ฅผ ํ ์คํธ ํ๊ธฐ ์ํด ๋ก์ปฌ์์ ๋๋ฆฌ๋ ๋์ค ํด๋น ์ด์๋ฅผ ๋ง๋ฌ๋ค. ๊ตฌ์ฑ์ ๋ค์๊ณผ ๊ฐ๋ค. localhost:8080/users - SpringBoot API Server ์์ ์ ์ ๋ฐ์ดํฐ๋ฅผ ๋ฆฌํด. localhost:8090/index.html..
lucete-stellae.tistory.com
1. Global WebConfig
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // CORS๋ฅผ ์ ์ฉํ URL ํจํด.
/*
* [addMapping ์ค์ ๋ง ํ์ ๋์ ๊ธฐ๋ณธ ๊ฐ]
* Allow all origins.
* Allow "simple" methods GET, HEAD and POST.
* Allow all headers.
* Set max age to 1800 seconds (30 minutes).
*/
.allowedOrigins("*") // CORS๋ฅผ ํ์ฉํ Origin ์ง์ . ํน์ ์ฃผ์์์ ์์ฒญ ์ ํ์ฉํ๋ ๊ฒ์ ์๋ฏธ. .allowedOrigins("http://localhost:8080","http://localhost:80") ์ ๊ฐ์ด ์ง์ ์ฃผ์ ๋งคํ๊ฐ๋ฅ.
.allowedMethods("GET", "POST") // CORS๋ฅผ ํ์ฉํ ๋ฉ์๋.
.maxAge(3000); // ์์ฒญ์ ๋ํ ์บ์ฑ ์ฒ๋ฆฌ ์๊ฐ
}
}
2. Annotation
๋์ผํ ๋ฐฉ์์ผ๋ก ์ปจํธ๋กค๋ฌ ์ ์ฒด ํน์ ๋ฉ์๋์ ์ ์ฉ ๊ฐ๋ฅ.
@RequestMapping("/")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class SampleController {
@RequestMapping("/getData", method = RequestMethod.POST)
@CrossOrigin(origins = "*", allowedHeaders = "*")
public int SampleGetData() {
...
}
}
์ฐธ๊ณ ๊ฐ์ด๋
https://spring.io/guides/gs/rest-service-cors/
Enabling Cross Origin Requests for a RESTful Web Service
this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team
spring.io
728x90
๋ฐ์ํ
'๐ฎSpring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SpringBoot] Rest Api Sample ๋ง๋ค๊ธฐ #2 (0) | 2022.07.05 |
---|---|
[SpringBoot] Rest Api Sample ๋ง๋ค๊ธฐ #1 (0) | 2022.07.05 |
[AOP] AOP Aspect ๋ฅผ ์ด์ฉํ ๋ก๊ทธ ์ฒ๋ฆฌ ํ๊ธฐ (0) | 2022.04.18 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -4- (0) | 2022.03.30 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -3- (0) | 2022.03.27 |
๋๊ธ