การแชร์ทรัพยากรข้ามต้นทาง (CORS)
ทำความเข้าใจนโยบาย CORS ในบริบทของ OAuth2 และ OIDC โดยเฉพาะสำหรับแอปพลิเคชันหน้าเดียวที่เข้าถึงทรัพยากรที่ได้รับการปกป้อง
การแชร์ทรัพยากรข้ามต้นทาง (CORS) เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
CORS & Secure Web Apps
Welcome! Today we'll explore Cross-Origin Resource Sharing (CORS), a crucial security feature for modern web applications.
CORS allows web browsers to securely handle requests between a client-side application (like a Single-Page Application, SPA) and an API server when they're on different domains.
This is especially vital when your SPA uses OAuth2 or OpenID Connect to access protected resources.
The Same-Origin Policy
To understand CORS, we first need to know about the Same-Origin Policy (SOP).
- SOP is a fundamental browser security feature.
- It prevents web pages from making requests to a different domain than the one that served the page.
- For example, a script from
app.example.comcannot directly make an AJAX request toapi.anothersite.com.
This protects users from malicious scripts trying to steal data from other sites you're logged into.
Why CORS is Needed
While SOP is great for security, it creates a problem for modern web architectures.
Many applications, especially SPAs, need to fetch data from APIs hosted on a different domain or port. For instance:
- Your SPA is at
https://my-app.com - Your API is at
https://api.my-app.com - Your OAuth2 Authorization Server is at
https://auth.my-app.com
CORS provides a controlled way to relax the SOP, allowing these cross-origin requests while maintaining security.
Simple CORS Requests
Some cross-origin requests are considered 'simple' by browsers. These don't trigger a special preflight check.
A request is simple if it meets all these conditions:
- Method is
GET,HEAD, orPOST. - Only specific allowed headers (e.g.,
Accept,Accept-Language,Content-Language,Content-Type). Content-Typeheader is limited toapplication/x-www-form-urlencoded,multipart/form-data, ortext/plain.
If simple, the browser sends the request directly, and the server includes CORS headers in its response.
Preflight for Complex Requests
Most requests in modern SPAs, especially those involving OAuth2 tokens, are not simple.
A 'complex' request requires a browser to send a 'preflight' OPTIONS request before the actual request:
- Methods:
PUT,DELETE, or custom methods. - Headers: Custom headers (like
Authorizationfor access tokens). - Content-Type:
application/json(common for APIs).
The server must respond to this OPTIONS request with appropriate CORS headers, indicating if the actual request is allowed.
Key CORS Response Headers
The server communicates its CORS policy through specific HTTP response headers:
Access-Control-Allow-Origin:Specifies which origins are allowed to access the resource. Can be*(wildcard, generally discouraged for security) or a specific origin likehttps://my-app.com.Access-Control-Allow-Methods:Lists the HTTP methods (e.g.,GET, POST, PUT, DELETE) allowed for the resource.Access-Control-Allow-Headers:Indicates which HTTP headers (e.g.,Authorization, Content-Type) are allowed in the actual request.
These headers are crucial for the browser to permit the cross-origin request.
CORS & Credentials
The Access-Control-Allow-Credentials header is another important CORS header.
When set to true, it tells the browser that the server permits cookies, HTTP authentication, or client-side SSL certificates to be included with the cross-origin request.
For OAuth2/OIDC, access tokens are typically sent in the Authorization header, not as cookies. However, if you're using session cookies for authentication or identity management alongside tokens, this header becomes relevant.
CORS in OAuth2/OIDC Flows
CORS is essential at several points in OAuth2/OIDC:
- Token Exchange: Your SPA might need to exchange an authorization code for an access token at the Authorization Server's token endpoint. This is a cross-origin
POSTrequest. - API Access: Once your SPA has an access token, it uses it to call a Resource Server's API (e.g.,
GET /userinfo,POST /orders). This is also a cross-origin request.
The Authorization Server and Resource Server must be configured correctly to allow these requests from your SPA's origin.
Best Practices for CORS
To ensure secure OAuth2/OIDC implementations with CORS:
- Specific Origins: Always specify exact origins (e.g.,
https://my-app.com) inAccess-Control-Allow-Origin, never use*in production. - Limit Methods & Headers: Only allow the HTTP methods and headers that your client applications genuinely need.
- Configure Servers: Ensure your Authorization Server and Resource Server are correctly configured to send the necessary CORS headers.
- Handle Preflights: Make sure your server can respond to
OPTIONSrequests for preflight checks.
Misconfigured CORS can lead to security vulnerabilities, allowing unauthorized access.
CORS Configuration Check
Your SPA at https://my-app.com needs to make a POST request with an Authorization header and Content-Type: application/json to your API at https://api.my-app.com/data. Which CORS response headers should the API server include to allow this?
Recap: CORS & Security
You've learned about Cross-Origin Resource Sharing (CORS) and its vital role in securing modern web applications, especially those leveraging OAuth2 and OpenID Connect.
- CORS relaxes the browser's Same-Origin Policy.
- It enables secure communication between different origins.
- Preflight requests for 'complex' API calls are common with OAuth2 tokens.
- Proper server-side configuration of CORS headers (
Access-Control-Allow-Origin,Access-Control-Allow-Methods,Access-Control-Allow-Headers) is key.
Always follow best practices to prevent misconfigurations that could expose your protected resources.
คำถามที่พบบ่อย
บทเรียน “การแชร์ทรัพยากรข้ามต้นทาง (CORS)” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแชร์ทรัพยากรข้ามต้นทาง (CORS)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแชร์ทรัพยากรข้ามต้นทาง (CORS)”
ทำความเข้าใจนโยบาย CORS ในบริบทของ OAuth2 และ OIDC โดยเฉพาะสำหรับแอปพลิเคชันหน้าเดียวที่เข้าถึงทรัพยากรที่ได้รับการปกป้อง คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การแชร์ทรัพยากรข้ามต้นทาง (CORS)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความยินยอมและประสบการณ์ผู้ใช้
- การแชร์ทรัพยากรข้ามต้นทาง (CORS)
- การออกจากระบบผ่านช่องทางด้านหน้าและด้านหลัง
- โทเค็นที่ผูกกับผู้ส่งด้วย mTLS