1 minute read

Mention : SOP?πŸ™† CORS?πŸ™…β€β™‚οΈ

SOP (Same Origin Policy)

  • λ‹€λ₯Έ 좜처의 λ¦¬μ†ŒμŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” 것에 μ œν•œ ν•˜λŠ” λ³΄μ•ˆ 방식
  • 좜처(origin) λž€?

    https://hanseul-lee.github.io/2020/12/24/20-12-24-URL/0.png

    • Parameters β†’ Query String, Anchor β†’ Fragment
    • URL의 Protocol, Host, Port λ₯Ό 톡해 좜처λ₯Ό νŒλ‹¨ν•  수 μžˆλ‹€ (세가지가 κ°™μ•„μ•Ό SOP)
    • Domain Name β†’ λΈŒλΌμš°μ €λŠ” String value둜 같은지 νŒλ‹¨ν•œλ‹€.
  • μ™œ SOPλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•˜λ‚˜?
  • λ‹€λ₯Έ 좜처의 λ¦¬μ†ŒμŠ€κ°€ ν•„μš”ν•˜λ‹€λ©΄?
    • CORS
  • CORS?
    • Cross-Origin Resource Sharing
    • λ‹€λ₯Έ 좜처의 μžμ›μ„ 곡유

    ꡐ차 좜처 λ¦¬μ†ŒμŠ€ 곡유(CORS)λŠ” μΆ”κ°€ HTTP 헀더λ₯Ό μ‚¬μš©ν•˜μ—¬, ν•œ μΆœμ²˜μ—μ„œ 싀행쀑인 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ λ‹€λ₯Έ 좜처의 μ„ νƒν•œ μžμ›μ— μ ‘κ·Όν•  수 μžˆλŠ” κΆŒν•œμ„ λΆ€μ—¬ν•˜λ„λ‘ λΈŒλΌμš°μ €μ— μ•Œλ €μ£ΌλŠ” μ²΄μ œμ΄λ‹€. -MOZILLA-

  • CORS μ ‘κ·Ό μ œμ–΄ μ‹œλ‚˜λ¦¬μ˜€
    • λ‹¨μˆœ μš”μ²­ (Simple Request)

      https://velog.velcdn.com/images%2Fsj950902%2Fpost%2F5749e233-2da6-4b79-82b8-8b244a72d7de%2Fcors_simplerequest.png

      • Preflight μš”μ²­ 없이 λ°”λ‘œ μš”μ²­μ„ λ‚ λ¦°λ‹€.
      • λ‹€μŒ 쑰건을 λͺ¨λ‘ λ§Œμ‘±ν•΄μ•Ό ν•œλ‹€.
        • GET, POST, HEAD λ©”μ„œλ“œ
        • Content-Type
          • application/x-www-form-urlencoded
          • multipart/form-data
          • text/plain
        • ν—€λ”λŠ” Accept, Accept-Language, Content-Language, Content-Type 만 ν—ˆμš©λœλ‹€.
    • 프리 ν”ŒλΌμ΄νŠΈ μš”μ²­ (Preflight Request)
      • 사전 확인 μž‘μ—…
      • OPTIONS λ©”μ„œλ“œλ₯Ό 톡해 λ‹€λ₯Έ λ„λ©”μΈμ˜ λ¦¬μ†ŒμŠ€μ— μš”μ²­μ΄ κ°€λŠ₯ν•œ 지 확인
      • μš”μ²­μ΄ κ°€λŠ₯ν•˜λ‹€λ©΄ μ‹€μ œ μš”μ²­ (Actual Request)을 보낸닀.

        ![https://miro.medium.com/max/1400/1zCXcC1VkBB16BDXUxkWoew.png](https://miro.medium.com/max/1400/1zCXcC1VkBB16BDXUxkWoew.png)

        • Preflight Request
          • Origin : μš”μ²­ 좜처
          • Access-Control-Request-Method : μ‹€μ œ μš”μ²­μ˜ λ©”μ„œλ“œ
          • Access-Control-Request-Headers : μ‹€μ œ μš”μ²­μ˜ μΆ”κ°€ 헀더
        • Preflight Response
          • Access-Control-Allow-Origin : μ„œλ²„ μΈ‘ ν—ˆκ°€ 좜처
          • Access-Control-Allow-Method : μ„œλ²„ μΈ‘ ν—ˆκ°€ λ©”μ„œλ“œ
          • Access-Control-Allow-Headers : μ„œλ²„ μΈ‘ ν—ˆκ°€ 헀더
          • Access-Control-Max-Age : Preflight 응닡 μΊμ‹œ κΈ°κ°„
          • 응닡 μ½”λ“œλŠ” 200λŒ€μ—¬μ•Ό ν•œλ‹€.
          • 응닡 λ°”λ””λŠ” λΉ„μ–΄μžˆλŠ” 것이 μ’‹λ‹€.
        • μ™œ Preflightκ°€ ν•„μš”ν•œκ°€?
          • corsλ₯Ό λͺ¨λ₯΄λŠ” μ„œλ²„λ₯Ό μœ„ν•΄μ„œ 사전 검증
    • 인증정보 포함 μš”μ²­ (Credentialed Request)
      • 인증 κ΄€λ ¨ 헀더λ₯Ό 포함할 λ•Œ μ‚¬μš©ν•˜λŠ” μš”μ²­μ΄λ‹€.
        • ν΄λΌμ΄μ–ΈνŠΈ μΈ‘ credentials : include
        • μ„œλ²„ μΈ‘ : Access-Control-Allow-Credintial : true (Access-Control-Allow-Origin: *μ•ˆλœλ‹€.)
  • CORS ν•΄κ²°ν•˜κΈ°
    • ν”„λ‘ νŠΈ ν”„λ‘μ‹œ μ„œλ²„ μ„€μ • (개발 ν™˜κ²½)
      • ν”„λ‘μ‹œ(Proxy)λž€ ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„ μ‚¬μ΄μ˜ 쀑계 λŒ€λ¦¬μ 
      • cors proxy app (axios)
    • 직접 μ„œλ²„μ—μ„œ 헀더에 μ„€μ •ν•΄μ£ΌκΈ°
      • 직접 μ„œλ²„μ—μ„œ HTTP 헀더 μ„€μ • (κ°€μž₯ 정석적인 ν•΄κ²°μ±…)
    • μŠ€ν”„λ§ λΆ€νŠΈ μ΄μš©ν•˜κΈ°
      • νŠΉμ • μ»¨νŠΈλ‘€λŸ¬μ— 적용 @CrossOrigin(origins = β€œhttp://localhost:8080”, allowCredentials = β€œtrue”)
      • μ „μ—­μ μœΌλ‘œ μ„€μ • @Configuration

          @Configuration
          public class CorsConfiguration implements WebMvcConfiguer {
          	registry.addMapping("/api")
          					.allowedOrigins("http://localhost:8081")
          }
        
  • Summary
    • ν•œ μΆœμ²˜μ— μžˆλŠ” μžμ›μ—μ„œ λ‹€λ₯Έ μΆœμ²˜μ— μžˆλŠ” μžμ›μ— μ ‘κ·Όν•˜λ„λ‘ ν•˜λŠ” κ²ƒμœΌλ‘œ κ΅μ°¨λ˜λŠ” 좜처 μžμ›λ“€μ˜ κ³΅μœ μž…λ‹ˆλ‹€. λ‹€λ₯Έ 좜처의 λ¦¬μ†ŒμŠ€λ₯Ό κ°€μ Έμ˜€λŠ” μƒν™©μ—μ„œ SOPλŠ” 이 접근을 μ°¨λ‹¨ν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ, CORS 섀정을 ν†΅ν•˜μ—¬ Access-Control-Allow-Origin을 μ„œλ²„μ˜ 응닡헀더에 μž‘μ„±ν•˜κ²Œ 되면 μ ‘κ·Ό κΆŒν•œμ„ 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

Reference πŸ“š

https://www.youtube.com/watch?v=-2TgkKYmJt4

https://inpa.tistory.com/entry/WEB-πŸ“š-CORS-πŸ’―-정리-ν•΄κ²°-방법-πŸ‘#πŸ“œ_동일_좜처_정책이_ν•„μš”ν•œ_이유

Leave a comment