Code review
๐ป[Code Review] WebConfig ํด๋์ค ์ฝ๋ ๋ฆฌ๋ทฐ.
1๏ธโฃ ์ ์ฒด ์ฝ๋.
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Controller
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // ๋ชจ๋ ๊ฒฝ๋ก ํ์ฉ
.allowedOriginPatterns("http://localhost:*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true) // ์๊ฒฉ ์ฆ๋ช
ํ์ฉ
.maxAge(3600);
}
}
- ์ ์ฝ๋๋ Spring Framework์์ CORS(Cross-Origin Resource Sharing) ์ค์ ์ ์ ์ํ ์ฝ๋์
๋๋ค.
- WebMvcConfigurer ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ CORS ์ ์ฑ
์ ๊ตฌ์ฑํ๊ณ , ํด๋ผ์ด์ธํธ(์: ํ๋ก ํธ์๋)์์ ๋ฐฑ์ํธ API์ ์์ฒญํ ๋ ๋ฐ์ํ ์ ์๋ CORS ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค.
2๏ธโฃ CORS๋? ๐ค
- CORS(Cross-Origin Resource Sharing)๋ ๋ธ๋ผ์ฐ์ ๋ณด์ ์ ์ฑ
์ค ํ๋๋ก, ์น ์ ํ๋ฆฌ์ผ์ด์
์ด ์์ ๊ณผ ๋ค๋ฅธ ์ถ์ฒ(origin)์์ ๋ฆฌ์์ค๋ฅผ ์์ฒญํ ๋ ์ด๋ฅผ ํ์ฉํ๊ฑฐ๋ ์ ํํ๋ ๋ฐฉ์์
๋๋ค.
- ์๋ฅผ ๋ค์ด:
- ํด๋ผ์ด์ธํธ๊ฐ
http://localhost:3000
์์ ๋์ํ๊ณ , ์๋ฒ๊ฐ http://localhost:8080
์์ ๋์ํ๋ฉด ๋ ๋๋ฉ์ธ์ ์๋ก ๋ค๋ฅธ ์ถ์ฒ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
- ๋ธ๋ผ์ฐ์ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ฐ ๊ต์ฐจ ์ถ์ฒ ์์ฒญ์ ์ฐจ๋จํ๋ฏ๋ก, ์๋ฒ์์ ๋ช
์์ ์ผ๋ก ์ด๋ฅผ ํ์ฉํด์ผ ํฉ๋๋ค.
3๏ธโฃ ์ฝ๋ ๋ถ์.
1๏ธโฃ ํด๋์ค ์ ์ธ.
@Controller
public class WebConfig implements WebMvcConfigurer {...}
- WebMvcConfigurer๋ Spring MVC ์ค์ ์ ์ปค์คํฐ๋ง์ด์งํ๊ธฐ ์ํ ์ธํฐํ์ด์ค์
๋๋ค.
- WebConfig ํด๋์ค๋ WebMvcConfigurer๋ฅผ ๊ตฌํํ์ฌ Spring MVC ์ค์ ์ ์ฌ์ฉ์ ์ ์ํ๊ณ ์์ต๋๋ค.
2๏ธโฃ addCorsMappings ๋ฉ์๋.
@Override
public void addCorsMappings(CorsRegistry registry) {...}
- Spring MVC์์ CORS ๋งคํ์ ์ค์ ํ๊ธฐ ์ํด CorsRegistry๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ์ด ๋ฉ์๋๋ ํด๋ผ์ด์ธํธ์์ ํน์ ๊ฒฝ๋ก๋ก์ ์์ฒญ์ ํ์ฉํ๊ฑฐ๋ ์ ํํ๋ ๊ท์น์ ์ ์ํฉ๋๋ค.
3๏ธโฃ CORS ์ค์ .
registry.addMapping("/**") // ๋ชจ๋ ๊ฒฝ๋ก ํ์ฉ
.allowedOriginPatterns("http://localhost:*") // localhost์ ๋ชจ๋ ํฌํธ ํ์ฉ
.allowedMethods("GET", "POST", "PUT", "DELETE") // ํ์ฉํ HTTP ๋ฉ์๋
.allowedHeaders("*") // ๋ชจ๋ ํค๋ ํ์ฉ
.alloweCredentials(true) // ์ฟ ํค์ ๊ฐ์ ์๊ฒฉ ์ฆ๋ช
ํ์ฉ
.maxAge(3600) // ์บ์ ์๊ฐ (์ด ๋จ์)
-
addMapping("/**")
- ๋ชจ๋ URL ํจํด
(/**)
์ ๋ํด CORS ์ ์ฑ
์ ์ ์ฉํฉ๋๋ค.
- ์:
/api/*, /resources/*
๋ฑ ๋ชจ๋ ๊ฒฝ๋ก ํ์ฉ.
-
allowedOriginPatterns("http://localhost:*)"
- ํด๋ผ์ด์ธํธ๊ฐ
http://localhost
์์ ์์๋๋ ๋ชจ๋ ํฌํธ(์: http://localhost:3000, http://localhost:8080
)์ ๋ํด ์์ฒญ์ ํ์ฉํฉ๋๋ค.
- Spring Boot 2.4+์์๋ allowedOrigin ๋์ allowedOriginPatterns ์ฌ์ฉ์ ๊ถ์ฅํฉ๋๋ค.
-
allowedMethods("GET", "POST", "PUT", "DELETE")
- ํ์ฉํ HTTP ๋ฉ์๋(GET, POST, PUT, DELETE)๋ฅผ ์ ์ํฉ๋๋ค.
- ์๋ฅผ ๋ค์ด, OPTIONS์ ๊ฐ์ ๋ค๋ฅธ ๋ฉ์๋๋ ํ์ฉ๋์ง ์์ต๋๋ค.
-
allowedHeaders("*")
- ๋ชจ๋ ์์ฒญ์ ํค๋๋ฅผ ํ์ฉํฉ๋๋ค.
- ์: Content-Type, Authorization ๋ฑ์ด ํ์ฉ๋ฉ๋๋ค.
-
allowCredentials(true)
- ํด๋ผ์ด์ธํธ์์ ์ฟ ํค๋ฅผ ์ฌ์ฉํ ์์ฒญ์ ํ์ฉํฉ๋๋ค.
- ์: ์ธ์ฆ์ด ํ์ํ ์์ฒญ์์ ์ฟ ํค๋ฅผ ํตํด ์ธ์
์ ๋ณด๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค.
-
maxAge(3600)
- ๋ธ๋ผ์ฐ์ ๊ฐ ํ๋ฆฌํ๋ผ์ดํธ ์์ฒญ(OPTIONS ์์ฒญ)์ ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํ๋ ์๊ฐ์ ์ ์ํฉ๋๋ค.
- ์ฌ๊ธฐ์๋ 3600์ด(1์๊ฐ) ๋์ ์บ์ฑํฉ๋๋ค.
4๏ธโฃ ์ฌ์ฉ ์ฌ๋ก.
-
1. ํ๋ก ํธ์๋-๋ฐฑ์๋ ๋ถ๋ฆฌ๋ ํ๊ฒฝ.
- ํ๋ก ํธ์๋์ ๋ฐฑ์๋๊ฐ ์๋ก ๋ค๋ฅธ ์ถ์ฒ์์ ๋์ํ๋ ๊ฒฝ์ฐ:
- ํ๋ก ํธ์๋:
http://localhost:3000
- ๋ฐฑ์๋:
http:// localhost:8080
- ์ด ๊ฒฝ์ฐ, ๋ธ๋ผ์ฐ์ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ต์ฐจ ์ถ์ฒ ์์ฒญ์ ์ฐจ๋จํ๋ฏ๋ก ์์ ๊ฐ์ด CORS ์ค์ ์ด ํ์ํฉ๋๋ค.
5๏ธโฃ ์ฃผ์์ฌํญ.
-
๋ณด์ ๊ณ ๋ ค
- ์ค์ ๋ฐฐํฌ ํ๊ฒฝ์์๋
allowedOriginPatterns
์ ์์ผ๋์นด๋("*")
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ํผํ๊ณ , ํน์ ์ถ์ฒ๋ง ํ์ฉํด์ผ ํฉ๋๋ค.
- ์:
.allowedOriginPatterns("https://example.com)"
-
allowCredentials
์ *
์ ์ถฉ๋
-
allowCredentials(true)
์ allowedOriginPatterns("*")
๋ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ์ฟ ํค๋ฅผ ์ฌ์ฉํ ์์ฒญ์ ํ์ฉํ๋ ค๋ฉด ํน์ ์ถ์ฒ๋ฅผ ๋ช
์ํด์ผ ํฉ๋๋ค.