php - 从 Drupal 8 注销并休息

标签 php rest vue.js drupal-8 logout

从此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 错误。

我将我的其余用户资源设置如下:

/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;

VueJS

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
})

我尝试触发 postman 的发布请求,它成功了,但我注意到它创建了一个 cookie。

有人解决/理解它是如何工作的吗?

最佳答案

我在注销时遇到了很大的困难。 CORS 设置正确并且

endpoints: http://domain/user/logout?_format=json&token=logout_token

已按上述设置。我唯一缺少的是这一行:

withCredentials: true

然后它就开始工作了。我们现在可以登录和退出了!我希望这对在解耦系统中遇到注销问题的其他人有所帮助。

关于php - 从 Drupal 8 注销并休息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56702693/

相关文章:

java - 如何在 Spring/Java 中使用 JSON 未命名值来使用 REST?

java - 解码 Json 对象时出现 "Content is not allowed in prolog"

javascript - 如何更新此对象内的 vue 对象值

javascript - 如何在vue渲染函数中为自定义组件声明v-model?

php - 我如何告诉 CodeIgniter ActiveRecord 不要转义我的插入和更新查询?

php - 如果在循环中包含 PHP 中的文件,每次在循环中运行时它都会访问该文件吗?

php - MYSQL/PHP 基于另一个表中的文本片段的联接

swift - 等待返回主 block ,直到其封装的子 block 完成

php - 听表单提交的钩子(Hook)是什么?

javascript - 组件的 Vue.js 接口(interface)