Keycloak 不刷新令牌

问题描述 投票:0回答:2

我正在尝试刷新令牌,但 Keycloak 返回

400 Bad Request
错误并显示以下消息:

{
    "error": "invalid_grant",
    "error_description": "Invalid refresh token"
}

我在这样的请求中成功获取了刷新令牌:

curl --location --request POST 'http://localhost:8080/auth/realms/my_realm/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=my_user' \
--data-urlencode 'password=my_password' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=my_client_id' \
--data-urlencode 'client_secret=my_client_secret'

因此我收到带有访问令牌和刷新令牌的 JWT 响应。当我将它们加载到jwt.io时,它们似乎都是有效的。

但是当我尝试使用刷新令牌时,我收到了之前的错误。请求是这样的:

curl --location --request POST 'http://localhost:8080/auth/realms/my_realm/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=my_client_id' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'refresh_token=my_refresh_token' \
--data-urlencode 'client_secret=my_client_secret'

在 Keycloak 的日志中没有任何线索说明问题所在。

可能是什么原因?或者至少,有没有办法从 Keycloak 的响应或日志中获取有关错误原因的更多信息?

编辑:

我已经实现了一个用户存储提供程序 SPI,因此它可以针对外部数据库进行身份验证,但它不会将用户管理到 Keycloak 中。是否需要令牌所有者用户存在于 Keycloak 中才能刷新令牌?

谢谢。

authentication oauth-2.0 jwt keycloak
2个回答
0
投票

最后,这是我的用户存储提供商 SPI 中的一个问题。有必要实现以下方法,因为 Keycloak 从刷新令牌中获取用户 ID 并通过该 ID 查找用户:

public UserModel getUserById(String id, RealmModel realm) {
    StorageId storageId = new StorageId(id);
    String username = storageId.getExternalId();
    return getUserByUsername(username, realm);
}

0
投票

如果您仍在使用 keycloak,这可能是相关的,因为当前在多节点环境中它会丢失会话。 https://github.com/keycloak/keycloak/issues/14040

© www.soinside.com 2019 - 2024. All rights reserved.