ホームページ  >  記事  >  PHPフレームワーク  >  thinkphp withCredentials のクロスドメイン問題解決アイデアの分析

thinkphp withCredentials のクロスドメイン問題解決アイデアの分析

藏色散人
藏色散人転載
2021-02-17 09:22:292050ブラウズ

thinkphp withCredentials のクロスドメイン問題解決アイデアの分析


#thinkphp withCredentials クロスドメインの問題解決のアイデア

クロスドメインとは何かについては、ここでは詳しく説明しません。ここでの主な焦点は thinkphp5.1 です。一般的な解決策について話しましょう
まず、フロントエンドは自分で書いているため、

axios 構成で、次の

withCredentials: true を設定します。 // クロスドメイン リクエスト中に Cookie を送信します

// 创建一个axios
const service = axios.create({
  baseURL: URL , 
  withCredentials: true, // 跨域请求时发送cookie
  timeout: 5000 // request timeout
})
バックエンド設定では、

header("Access-Control-Allow-Origin: *");
が設定されているため、このようなエラーがスローされます

Access to XMLHttpRequest at 'http://store.ink/admin/me?sid=lbn3mpacfb3k1mbehnk9qh8kf3' from origin 'http://vue-admin-web.ink' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
これは ## を設定することを意味します#withCredentials

から true、オリジンを # にすることはできません。オリジンはソース アドレス に設定する必要があります。つまり、http:/ /a.com は http://b.com をリクエストします。 http://a.com は、渡すために起点を http://b.com に設定する必要があります。

最後に、次の構成を参照します。

 $origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
        header("Access-Control-Allow-Origin: $origin");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
        header('Access-Control-Max-Age: 1728000');
もちろん、* の場合は、次のこともできます。

header("Access-Control-Allow-Origin: *");
        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
        header('Access-Control-Max-Age: 1728000');

最初にミドルウェアを定義します

php think make:middleware CrossDomain

<pre class="brush:php;toolbar:false">&lt;?php namespace app\http\middleware; use think\Response; class CrossDomain { public function handle($request, \Closure $next) { $origin = $_SERVER[&amp;#39;HTTP_ORIGIN&amp;#39;] ?? &amp;#39;*&amp;#39;; header(&quot;Access-Control-Allow-Origin: $origin&quot;); header(&amp;#39;Access-Control-Allow-Credentials: true&amp;#39;); header(&amp;#39;Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With&amp;#39;); header(&amp;#39;Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE&amp;#39;); header(&amp;#39;Access-Control-Max-Age: 1728000&amp;#39;); return $next($request); } }</pre>

router.php

<pre class="brush:php;toolbar:false">Route::group(&amp;#39;&amp;#39;, function (){ .... 这里写路由 .... })-&gt;middleware(['CrossDomain']);</pre>

その後、新たな問題が発生します

上記はルーティング ファイルであるため、リクエストされたとき

url

がルートと一致すると、クロスドメイン ミドルウェアが使用されます。ご存知のとおり、delete や put などのメソッドは事前にオプション リクエストを開始します。つまり、ルーティング ファイルと一致せず、クロスドメイン ミドルウェアが使用されません。は使用できません。したがって:

エラー例外引き継ぎの定義 https://www.kancloud.cn/manual/thinkphp5_1/354092#_42

....
public function render(Exception $e)
{
    # 这里来处理跨域问题 
    $origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
    header("Access-Control-Allow-Origin: $origin");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
    header('Access-Control-Max-Age: 1728000');
    $type = request()->isAjax() ? 'json' : "html";
    $response = \think\response\Json::create([], $type, 200, []);
    return $response; # response  // 在异常处理接管中,必须返回的是一个人response响应, 而不是 `throw new `抛出一个响应
}
...

が完了しました。

以上がthinkphp withCredentials のクロスドメイン問題解決アイデアの分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はsegmentfault.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。