如何在 Keycloak 中指定刷新令牌的生命周期
How to specify refresh tokens lifespan in Keycloak
Keycloak 刷新令牌的生命周期为 1800 秒:
"refresh_expires_in": 1800
如何指定不同的到期时间?在 Keycloak admin UI 中,只能指定访问令牌生命周期:
刷新令牌的生命周期由 SSO 会话空闲设置控制。 30 分钟 = 30 * 60 = 1800 秒(refresh_expires_in 值)
在 v11.0.3 中,在客户端的高级设置下,没有 SSO 会话空闲设置(不确定这些是否刚刚被重命名、移动,或者是管理界面其他地方可用的领域设置),因此,从默认客户端设置开始,您可以指定 Client Session Max 来控制刷新令牌的生命周期,而无需更改其他持续时间设置(访问令牌生命周期继续如您所愿)。证据:调整设置并检查 refresh_expires 作为响应。
刷新令牌的寿命由领域设置的“令牌”选项卡中的“客户端会话最大值”参数定义。
它也可以在客户端设置页面的“高级设置”菜单下的单个客户端级别覆盖。
如 Keycloak 文档中所述:https://www.keycloak.org/docs/latest/server_admin/#_timeouts
Client Session Max
The maximum time before a refresh token is
expired and invalidated. It allows for the specification of a shorter
timeout of refresh token than session timeout. And it can be
overridden on individual clients. It is an optional configuration and
if not set to a value bigger than 0 it uses the same idle timeout set
in the SSO Session Max configuration.
正如 @Kuba Šimonovský the 在评论中指出的那样,缺少其他重要因素:
Actually, it is much much much more complicated.
TL;DR 可以推断 refresh token lifespan
将等于 (SSO Session Idle
, Client Session Idle
, SSO Session Max
, 和 Client Session Max
).
在花了一些时间研究这个问题之后,现在回头看这个帖子,我觉得 之前的答案不足以详细解释发生了什么(一个甚至可能会争辩说他们实际上是错误的)。
让我们假设现在只有 SSO Session Idle
和 SSO Session Max
:
- 和
SSO Session Max
> SSO Session Idle
在这种情况下 refresh token lifetime
与 SSO Session Idle
相同。为什么?因为如果应用程序空闲 SSO Session Idle
时间,用户将注销,这就是刷新令牌绑定到该值的原因。每当应用程序请求新令牌时,refresh token lifetime
和 SSO Session Idle
倒计时值将再次重置;
- 和
SSO Session Max
<= SSO Session Idle
那么 refresh token lifetime
将与 SSO Session Max
相同。为什么?因为无论用户做什么(即空闲与否),用户都会在 SSO Session Max
时间后注销,因此刷新令牌绑定到该值的原因。
从这里我们得出结论,刷新令牌的生命周期被绑定到两个值中的最低值 SSO Session Idle
和 SSO Session Max
。
这两个值都与单点登录 (SSO) 有关。我们仍然需要考虑领域设置的 Client Session Idle
和 Client Session Max
字段的值,当未设置时,它们分别与 SSO Session Idle
和 SSO Session Max
相同。
如果设置了这些值,在刷新令牌的上下文中,它们将覆盖 SSO Session Idle
和 SSO Session Max
中的值,但仅当它们低于 [=11] 中的值时=] 和 SSO Session Max
.
让我们看下面的例子:SSO Session Idle
= 1800 秒,SSO Session Max
= 10 小时和:
Client Session Idle
= 600 秒和 Client Session Max
= 1 小时。在这种情况下,refresh token lifespan
与 Client Session Idle; 相同
Client Session Idle
= 600 秒和 Client Session Max
= 60 秒。在这种情况下,refresh token lifespan
与 Client Session Max. 相同
Client Session Idle
= 1 天,Client Session Max
= 10 天。在这种情况下,refresh token lifespan
与 SSO 会话空闲; 相同
所以简而言之,您可以推断出 refresh token lifespan
将等于 (SSO Session Idle
、Client Session Idle
、SSO Session Max
和 Client Session Max
之间的最小值).
因此,之前的答案中关于您可以简单地使用 Client Session Max
来控制刷新令牌寿命的说法是错误的。只需看看前面的示例 1) 和 3)。
最后,领域设置中的字段 Client Session Idle
和 Client Session Max
可以被客户端自身中的 Client Session Idle
和 Client Session Max
覆盖,这将影响 refresh token lifespan
特别是对于那个客户。
同样的逻辑适用,但不需要考虑领域设置中的值 Client Session Idle
和 Client Session Max
,需要考虑客户端高级设置中的值。
Keycloak 刷新令牌的生命周期为 1800 秒:
"refresh_expires_in": 1800
如何指定不同的到期时间?在 Keycloak admin UI 中,只能指定访问令牌生命周期:
刷新令牌的生命周期由 SSO 会话空闲设置控制。 30 分钟 = 30 * 60 = 1800 秒(refresh_expires_in 值)
在 v11.0.3 中,在客户端的高级设置下,没有 SSO 会话空闲设置(不确定这些是否刚刚被重命名、移动,或者是管理界面其他地方可用的领域设置),因此,从默认客户端设置开始,您可以指定 Client Session Max 来控制刷新令牌的生命周期,而无需更改其他持续时间设置(访问令牌生命周期继续如您所愿)。证据:调整设置并检查 refresh_expires 作为响应。
刷新令牌的寿命由领域设置的“令牌”选项卡中的“客户端会话最大值”参数定义。
它也可以在客户端设置页面的“高级设置”菜单下的单个客户端级别覆盖。
如 Keycloak 文档中所述:https://www.keycloak.org/docs/latest/server_admin/#_timeouts
Client Session Max
The maximum time before a refresh token is expired and invalidated. It allows for the specification of a shorter timeout of refresh token than session timeout. And it can be overridden on individual clients. It is an optional configuration and if not set to a value bigger than 0 it uses the same idle timeout set in the SSO Session Max configuration.
正如 @Kuba Šimonovský the
Actually, it is much much much more complicated.
TL;DR 可以推断 refresh token lifespan
将等于 (SSO Session Idle
, Client Session Idle
, SSO Session Max
, 和 Client Session Max
).
在花了一些时间研究这个问题之后,现在回头看这个帖子,我觉得 之前的答案不足以详细解释发生了什么(一个甚至可能会争辩说他们实际上是错误的)。
让我们假设现在只有 SSO Session Idle
和 SSO Session Max
:
- 和
SSO Session Max
>SSO Session Idle
在这种情况下refresh token lifetime
与SSO Session Idle
相同。为什么?因为如果应用程序空闲SSO Session Idle
时间,用户将注销,这就是刷新令牌绑定到该值的原因。每当应用程序请求新令牌时,refresh token lifetime
和SSO Session Idle
倒计时值将再次重置; - 和
SSO Session Max
<=SSO Session Idle
那么refresh token lifetime
将与SSO Session Max
相同。为什么?因为无论用户做什么(即空闲与否),用户都会在SSO Session Max
时间后注销,因此刷新令牌绑定到该值的原因。
从这里我们得出结论,刷新令牌的生命周期被绑定到两个值中的最低值 SSO Session Idle
和 SSO Session Max
。
这两个值都与单点登录 (SSO) 有关。我们仍然需要考虑领域设置的 Client Session Idle
和 Client Session Max
字段的值,当未设置时,它们分别与 SSO Session Idle
和 SSO Session Max
相同。
如果设置了这些值,在刷新令牌的上下文中,它们将覆盖 SSO Session Idle
和 SSO Session Max
中的值,但仅当它们低于 [=11] 中的值时=] 和 SSO Session Max
.
让我们看下面的例子:SSO Session Idle
= 1800 秒,SSO Session Max
= 10 小时和:
Client Session Idle
= 600 秒和Client Session Max
= 1 小时。在这种情况下,refresh token lifespan
与 Client Session Idle; 相同
Client Session Idle
= 600 秒和Client Session Max
= 60 秒。在这种情况下,refresh token lifespan
与 Client Session Max. 相同
Client Session Idle
= 1 天,Client Session Max
= 10 天。在这种情况下,refresh token lifespan
与 SSO 会话空闲; 相同
所以简而言之,您可以推断出 refresh token lifespan
将等于 (SSO Session Idle
、Client Session Idle
、SSO Session Max
和 Client Session Max
之间的最小值).
因此,之前的答案中关于您可以简单地使用 Client Session Max
来控制刷新令牌寿命的说法是错误的。只需看看前面的示例 1) 和 3)。
最后,领域设置中的字段 Client Session Idle
和 Client Session Max
可以被客户端自身中的 Client Session Idle
和 Client Session Max
覆盖,这将影响 refresh token lifespan
特别是对于那个客户。
同样的逻辑适用,但不需要考虑领域设置中的值 Client Session Idle
和 Client Session Max
,需要考虑客户端高级设置中的值。