正在显示
1 个修改的文件
包含
14 行增加
和
17 行删除
| @@ -15,22 +15,19 @@ class EnableCrossRequestMiddleware | @@ -15,22 +15,19 @@ class EnableCrossRequestMiddleware | ||
| 15 | */ | 15 | */ |
| 16 | public function handle($request, Closure $next) | 16 | public function handle($request, Closure $next) |
| 17 | { | 17 | { |
| 18 | - $response = $next($request); | ||
| 19 | - $origin = $request->server('HTTP_ORIGIN') ?: ''; | ||
| 20 | -// $allow_origin = [ | ||
| 21 | -// 'http://localhost:8080', | ||
| 22 | -// ]; | ||
| 23 | -// if (in_array($origin, $allow_origin)) { | ||
| 24 | - $header = [ | ||
| 25 | -// 'Access-Control-Allow-Origin' => $origin, | ||
| 26 | - 'Access-Control-Allow-Origin' => '*', | ||
| 27 | - 'Access-Control-Allow-Headers' => '*', | ||
| 28 | - 'Access-Control-Expose-Headers' => '*', | ||
| 29 | - 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS', | ||
| 30 | - 'Access-Control-Allow-Credentials' => 'true', | ||
| 31 | - ]; | ||
| 32 | - $response->headers->add($header); | ||
| 33 | -// } | ||
| 34 | - return $response; | 18 | + // 指定允许其他域名访问 |
| 19 | + $http_origin = "*"; | ||
| 20 | + if(isset($_SERVER['HTTP_ORIGIN'])){ | ||
| 21 | + $http_origin = $_SERVER['HTTP_ORIGIN']; | ||
| 22 | + } | ||
| 23 | + header("Access-Control-Allow-Origin:".$http_origin); | ||
| 24 | + header('Access-Control-Allow-Methods:POST,GET'); //支持的http 动作 | ||
| 25 | + header('Access-Control-Allow-Credentials: true'); | ||
| 26 | + header('Access-Control-Max-Age: 1000'); | ||
| 27 | + header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization, token'); //响应头 请按照自己需求添加。 | ||
| 28 | + if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') { | ||
| 29 | + exit; | ||
| 30 | + } | ||
| 31 | + return $next($request); | ||
| 35 | } | 32 | } |
| 36 | } | 33 | } |
-
请 注册 或 登录 后发表评论