如何在Spring Boot中实现刷新令牌

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

我已按照本指南 https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/ 在我的 Web 应用程序中实现访问令牌,并且工作正常。但是,本指南没有提及任何有关刷新令牌的内容。

任何人都可以帮我解决如何在 Java Spring Boot 中实现这一点吗?或者有其他方法可以让用户保持登录状态吗?

java spring-boot jwt token
1个回答
6
投票

Spring 提供了获取新访问令牌的功能(如果您配置正确),即如果

authorizedGrantTypes
包含
"refresh_code"

您应该使用刷新令牌来通过使用令牌端点来获取新的访问令牌,如下所示:

curl -H "Authorization: Bearer [base64encode(clientId:clientSecret)]" "https://yourdomain.com/oauth/token?grant_type=refresh_token&refresh_token=[yourRefreshToken]"

示例:

curl -X POST -H 'Authorization: Basic dGVzdGNsaWVudDpzZWNyZXQ=' -d 'refresh_token=fdb8fdbecf1d03ce5e6125c067733c0d51de209c&grant_type=refresh_token' localhost:3000/oauth/token

{
    "token_type":"bearer",
"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiVlx1MDAxNcKbwoNUwoonbFPCu8KhwrYiLCJpYXQiOjE0NDQyNjI4NjYsImV4cCI6MTQ0NDI2Mjg4Nn0.Dww7TC-d0teDAgsmKHw7bhF2THNichsE6rVJq9xu_2s",
"expires_in":20,
"refresh_token":"7fd15938c823cf58e78019bea2af142f9449696a"
}

如此处所述:https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/

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