Compare commits
2 commits
15ad765e2c
...
44c322a506
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44c322a506 | ||
|
|
cc482df902 |
4 changed files with 56 additions and 13 deletions
|
|
@ -40,6 +40,7 @@ class Kernel extends HttpKernel
|
||||||
|
|
||||||
'api' => [
|
'api' => [
|
||||||
\App\Http\Middleware\ValidateHeaders::class,
|
\App\Http\Middleware\ValidateHeaders::class,
|
||||||
|
\App\Http\Middleware\CustomCors::class,
|
||||||
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
|
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
|
||||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||||
],
|
],
|
||||||
|
|
|
||||||
36
app/Http/Middleware/CustomCors.php
Normal file
36
app/Http/Middleware/CustomCors.php
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class CustomCors
|
||||||
|
{
|
||||||
|
public function handle(Request $request, Closure $next): Response
|
||||||
|
{
|
||||||
|
$response = $next($request);
|
||||||
|
|
||||||
|
if (!$response instanceof Response) {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 處理 OPTIONS 請求
|
||||||
|
if ($request->getMethod() === 'OPTIONS') {
|
||||||
|
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE, PATCH');
|
||||||
|
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, X-API-Version, X-Client-ID, Authorization, X-Requested-With, Accept');
|
||||||
|
$response->headers->set('Access-Control-Max-Age', '86400');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 允許所有域名
|
||||||
|
$response->headers->set('Access-Control-Allow-Origin', '*');
|
||||||
|
|
||||||
|
// 當允許所有域名時,不能設置 credentials
|
||||||
|
// $response->headers->set('Access-Control-Allow-Credentials', 'true');
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
config/cors.php
Normal file
19
config/cors.php
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'paths' => ['api/*'],
|
||||||
|
'allowed_methods' => ['*'],
|
||||||
|
'allowed_origins' => ['*'],
|
||||||
|
'allowed_origins_patterns' => [],
|
||||||
|
'allowed_headers' => [
|
||||||
|
'Content-Type',
|
||||||
|
'X-Requested-With',
|
||||||
|
'Authorization',
|
||||||
|
'X-API-Version',
|
||||||
|
'X-Client-ID',
|
||||||
|
'Accept',
|
||||||
|
],
|
||||||
|
'exposed_headers' => [],
|
||||||
|
'max_age' => 0,
|
||||||
|
'supports_credentials' => true,
|
||||||
|
];
|
||||||
|
|
@ -81,19 +81,6 @@ <h2>Endpoints</h2>
|
||||||
|
|
||||||
<h3>Public Routes</h3>
|
<h3>Public Routes</h3>
|
||||||
|
|
||||||
<div class="endpoint">
|
|
||||||
<h4><span class="method get">GET</span> /</h4>
|
|
||||||
<p>Root endpoint that returns the API status.</p>
|
|
||||||
<h5>Response:</h5>
|
|
||||||
<pre><code>{
|
|
||||||
"success": true,
|
|
||||||
"data": {
|
|
||||||
"status": "ok",
|
|
||||||
"version": "1.0"
|
|
||||||
}
|
|
||||||
}</code></pre>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="endpoint">
|
<div class="endpoint">
|
||||||
<h4><span class="method get">GET</span> /api/test</h4>
|
<h4><span class="method get">GET</span> /api/test</h4>
|
||||||
<p>Simple test endpoint to verify API connectivity.</p>
|
<p>Simple test endpoint to verify API connectivity.</p>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue