【发布时间】:2019-11-04 06:15:42
【问题描述】:
从这个link 看起来可以执行注销发送帖子到
http://domain/user/logout?_format=json&token=logout_token
其中 logout_token 是从先前登录返回的值(示例如下)
{
"current_user": {
"uid": "65",
"name": "FooBar"
},
"csrf_token": "WDwkGWulI1qBkwDuHtZX8Hakl4X2T7kKIm5kG5StWng",
"logout_token": "l_6TO3cYldLtOx870LI0cYNUwi0wPNSneUA4eZXcZQk"
}
直截了当,但我遇到了 403 错误。
我将我的 rest User 资源设置如下:
/user/{user}: GET, PATCH, DELETE
/entity/user: POST
methods: GET, POST
formats: json
authentication: basic_auth
我的 Drupal services.yml 包含:
cors.config:
enabled: true
# Specify allowed headers, like 'x-allowed-header'.
allowedHeaders: ['*']
# Specify allowed request methods, specify ['*'] to allow all possible ones.
allowedMethods: ['*']
# Configure requests allowed from specific origins.
allowedOrigins: ['*']
# Sets the Access-Control-Expose-Headers header.
exposedHeaders: false
# Sets the Access-Control-Max-Age header.
maxAge: false
# Sets the Access-Control-Allow-Credentials header.
supportsCredentials: false
尽管 Drupal 和调用者位于两台不同的服务器上(be 和 fe 托管在两台不同的物理服务器上),但 cors 没有问题。
我尝试了两个简单的脚本(php 和 vue js - 他们做同样的事情)并得到相同的结果
PHP
$url = 'domain/user/logout?_format=json&token=' . $token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Access-Control-Allow-Credentials: true'
));
$output = curl_exec($curl);
$info = curl_getinfo($curl);
$response = [
'info' => $info,
'data' => json_decode($output, true)
];
curl_close($curl);
return $response;
Vue JS
import Axios from 'axios'
Vue.prototype.$http = Axios
var URL = 'domain/user/logout?_format=json&token=' + this.logout_token
this.$http.post(URL).then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
})
.finally(() => {
this.loading = false
})
我尝试从邮递员那里触发 post 请求,它成功了,但我注意到它创建了一个 cookie。
有人解决/了解它的工作原理吗?
【问题讨论】:
标签: php rest vue.js drupal-8 logout