网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何解决thinkphpwithCredentials跨域问题-创新互联

小编给大家分享一下如何解决thinkphp withCredentials跨域问题,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

创新互联网站建设公司是一家服务多年做网站建设策划设计制作的公司,为广大用户提供了网站设计、成都网站制作,成都网站设计,1元广告,成都做网站选创新互联,贴合企业需求,高性价比,满足客户不同层次的需求一站式服务欢迎致电。

                                                       下面由thinkphp教程栏目给大家介绍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.

意思大概为 设置withCredentialstrue 时, origin 是不允许为*的, origin必须设置为来源的地址

也就是 http://a.com 请求 http://b.com 的时候, http://a.com 必须设置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');

当然, 为 * 的时候也可以这样

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

router.php

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

然后又有一个新问题

因为如上是走的路由文件,当请求的url匹配路由的时候, 会走跨域中间件, 当大家都知道的是, delete 和 put 等方法是会提前发起一个options请求的, 也就是无法匹配路由文件,无法走跨域中间件

故而:

定义一个 错误异常接管 /tupian/20230522/354092>.... 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跨域问题”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


当前题目:如何解决thinkphpwithCredentials跨域问题-创新互联
当前路径:http://bjjierui.cn/article/idhih.html

其他资讯