站長(zhǎng)資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

解析thinkphp withCredentials跨域問(wèn)題解決思路

解析thinkphp withCredentials跨域問(wèn)題解決思路

thinkphp withCredentials 跨域問(wèn)題解決思路

跨域是什么這里就不細(xì)講, 這里主要是thinkphp5.1, 說(shuō)一下大概的解決思路

首先,因?yàn)榍岸耸亲约簩懙? 在axios配置中, 我設(shè)置了如下

withCredentials: true // 跨域請(qǐng)求時(shí)發(fā)送cookie

// 創(chuàng)建一個(gè)axios const service = axios.create({   baseURL: URL ,    withCredentials: true, // 跨域請(qǐng)求時(shí)發(fā)送cookie   timeout: 5000 // request timeout })

在后端的配置中,配置的是

header("Access-Control-Allow-Origin: *");

故而拋出了這樣一個(gè)錯(cuò)誤

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.

意思大概為 設(shè)置 withCredentialstrue 時(shí), origin 是不允許為 *的, origin必須設(shè)置為來(lái)源的地址

也就是 http://a.com 請(qǐng)求 http://b.com 的時(shí)候, http://a.com 必須設(shè)置origin 為 http://b.com 才能通過(guò)

最后參閱配置如下

 $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');

當(dāng)然, 為 * 的時(shí)候也可以這樣

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');

首先 定義個(gè)中間件 php think make:middleware CrossDomain

<?php namespace apphttpmiddleware;  use thinkResponse;   class CrossDomain {     public function handle($request, Closure $next)     {         $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');          return $next($request);     } }

router.php

Route::group('', function (){     ....     這里寫路由     .... })->middleware(['CrossDomain']);

然后又有一個(gè)新問(wèn)題

因?yàn)槿缟鲜亲叩穆酚晌募?當(dāng)請(qǐng)求的url匹配路由的時(shí)候, 會(huì)走跨域中間件, 當(dāng)大家都知道的是, delete 和 put 等方法是會(huì)提前發(fā)起一個(gè)options請(qǐng)求的, 也就是無(wú)法匹配路由文件,無(wú)法走跨域中間件

故而:

定義一個(gè) 錯(cuò)誤異常接管 https://www.kancloud.cn/manual/thinkphp5_1/354092#_42

.... public function render(Exception $e) {     # 這里來(lái)處理跨域問(wèn)題      $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 = thinkresponseJson::create([], $type, 200, []);     return $response; # response  // 在異常處理接管中,必須返回的是一個(gè)人response響應(yīng), 而不是 `throw new `拋出一個(gè)響應(yīng) } ...

完成。

贊(0)
分享到: 更多 (0)
網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
91在线老王精品免费播放| 黑巨人与欧美精品一区| 久久99精品久久久久久噜噜| 日韩亚洲欧洲在线com91tv| 精品国产三级a∨在线观看| 国产精品午夜剧场| 亚洲乱码日产精品BD在线观看| 久久久999久久久精品| 亚洲精品亚洲人成在线观看| 国产精品高清2021在线| 精品一区二区三区电影| 日韩欧美中文字幕公布| 日韩中文字幕免费视频| 日韩有码一区二区| 国产69精品久久久久999三级| 日韩精品中文字幕无码专区| 国产精品∧v在线观看| 亚洲精品色在线网站| 亚洲精品久久无码| 宅男宅女精品国产av天堂| 久久99这里只有精品国产| 国产精品网站在线观看| 97精品国产一区二区三区| 国产精品视频你懂的| 亚洲首页国产精品丝袜| 2020亚洲男人天堂精品| 日韩视频在线精品视频免费观看| 国产精品亚洲片在线va| 亚洲精品免费网站| 亚洲综合精品伊人久久| 国产日产欧产精品精品浪潮| 精品国产一区二区三区AV| 国产精品久久久久久久久久久不卡| 人妻少妇精品久久久久久| 久久精品无码一区二区三区不卡 | 日韩毛片高清在线看| 日韩专区在线观看| 成人国内精品久久久久影院| 一本一本久久a久久精品综合| 亚洲精品456播放| 久久久久久国产精品无码下载|