【问题标题】:Log out from Drupal 8 with rest休息时从 Drupal 8 注销
【发布时间】: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


    【解决方案1】:

    您需要使用logout_token 作为token 查询字符串参数。不要将该标记与通常应用于标头的X-CSRF-Token 混淆。

    您看到的提及“csrf_token”的消息要么是拼写错误,要么只是默认父类的消息,核心开发人员尚未开始调整该 REST Logout 消息。

    【讨论】:

    • 错误是误导,反正我是通过logout_token
    【解决方案2】:

    我在注销方面遇到了很大的困难。 CORS 设置正确,

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

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

    withCredentials: true
    

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

    【讨论】:

    • 你在哪里设置的?在我的 php 脚本中,我在标题中设置了 ''Access-Control-Allow-Credentials: true'(上面的示例),但没有运气。在我的 vue 脚本中也不起作用
    【解决方案3】:

    上述解决方案都不适合我,因为我们使用简单的 oAuth 进行身份验证。

    你应该做的两件事:

    1. 应该是 Get 请求而不是 Post 请求。
    2. 如果您使用简单 OAuth 进行用户身份验证,则不需要注销令牌。使用 Simple oAuth 生成的访问令牌作为 Authorization 标头。

    这是一个示例代码:-

    const drupalLogout = async () => {
      const oauthToken = JSON.parse(localStorage.getItem('drupal-oauth-token'));
      const logoutoken = oauthToken.access_token;
      if (logoutoken) {
        const res = await fetch(`${base_url}/user/logout?_format=json`, {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${logoutoken}`,
          },
        });
        if (res.ok) {
          return true;
        }
      }
    };
    
    

    当用户登录时,我正在使用存储在浏览器 localStorage 中的访问令牌。

    【讨论】:

    • 没有尝试使用 OAuth - 从 Drupal 的“文档”中他们说要使用帖子,我也尝试使用 get,但两者都没有使用
    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多