gpt4 book ai didi

Laravel “CSRF token mismatch” 用于带有 Laravel-cors 和 axios 的 POST

转载 作者:行者123 更新时间:2023-12-03 06:40:06 24 4
gpt4 key购买 nike

我有一个 domain_A 正在运行 Laravel 5.8 引擎在 Web 路由上返回 API。它必须检查源以只为几个域提供服务,包括 domain_B。

Barryvdh/laravel-cors
我安装了 barryvdh/laravel-cors通过 Composer 并配置它全局更新 Kernel.php。这也应该适用于网络路由。

内核.php

protected $middleware = [
...
\Barryvdh\Cors\HandleCors::class,
];

然后我配置 Laravel Cors 使用标准配置作为测试以允许任何域。

/config/cors.php
 return [
'supportsCredentials' => false,
'allowedOrigins' => ['http:www.domain_b.com','https:www.domain_b.com','http:domain_b.com'],
'allowedHeaders' => ['Access-Control-Allow-Origin', 'X-CSRF-TOKEN', 'Content-Type', 'X-Requested-With'],
'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT', 'DELETE']
'exposedHeaders' => [],
'maxAge' => 0,
];

axios 配置是:

(domain_a)/Repository.js
import axios from 'axios/index';

const baseDomain = "https://domain_a";
const baseURL = `${baseDomain}`;

let withCredentials = false;

const token = document.head.querySelector('meta[name="csrf-token"]');

const headers = {
'X-CSRF-TOKEN': token.content,
'Access-Control-Allow-Origin': '*',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
};


export default axios.create({
baseURL,
withCredentials: withCredentials,
headers: headers
});

GET 请求也被过滤了,PUT 请求返回 419 错误为什么?我已经设置了 'allowedMethods' => ['*'] 所以它应该可以工作......我错过了什么?

[ 编辑 ]

在调试时我现在收到此错误...

message: "CSRF token mismatch."



为什么 POST 没有得到 header Token?

我也尝试像这样传递 POST token :
 const token = document.head.querySelector('meta[name="csrf-token"]');
const options = {
headers: {
'Authorization' : 'bearer '+token.content,
}
};
const body = {};
return Repository.post(`${resource}/${$playerId}/${$cozzaloID}`, body, options)

预检 header 响应
 Access-Control-Allow-Headers: authorization, content-type, x-requested-with, x-csrf-token
Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: http://www.******.**
Cache-Control: no-cache, private
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Mon, 01 Jul 2019 05:14:22 GMT
Keep-Alive: timeout=5, max=98
Server: Apache
X-Powered-By: PHP/7.1.30, PleskLin

header 响应 :
Access-Control-Allow-Origin: http://www.xxxxxxx.xx
Cache-Control: no-cache, private
Connection: Keep-Alive
Content-Type: application/json
Date: Mon, 01 Jul 2019 05:14:22 GMT
Keep-Alive: timeout=5, max=97
Server: Apache
Transfer-Encoding: chunked
Vary: Origin,Authorization
X-Powered-By: PHP/7.1.30, PleskLin

header 请求 :
Provisional headers are shown
Accept: application/json, text/plain, */*
Authorization: Bearer jW6pFcVlkKyApCxtZIlfaHDPMSFWCWcbnPPTQ7EJ
Content-Type: application/json
Origin: http://www.xxxxxxx.xx
Referer: http://www.xxxxxx.xx/players/739
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100
Safari/537.36
X-CSRF-TOKEN: jW6pFcVlkKyApCxtZIlfaHDPMSFWCWcbnPPTQ7EJ
X-Requested-With: XMLHttpRequest

关于 的注意事项 token : 应该没问题,因为它和在同一任务中完成的另一个GET请求相同。

最佳答案

请使用 routes/api.php 进行 apis 路由,
不要将 routes/web.php 用于 api。

如果要使用子域,请在以下文件中进行所需的更改:

app/Providers/RouteServiceProvider.php



原来的:
protected function mapApiRoutes() {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}

更新:
protected function mapApiRoutes() {
Route::domain('api.' . env('APP_URL'))
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}

关于Laravel “CSRF token mismatch” 用于带有 Laravel-cors 和 axios 的 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56824676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com