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
Paper Plane
katiecoart
PRO
1
51k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
320
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
210
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
240
How STYLIGHT went responsive
nonsquared
100
6.2k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
YesSQL, Process and Tooling at Scale
rocio
174
15k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
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!