27 lines
787 B
PHP
27 lines
787 B
PHP
<?php
|
|
// app/middleware/Cors.php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\middleware;
|
|
|
|
class Cors
|
|
{
|
|
public function handle($request, \Closure $next)
|
|
{
|
|
// 设置CORS响应头
|
|
header('Access-Control-Allow-Origin: *'); // 允许的源
|
|
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Authorization, Content-Type, X-Requested-With, X-CSRF-Token');
|
|
header('Access-Control-Allow-Credentials: true');
|
|
header('Access-Control-Max-Age: 1728000');
|
|
|
|
// 处理CORS预检请求
|
|
if ($request->isOptions()) {
|
|
return response()->code(200); // 预检请求,允许通过
|
|
}
|
|
|
|
return $next($request); // 继续处理请求
|
|
}
|
|
}
|