从 Drupal 8 注销休息
Log out from Drupal 8 with rest
从这个 link 看来可以执行注销发送 post 到
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}: 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 人的 post 请求并且成功了,但我注意到它创建了一个 cookie。
有人 solved/understood 它是如何运作的?
您需要使用 logout_token
作为 token
查询字符串参数。不要将该标记与通常应用于 Headers.
的 X-CSRF-Token
混淆
您看到的提到 'csrf_token' 的消息要么是拼写错误,要么只是默认 Parent Class 的消息,核心开发人员还没有开始调整REST 注销消息尚未发送。
我挣扎着注销。 CORS 设置正确并且
endpoints: http://domain/user/logout?_format=json&token=logout_token
如上所述设置。我唯一缺少的是这一行:
withCredentials: true
然后它开始工作了。我们现在可以登录和注销了!我希望这对在解耦系统中遇到注销问题的其他人有所帮助。
None 以上解决方案对我有用,因为我们使用简单的 oAuth 进行身份验证。
2 件你应该做的事情:
- 应该是 Get 请求而不是 Post 请求。
- 如果您使用简单 OAuth 进行用户身份验证,则不需要注销令牌。使用 Simple oAuth 生成的访问令牌作为授权 header。
这是一个示例代码:-
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 中的访问令牌。
从这个 link 看来可以执行注销发送 post 到
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}: 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 人的 post 请求并且成功了,但我注意到它创建了一个 cookie。
有人 solved/understood 它是如何运作的?
您需要使用 logout_token
作为 token
查询字符串参数。不要将该标记与通常应用于 Headers.
X-CSRF-Token
混淆
您看到的提到 'csrf_token' 的消息要么是拼写错误,要么只是默认 Parent Class 的消息,核心开发人员还没有开始调整REST 注销消息尚未发送。
我挣扎着注销。 CORS 设置正确并且
endpoints: http://domain/user/logout?_format=json&token=logout_token
如上所述设置。我唯一缺少的是这一行:
withCredentials: true
然后它开始工作了。我们现在可以登录和注销了!我希望这对在解耦系统中遇到注销问题的其他人有所帮助。
None 以上解决方案对我有用,因为我们使用简单的 oAuth 进行身份验证。
2 件你应该做的事情:
- 应该是 Get 请求而不是 Post 请求。
- 如果您使用简单 OAuth 进行用户身份验证,则不需要注销令牌。使用 Simple oAuth 生成的访问令牌作为授权 header。
这是一个示例代码:-
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 中的访问令牌。