refactor: CORS中间件改用\Illuminate\Http\Middleware\HandleCors

This commit is contained in:
xboard 2023-12-01 00:36:09 +08:00
parent e35f81f2c8
commit 1d66023bd1
2 changed files with 1 additions and 29 deletions

View File

@ -2,7 +2,6 @@
namespace App\Http;
use Fruitcake\Cors\HandleCors;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
@ -15,7 +14,7 @@ class Kernel extends HttpKernel
* @var array
*/
protected $middleware = [
\App\Http\Middleware\CORS::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,

View File

@ -1,27 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
class CORS
{
public function handle($request, Closure $next)
{
$origin = $request->header('origin');
if (empty($origin)) {
$referer = $request->header('referer');
if (!empty($referer) && preg_match("/^((https|http):\/\/)?([^\/]+)/i", $referer, $matches)) {
$origin = $matches[0];
}
}
$response = $next($request);
$response->header('Access-Control-Allow-Origin', preg_replace('~(?:^/|/$)~', '', "/".$origin));
$response->header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,HEAD');
$response->header('Access-Control-Allow-Headers', 'Origin,Content-Type,Accept,Authorization,X-Request-With');
$response->header('Access-Control-Allow-Credentials', 'true');
$response->header('Access-Control-Max-Age', 10080);
return $response;
}
}