Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CORS_再入門.pdf
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
dach
February 27, 2020
1.4k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CORS_再入門.pdf
dach
February 27, 2020
More Decks by dach
See All by dach
dbt_ベストプラクティス_完全に理解した.pdf
dach
2
1.2k
プロジェクトマネージャーと炎の回避術
dach
0
980
SLO_By_Google_Cloud_Monitoring
dach
0
230
状態遷移テスト完全に理解しよう.pdf
dach
0
880
JWT完全に理解しよう-認証編-.pptx.pdf
dach
0
830
JWT完全に理解しよう-公開鍵編-.pptx.pdf
dach
0
730
チームの垣根を越境する_チーム間交換留学
dach
0
110
設計書のないサービスとの付き合い方.pptx.pdf
dach
0
210
designからWebページを作るやりかた完全に理解した.pdf
dach
1
390
Featured
See All Featured
HDC tutorial
michielstock
2
730
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
For a Future-Friendly Web
brad_frost
183
10k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Making the Leap to Tech Lead
cromwellryan
135
10k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
Odyssey Design
rkendrick25
PRO
2
730
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
350
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
300
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
340
Deep Space Network (abreviated)
tonyrice
0
220
Transcript
CORS 再入門 @dach
Who is me? Job • 元SRE →まねーじめんと(実装したい) 所属 • EasyEasy運営 •
TOPGATE • チキン南蛮を支える会(仮) 最近の主な出費 • 食費、嫁の時計
None
Why speak CORS?
Former me 完全に理解したわ
The other day... @dach CORS問題を解消するために、 API 側でも改修入れました。 これで localhost で
CORS が出てたのも解消されました。
あれ? CORSって ClientServer側の話 じゃないの?
What’s CORS?
CORS (オリジン間リソース共有) 追加の HTTP ヘッダーを使用して、あるオリ ジンで動作しているウェブアプリケーションに、 異なるオリジンにある選択されたリソースへの アクセス権を与えるようブラウザーに指示する ための仕組みです。 (「MDN
web docs」より引用) https://developer.mozilla.org/ja/docs/Web/HTTP/CORS
Question 次の2つのケースで、CORS制約で怒られるケースはどれで しょう
In this case: 1 front-end Container API Contaner domain: default-dot-app
domain: api-dot-app
In this case: 2 front-end Container API Contaner port:443 network:A
port:8080 network:A localhost
Answer どちらも
なぜCase2でも怒ら れるのだろうか?
Answer ウェブアプリケーションは、自分とは異なるオリジン (ドメイン、プロトコル、ポート番 号) にあるリソースをリクエストするとき、 オリジン間 HTTP リクエストを実行します。 (「MDN web
docs」より引用)
PlayBack @dach CORS問題を解消するために、 API 側でも改修入れました。 これで localhost で CORS が出てたのも解消されました。
手を加えるところって フロントじゃないの?
CORS in detail const xhr = new XMLHttpRequest(); const url
= 'https://bar.other/resources/public-data/'; xhr.open('GET', url); xhr.onreadystatechange = someHandler; xhr.send();
CORS in detail const xhr = new XMLHttpRequest(); const url
= 'https://bar.other/resources/public-data/'; xhr.open('GET', url); xhr.onreadystatechange = someHandler; xhr.send(); 誰が呼び出したか
CORS in detail if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods:
GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; }
CORS in detail if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods:
GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; } 誰からのアクセス を許可しているか
CORS in detail if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods:
GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; } 誰からのアクセス を許可しているか 結果
CORS in detail const xhr = new XMLHttpRequest(); const url
= 'https://bar.other/resources/public-data/'; xhr.open('GET', url); xhr.onreadystatechange = someHandler; xhr.send(); 誰が呼び出したか 誰からのアクセス を許可しているか if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods: GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; }
Answer 「Access-Comntrol-Allow-Origin」で対象のリソースにアクセスできるオリジンは 誰かを指定する必要がある = API側の修正が必要だった
Conclusion
Conclusion • origin(ドメイン、プロトコル、ポート番号) • 異なるoriginにあるリソースに対してアクセスするときには 「レスポンス側」に設定が必要
Thanks!