ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ CORS ์ค์ 1
Access to XMLHttpRequest at 'http://localhost:5050/auth/login' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Vue์์ SpringBoot ์๋ฒ๋ก REST API๋ฅผ ์์ฒญํ์ ๋ ๋ค์๊ณผ ๊ฐ์ CORS ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
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("/**")
.allowedOrigins("http://localhost:8080")
.allowCredentials(true);
}
}
๐ CORS ์ค์ 2
Access to XMLHttpRequest at 'http://localhost:5050/user/image?image_path=images%2Fprofile%2F5.jpg' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
๐ Spring Security๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ง ํด๋น
์ธ์ฆ ํ ํฐ์ ๊ฐ์ด ์ ์กํ์ฌ ์ธ์ฆ ๊ณผ์ ์ ๊ฑฐ์น ๊ฒฝ์ฐ ๋ CORS ์๋ฌ๊ฐ ๋ฐ์ํ์๋ค.
Spring Security ์ค์ ๋ถ๋ถ์์ ๋ค์๊ณผ ๊ฐ์ด CORS ์ค์ ์ ์ถ๊ฐํด์ค๋ค.
// WebSecurityConfig.java
@Override
protected void configure(HttpSecurity http) throws Exception {
http
...
.and()
.cors()
...
}
Ref.
Spring Boot + Vue CORS ์ค์ - ์ ์ฅํ๊ณ ๊น๋จน์ (jungguji.github.io)
[Spring Security] CORS ์ ๋ํ์ฌ :: Icarus (tistory.com)