thinkphp withCredentials 跨域问题解决思路

跨域是什么这里就不细讲, 这里主要是thinkphp5.1, 说一下大概的解决思路

首先,由于前端是本身写的, 在axios配置中, 我设置了以下php

withCredentials: true // 跨域请求时发送cookiehtml

// 建立一个axios
const service = axios.create({
  baseURL: URL , 
  withCredentials: true, // 跨域请求时发送cookie
  timeout: 5000 // request timeout
})

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

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

故而抛出了这样一个错误vue

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必须设置为来源的地址ios

也就是 http://a.com 请求 http://b.com 的时候, http://a.com 必须设置origin 为 http://b.com 才能经过web

最后参阅配置以下

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

固然, 为 * 的时候也能够这样thinkphp

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 CrossDomainjson

<?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.phpaxios

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

而后又有一个新问题

由于如上是走的路由文件,当请求的url匹配路由的时候, 会走跨域中间件, 当你们都知道的是, delete 和 put 等方法是会提早发起一个options请求的, 也就是没法匹配路由文件,没法走跨域中间件后端

故而:

定义一个 错误异常接管 https://www.kancloud.cn/manua...

....
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 `抛出一个响应
}
...

完成

首发: 邓尘锋 http://surest.cn/archives/105/

相关文章
相关标签/搜索