通过Keycloak中的refresh_token刷新access_token

时间:2018-07-17 16:33:21

标签: java oauth-2.0 keycloak vert.x

如果用户的access_token过期并且用户想要保留登录名,则需要使用户保留在系统中的登录名。如何在Keycloak上使用refresh_token获得新近更新的access_token?

我将vertx-auth上的Keycloak用于auth实现。是否可以使用vert.xvertx-auth的REST API本身刷新access_token?抑或是另一种实现方式?

3 个答案:

答案 0 :(得分:14)

keycloak具有用于使用access_token创建refresh_token的REST API。这是POST endpoint with application/x-www-form-urlencoded

这是它的外观:

Method: POST URL: https://keycloak.example.com/auth/realms/myrealm/protocol/openid-connect/token Body type: x-www-form-urlencoded Form fields:
client_id : <my-client-name> grant_type : refresh_token refresh_token: <my-refresh-token>

这将使用刷新令牌为您提供新的访问令牌。

注意::如果您的刷新令牌已过期,则会引发400个异常,您可以再次使用户登录。

在Postman中查看示例,您可以使用它开发相应的API。

Sample in Postman

答案 1 :(得分:0)

我尝试使用4.8.2.Final,即使先前的访问令牌为“ Bearer”,它也会给出unauthorized_client之后的内容。 然后,我尝试使用Authorization标头中的Basic YXBwLXByb3h5OnNlY3JldA==。 然后它起作用了,但是我仍然不确定我在做正确的事情。

答案 2 :(得分:0)

@maslick 是正确的,您也必须提供客户端密钥,在这种情况下不需要授权标头:

http://localhost:8080/auth/realms/{realm}/protocol/openid-connect/token

enter image description here

如果刷新令牌过期,则返回:

enter image description here

如果您不添加密钥,即使刷新令牌正确,也会得到 401 未授权

enter image description here

相关问题