[WEB] CORS

dooboocookieยท2023๋…„ 6์›” 28์ผ
0

๐Ÿ˜ณ ๋ฌธ์ œ

  • ์ด๋ฒˆ ๋ฏธ์…˜์—์„œ ๋“œ๋””์–ด ํ”„๋ก ํŠธ์™€์˜ ํ˜‘์—…์„ ์ง„ํ–‰ํ•˜์˜€๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  EC2์— ๋ฐฐํฌ๋ฅผํ•˜๊ณ  public ip๋ฅผ ๊ณต์œ ํ•˜์˜€๋Š”๋ฐ,
    • ์ œ์ผ ๋จผ์ € ํ•ด๊ฒฐํ•  ๋ฌธ์ œ๋Š” CORS์˜€๋‹ค.

๐Ÿšจ SOP

  • Same-Origin Policy, ๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ…
  • ์–ด๋–ค ์ถœ์ฒ˜(Origin)์—์„œ ๋กœ๋“œ๋œ ๋ฆฌ์†Œ์Šค(Resource)์—์„œ ๋‹ค๋ฅธ ์ถœ์ฒ˜(Origin)์˜ ๋ฆฌ์†Œ์Šค(Resource)๋ฅผ ์ œํ•œํ•˜๋Š” ์ •์ฑ…
    • ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ ์šฉ ๋˜๋Š” JavaScript ๋ณด์•ˆ ์ •์ฑ…์ด๋‹ค.

Origin

  • ์ถœ์ฒ˜๋Š” URL์˜ Protocol, HostName, Port๋ฅผ ํ•ฉ์นœ ๊ฒƒ์„ ๋งํ•œ๋‹ค.

    • Protocol
      • ์†ก์‹  ํ˜ธ์ŠคํŠธ์™€ ์ˆ˜์‹  ํ˜ธ์ŠคํŠธ๊ฐ€ ์ •๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ทœ์•ฝ
      • ex. http, https, ftp, ...
    • HostName
      • ๋„๋ฉ”์ธ ์ด๋ฆ„์ด๋‚˜, IP๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
      • ex. localhost, 127.0.0.1, naver.com
    • Port
      - ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ์„ ๋งบ๊ธฐ ์œ„ํ•œ TCP ํฌํŠธ ๋ฒˆํ˜ธ
      - ex. 80, 443, 8080
  • ์ด ์„ธ๊ฐ€์ง€ ์ค‘์— ํ•˜๋‚˜๋ผ๋„ ๋‹ค๋ฅด๋ฉด ๋‹ค๋ฅธ ์ถœ์ฒ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.

  • ์ถœ์ฒ˜์— ๋Œ€ํ•œ ํŒ๋‹จ์€ String Value๋กœ ํ•œ๋‹ค.

    • 127.0.0.1๊ณผ localhost๋Š” ์‹ค์ œ๋กœ๋Š” ๊ฐ™์€ host์ง€๋งŒ ๋‹ค๋ฅธ ์ถœ์ฒ˜๋ผ๊ณ  ํŒ๋‹จํ•œ๋‹ค.
    • localhost์™€ localhost:80 ์€ 80ํฌํŠธ๋Š” ๊ธฐ๋ณธ ํฌํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ์ถœ์ฒ˜๋ผ๊ณ  ํŒ๋‹จํ•œ๋‹ค.

๐Ÿ”— CORS

  • Cross Origin Resources Sharing, ๊ต์ฐจ ์ถœ์ฒ˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ 
  • ๋‹ค๋ฅธ ์ถœ์ฒ˜(Origin)์—์„œ ๋ฆฌ์†Œ์Šค(Resource)๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ •์ฑ…

Origin Header

  • ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, Origin์ด๋ผ๋Š” ํ—ค๋”์— ํ˜„์žฌ ๋ฆฌ์†Œ์Šค์˜ ์ถœ์ฒ˜ ์ •๋ณด๋ฅผ ๋‹ด์•„์„œ ์š”์ฒญ์„ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค.

Access-Control-Allow-Origin Header

  • ์„œ๋ฒ„์—์„œ ์–ด๋–ค ์ถœ์ฒ˜์—์„œ์˜ ์š”์ฒญ๋งŒ ํ—ˆ์šฉํ•˜์—ฌ ํ•ด๋‹น ์„œ๋ฒ„์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณต์œ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.

CORS ์‹œ๋‚˜๋ฆฌ์˜ค

