這篇文章主要介紹了thinkphp中withCredentials如何跨域,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)建站長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為慈利企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),慈利網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
跨域是什么這里就不細(xì)講, 這里主要是thinkphp5.1, 說一下大概的解決思路
首先,因?yàn)榍岸耸亲约簩懙? 在axios
配置中, 我設(shè)置了如下
withCredentials: true // 跨域請求時(shí)發(fā)送cookie
// 創(chuàng)建一個(gè)axios const service = axios.create({ baseURL: URL , withCredentials: true, // 跨域請求時(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è)置 withCredentials
為 true
時(shí), origin 是不允許為 *
的, origin必須設(shè)置為來源的地址
也就是 http://a.com 請求 http://b.com 的時(shí)候, http://a.com 必須設(shè)置origin 為 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');
當(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 app\http\middleware; use think\Response; 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è)新問題
因?yàn)槿缟鲜亲叩穆酚晌募?當(dāng)請求的url
匹配路由的時(shí)候, 會走跨域中間件, 當(dāng)大家都知道的是, delete 和 put 等方法是會提前發(fā)起一個(gè)options請求的, 也就是無法匹配路由文件,無法走跨域中間件
故而:
定義一個(gè) 錯(cuò)誤異常接管 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 // 在異常處理接管中,必須返回的是一個(gè)人response響應(yīng), 而不是 `throw new `拋出一個(gè)響應(yīng) } ...
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“thinkphp中withCredentials如何跨域”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
網(wǎng)站題目:thinkphp中withCredentials如何跨域
轉(zhuǎn)載注明:http://chinadenli.net/article10/pghdgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、定制網(wǎng)站、云服務(wù)器、域名注冊、服務(wù)器托管、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)