我得到一个419(未知状态)错误消息CSRF令牌不匹配。
邮递http://127.0.0.1:8000/login419(身份不明)
CSRF令牌不匹配。
Laravel服务器:http://127.0.0.1:8000
Vue服务器:http://localhost:8080
app/Http/Kernel。php
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
应用程序/模型/用户。php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
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,
];
.环境
SESSION_DRIVER=cookie
SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost:8080
src/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\u STATEFUL\u域
设置为localhost:8080
,但代码的其余部分显示您在8000端口而不是8080端口上运行。如果你把它改成8000,你应该是金的。
我很难处理同样的问题。经过几次搜索,我登陆了这个页面。看到可能的建议解决方案后,我改变了主意
SANCTUM_STATEFUL_DOMAINS=localhost:8080
到
SANCTUM_STATEFUL_DOMAINS=http://localhost:8080
就这样!它工作得很好!!