本示例将使用 GraphQL Analytics API 检索 Access 登录事件日志。这些日志对于判断用户为何收到 403 Forbidden 错误特别有用,因为它们能提供仪表板 Access 日志中未显示的额外数据。
以下 API 调用将请求单个 Access 登录事件的日志,并输出所请求的字段。身份验证请求由其 Ray ID 标识,可从向用户显示的 403 Forbidden 页面获取。
你需要插入 <CLOUDFLARE_ACCOUNT_TAG>、<API_TOKEN>1 中的 API 凭据,并替换以下变量为你自己的值:
rayID:分配给身份验证请求的唯一标识符。datetimeStart:要查询的最早事件时间(不早于 2022 年 9 月 16 日)。datetimeEnd:要查询的最晚事件时间。请确保指定的时间范围包含你要查询的登录事件。
echo '{ "query":
"query accessLoginRequestsAdaptiveGroups($accountTag: string, $rayId: string, $datetimeStart: string, $datetimeEnd: string) {
viewer {
accounts(filter: {accountTag: $accountTag}) {
accessLoginRequestsAdaptiveGroups(limit: 100, filter: {datetime_geq: $datetimeStart, datetime_leq: $datetimeEnd, cfRayId: $rayId}, orderBy: [datetime_ASC]) {
dimensions {
datetime
isSuccessfulLogin
hasWarpEnabled
hasGatewayEnabled
hasExistingJWT
approvingPolicyId
cfRayId
ipAddress
userUuid
identityProvider
country
deviceId
mtlsStatus
mtlsCertSerialId
mtlsCommonName
serviceTokenId
}
}
}
}
}",
"variables": {
"accountTag": "<CLOUDFLARE_ACCOUNT_TAG>",
"rayId": "74e4ac510dfdc44f",
"datetimeStart": "2022-09-20T14:36:38Z",
"datetimeEnd": "2022-09-22T14:36:38Z"
}
}' | tr -d '\n' | curl --silent \
https://api.cloudflare.com/client/v4/graphql \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data @- | jq .{
"data": {
"viewer": {
"accounts": [
{
"accessLoginRequestsAdaptiveGroups": [
{
"dimensions": {
"approvingPolicyId": "",
"cfRayId": "744927037ce06d68",
"country": "US",
"datetime": "2022-09-02T20:56:27Z",
"deviceId": "",
"hasExistingJWT": 0,
"hasGatewayEnabled": 0,
"hasWarpEnabled": 0,
"identityProvider": "nonidentity",
"ipAddress": "2a09:bac0:15::814:7b37",
"isSuccessfulLogin": 0,
"mtlsCertSerialId": "",
"mtlsCommonName": "",
"mtlsStatus": "NONE",
"serviceTokenId": "",
"userUuid": ""
}
}
]
}
]
}
},
"errors": null
}你可以将查询结果与 Access 策略进行比对,以了解用户为何被阻止。例如,如果应用要求有效的 mTLS 证书,Access 会阻止上面显示的请求,因为 mtlsStatus、mtlsCommonName 和 mtlsCertSerialId 为空。
-
有关配置和权限的更多信息,请参阅 Configure an Analytics API token。 ↩