我在SPA项目中使用laravel-airlock auth。
根据文件https://laravel.com/docs/master/airlock,在loggin中需要以下代码:
axios.get('/airlock/csrf-cookie').then(response => {
// Login...
});
但是,当我对代码进行注释时,登录脚本仍然可以访问API控制器。
这是我下面的代码:
// axios.get('/airlock/csrf-cookie').then(response => {
axios.post('api/login', {
email: this.email,
password: this.password
})
.then(response => {
console.log(response);
})
.catch(error => {
self.errors.push(error.response.data.message)
});
// })
那么axios.get('/airlock/csrf-cookie')
有什么用呢?
这是因为在为SPA应用程序(HTML/JS/CCS(加载ass时,您已经进入了域,此时Laravel CSRF保护已初始化。
假设您的API位于不同的域中,那么您必须使用对/airlock/csrf-cookie
的get调用来初始化CSRF保护