日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

下面給大家介紹thinkphp withCredentials 跨域問題解決思路,希望對需要的朋友有所幫助!


thinkphp withCredentials 跨域問題解決思路

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

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

withCredentials: true // 跨域請求時發(fā)送cookie

// 創(chuàng)建一個axios
const service = axios.create({
    baseURL: URL , 
    withCredentials: true, // 跨域請求時發(fā)送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.

意思大概為 設(shè)置 withCredentials 為 true 時, origin 是不允許為 *的, origin必須設(shè)置為來源的地址

也就是 http://a.com 請求 http://b.com 的時候, 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)然, 為 * 的時候也可以這樣

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

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

然后又有一個新問題

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

故而:

定義一個 錯誤異常接管 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響應(yīng), 而不是 `throw new `拋出一個響應(yīng)
}
...

完成。


分享到:
標(biāo)簽:thinkphp withCredentials 跨域問題解決思路
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定