首页 > 解决方案 > 休息时从 Drupal 8 注销

问题描述

从这个链接看起来可以执行注销发送帖子到

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;

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。

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

标签: phprestvue.jsdrupal-8logout

解决方案


我在注销时非常挣扎。CORS 设置正确,并且

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

如上所述设置。我唯一缺少的是这一行:

withCredentials: true

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


推荐阅读