【问题标题】:Keycloak does not refresh the tokenKeycloak 不刷新令牌
【发布时间】:2021-01-22 07:27:09
【问题描述】:

我正在尝试刷新令牌,但 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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-03-29
      • 2019-04-21
      • 2017-07-18
      • 2020-08-14
      • 2021-05-13
      • 2021-09-29
      • 2018-12-08
      • 2020-09-01
      • 2022-01-21
      相关资源
      最近更新 更多