|
@@ -14,7 +14,7 @@ use think\Facade\Log;
|
|
|
* 跨域中间件
|
|
|
* Class AllowCrossDomain
|
|
|
*
|
|
|
- * @package \yckj\middleware
|
|
|
+ * @package \yckj\commons\middleware
|
|
|
*/
|
|
|
class AllowCrossDomain
|
|
|
{
|
|
@@ -23,7 +23,7 @@ class AllowCrossDomain
|
|
|
|
|
|
// header头配置
|
|
|
protected $header = [
|
|
|
- "Access-Control-Allow-Origin" => "*", //注意修改这里填写你的前端的域名
|
|
|
+ "Access-Control-Allow-Origin" => "*",
|
|
|
'Access-Control-Max-Age' => 1800,
|
|
|
'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
|
|
|
'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With,content-type,ignore,token', //如果有新增header字段,在这里添加
|
|
@@ -41,6 +41,7 @@ class AllowCrossDomain
|
|
|
|
|
|
/**
|
|
|
* 允许跨域请求
|
|
|
+ *
|
|
|
* @access public
|
|
|
* @param Request $request
|
|
|
* @param Closure $next
|
|
@@ -49,9 +50,12 @@ class AllowCrossDomain
|
|
|
*/
|
|
|
public function handle(Request $request, Closure $next, ?array $header = [])
|
|
|
{
|
|
|
- Log::write('header: ' . implode(' ', $header));
|
|
|
+ Log::debug(__NAMESPACE__ . ': Changing Header: ' . implode(' ', $header));
|
|
|
+
|
|
|
+ // 已有 header 则合并
|
|
|
$header = !empty($header) ? array_merge($this->header, $header) : $this->header;
|
|
|
|
|
|
+ // 调整跨域限制
|
|
|
if (!isset($header['Access-Control-Allow-Origin'])) {
|
|
|
$origin = $request->header('origin');
|
|
|
|
|
@@ -61,6 +65,7 @@ class AllowCrossDomain
|
|
|
$header['Access-Control-Allow-Origin'] = '*';
|
|
|
}
|
|
|
}
|
|
|
+ Log::debug(__NAMESPACE__ . ': Header Changed: ' . implode(' ', $header));
|
|
|
|
|
|
return $next($request)->header($header);
|
|
|
}
|