ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

 

๐Ÿ‘ 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()
            ...
}

 

https://velog.io/@hellozin/Spring-API-%EC%84%9C%EB%B2%84%EC%97%90%EC%84%9C-PUT-DELETE-%EC%9A%94%EC%B2%AD-%EC%8B%9C-CORS-%EC%84%A4%EC%A0%95%EC%9D%B4-%EC%A0%81%EC%9A%A9-%EC%95%88%EB%90%98%EB%8A%94-%EA%B2%BD%EC%9A%B0

 

Spring API ์„œ๋ฒ„์—์„œ PUT, DELETE ์š”์ฒญ ์‹œ CORS ์„ค์ •์ด ์ ์šฉ ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ

Spring Backend์— Vue Frontend๋ฅผ ๊ตฌ์„ฑํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋˜ ์ค‘ POST ์š”์ฒญ์€ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š”๋ฐ PUT ์š”์ฒญ ์‹œ 403 ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ƒํ™ฉ์„ ๋งŒ๋‚ฌ์Šต๋‹ˆ๋‹ค. ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์™€ ์š”์ฒญ์„ ์‚ดํŽด๋ณด๋‹ˆ PUT ์š”์ฒญ ์ „ preflight ์š”

velog.io

 


Ref.

Spring Boot + Vue CORS ์„ค์ • - ์ €์žฅํ•˜๊ณ  ๊นŒ๋จน์ž (jungguji.github.io)

[Spring Security] CORS ์— ๋Œ€ํ•˜์—ฌ :: Icarus (tistory.com)

728x90
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€