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
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
220
状態遷移テスト完全に理解しよう.pdf
dach
0
880
JWT完全に理解しよう-認証編-.pptx.pdf
dach
0
820
JWT完全に理解しよう-公開鍵編-.pptx.pdf
dach
0
730
チームの垣根を越境する_チーム間交換留学
dach
0
110
設計書のないサービスとの付き合い方.pptx.pdf
dach
0
210
designからWebページを作るやりかた完全に理解した.pdf
dach
1
380
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
55
12k
sira's awesome portfolio website redesign presentation
elsirapls
0
280
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
390
A designer walks into a library…
pauljervisheath
211
24k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
It's Worth the Effort
3n
188
29k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
360
How to make the Groovebox
asonas
2
2.2k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
430
Context Engineering - Making Every Token Count
addyosmani
9
970
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!