宝塔Laravel Sanctum SPA认证CSRF认证失败

laravel + vue前后端分离的项目,laravel给vue提供api,按照laravel文档,使用Laravel Sanctum 进行SPA认证,在本地wamp环境下可以正常运行,但是把项目部署到宝塔lnmp环境后csrf认证就不能通过,请问该怎么办?

laravel端域名:admin.hbhyit.cn
vue端域名:shop.hbhyit.cn

wamp 环境:


宝塔lamp环境:


《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

你这是因为跨域引起的,因为前端和后端使用了不同的域名。可以通过在 Laravel 中引用跨域处理的中间件,或者在 nginx 中设置跨域处理信息即可。

Laravel 处理方案
app/Http/Kernel.php 的中间件配置中,配置 HandleCors 中间件即可,如下:

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

Nginx 处理方案

在 nginx 配置文件中,增加以下跨域处理配置即可:

location / {
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=/$1  last;
        break;
    }
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        return 204;
    }
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET,POST,DELETE,PUT,OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
10个月前 评论
快乐的皮拉夫 (作者) 9个月前
Rache1 9个月前
liuhoupan (楼主) 9个月前
讨论数量: 6

你这是因为跨域引起的,因为前端和后端使用了不同的域名。可以通过在 Laravel 中引用跨域处理的中间件,或者在 nginx 中设置跨域处理信息即可。

Laravel 处理方案
app/Http/Kernel.php 的中间件配置中,配置 HandleCors 中间件即可,如下:

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

Nginx 处理方案

在 nginx 配置文件中,增加以下跨域处理配置即可:

location / {
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=/$1  last;
        break;
    }
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        return 204;
    }
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET,POST,DELETE,PUT,OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
10个月前 评论
快乐的皮拉夫 (作者) 9个月前
Rache1 9个月前
liuhoupan (楼主) 9个月前

file cors默认此路径已经忽略了,怎么还会报跨域问题呢

9个月前 评论
liuhoupan (楼主) 9个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!