first commit
This commit is contained in:
26
app/common/middleware/Cors.php
Normal file
26
app/common/middleware/Cors.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?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); // 继续处理请求
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user