1. Simple Request

  • ๋‹จ์ˆœํžˆ Access-Control-Allow-Origin ๋“ฑ์˜ ํ—ค๋”์˜ ์ •๋ณด๋ฅผ ํŒŒ์•…ํ•˜์—ฌ, ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ์ถœ์ฒ˜์ธ์ง€ ํŒŒ์•…ํ•œ๋‹ค.
  • ์ œ์•ฝ์กฐ๊ฑด๋“ค์ด ์กด์žฌํ•œ๋‹ค.
    • ๋ฉ”์†Œ๋“œ๋Š” GET, HEAD, POST๋งŒ ๊ฐ€๋Šฅ
    • ํ—ค๋”๋Š” Accept, Accept-Language, Content-Language, Content-Type, DPR, Downlink, Save-Data, Viewport-Width, Width๋งŒ ๊ฐ€๋Šฅ
    • Content-Type์€ application/x-www-form-urlencoded, multipart/form-data, text/plain๋งŒ ๊ฐ€๋Šฅ

2. Preflight Request

  • ๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค
  • ์˜ˆ๋น„ ์š”์ฒญ์„ ๋ณด๋‚ด Simple Request์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Access-Control-Allow-Origin ๋“ฑ์„ ํ™•์ธํ•˜์—ฌ, ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ์ถœ์ฒ˜์ธ์ง€ ํŒŒ์•…ํ•œ๋‹ค.
  • OPTIONS ๋ฉ”์„œ๋“œ์˜ ์š”์ฒญ์„ ๋ณด๋‚ด์„œ ํ—ˆ์šฉ๋˜๋Š” ์ถœ์ฒ˜์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

(์ถœ์ฒ˜: https://livebook.manning.com/concept/cors/preflight-cache)

3. Credentialed Request

  • ์ธ์ฆ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š” ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค์ด๋‹ค.
  • Access-Control-Allow-Origin ํ—ค๋”์— *(์™€์ผ๋“œ ์นด๋“œ)๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • ์‘๋‹ต์— Access-Control-Allow-Credentials ํ—ค๋”๋Š” true๋กœ ๊ฐ€์•ผํ•œ๋‹ค.

Spring์—์„œ CORS ์„ค์ •

1. WebMvcConfigurer๋ฅผ ์ด์šฉํ•œ ์„ค์ •

  • ์ „์—ญ์œผ๋กœ ์„ค์ •
  • addMapping
    • ํ•ด๋‹น ์„ค์ •์— ๋Œ€ํ•ด์„œ ์š”์ฒญ์„ ๋งคํ•‘ํ•˜๋Š” ์„ค์ •
  • allowedOrigins
    • ํ—ˆ์šฉํ•  ์ถœ์ฒ˜๋ฅผ ์„ค์ •
    • Access-Control-Allow-Origin ํ—ค๋”์— ์ •๋ณด๋ฅผ ๋‹ด์Œ
  • allowedMethods
    • ํ—ˆ์šฉํ•  ์š”์ฒญ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ค์ •
    • Access-Control-Allow-Methods ํ—ค๋”์— ์ •๋ณด๋ฅผ ๋‹ด์Œ
  • allowedHeaders
    • ํ—ˆ์šฉํ•  ํ—ค๋”๋ฅผ ์„ค์ •
    • Access-Control-Allow-Headers ํ—ค๋”์— ์ •๋ณด๋ฅผ ๋‹ด์Œ
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(final CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3000);
    }
}

2. ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

  • @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํ•ธ๋“ค๋Ÿฌ์— ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์„ค์ •
@RestController
public class MyController {
    @CrossOrigin(origins = "https://a.aa")
    @PostMapping("/orders")
    public ResponseEntity<Void> orders() {
        return ResponseEntity.build();
    }
}

๐Ÿ‘ ๊ฒฐ๋ก 

  • SOP
    • ์–ด๋–ค ์ถœ์ฒ˜(Origin)์—์„œ ๋กœ๋“œ๋œ ๋ฆฌ์†Œ์Šค(Resource)์—์„œ ๋‹ค๋ฅธ ์ถœ์ฒ˜(Origin)์˜ ๋ฆฌ์†Œ์Šค(Resource)๋ฅผ ์ œํ•œํ•˜๋Š” ์ •์ฑ…
  • CORS
    • ๋‹ค๋ฅธ ์ถœ์ฒ˜(Origin)์—์„œ ๋ฆฌ์†Œ์Šค(Resource)๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ •์ฑ…
    • Preflight Request๊ฐ€ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค๋‹ค.
  • Spring์œผ๋กœ CORS ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€ ์žˆ๋‹ค.
    • ์–ด๋…ธํ…Œ์ด์…˜ ์„ค์ •
    • WebMvcConfiguration ์„ค์ •
profile
1์ผ 1์‚ฐ์ฑ… 1์ปค๋ฐ‹

0๊ฐœ์˜ ๋Œ“๊ธ€