English 中文(简体)
Vue + Laravel estaitum CSRF 代币配对错配 419 错误
原标题:Vue + Laravel sanctum CSRF token mismatch 419 error

我收到一个"419(未知状态)"错误 信息是"CSRF代号不匹配"

POST http://127.0.0.1:800/login 419(未知状态)

“https://i.stack.imgur.com/9L6si.png' rel=“noreferrer'>CSRF象征性不匹配。

Laravel 服务器:http://127.0.0.1:800

Vue 服务器 : http://localhost:8080

App/Http/Kernel.php 应用/Http/Kernel.php

 api  => [
    LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,
     throttle:api ,
    IlluminateRoutingMiddlewareSubstituteBindings::class,
],

应用/模式/用户.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    //...
}

配置/cors.php

<?php

return [
     paths  => [
         api/* ,
         sanctum/csrf-cookie ,
         register ,
         login ,
    ],
     allowed_methods  => [ * ],
     allowed_origins  => [ * ],
     allowed_origins_patterns  => [],
     allowed_headers  => [ * ],
     exposed_headers  => [],
     max_age  => 0,
     supports_credentials  => true,
];

. env. env

SESSION_DRIVER=cookie
SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost:8080

rc/main.js

axios.interceptors.request.use((config) => {
    config.baseURL =  http://127.0.0.1:8000 
    config.withCredentials = true

    return config
})

src/views/auth/login.vue 观测/观测/下水道/下水道

import axios from  axios 
import { reactive } from  @vue/reactivity ;

export default {
    setup() {

        const credential = reactive({
            email:   ,
            password:   ,
        })

        const login = async () => {
            axios.get( /sanctum/csrf-cookie ).then( async () => {
                let response = await axios.post( /login , credential)
                console.log(response);
            });
        }

        return { login, credential }
    }
};
最佳回答

您的 SANCTUM_STATEUL_DOMAINS 设置为 localhost: 8080 , 但代码的其余部分显示您正在8000而不是8080的端口运行。 如果您更改为8000, 您应该是金色的 。

问题回答

我很难处理同样的问题。经过几次搜索,我登上这一页。看到可行的建议解决方案后,我改变了

SANCTUM_STATEFUL_DOMAINS=localhost:8080

SANCTUM_STATEFUL_DOMAINS=http://localhost:8080

就是这样,效果不错!

根据"https://laravel.com/docs/10.x/sanctum#cors-and-cookies" rel=“不跟随 noreferrer">Laravel Sanctum 文件

axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;

我忘了设置 axios. defaults. withxSRFToken = true; , 之后一切都正常。

I had the same problem and searched for a solution for days. The solution in my case (consider the settings above) was to change the .env to

SESSION_DRIVER=cookie




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签