【问题标题】:How to implement refresh token in Spring Boot如何在 Spring Boot 中实现刷新令牌
【发布时间】:2018-09-27 09:08:06
【问题描述】:

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

谁能帮助我了解如何在 Java Spring Boot 中实现这一点?或者有没有其他方法可以让用户保持登录状态?

【问题讨论】:

  • 按照设计,您的用户没有“登录”,因为 JWT 是无状态的。我建议在尝试使用 java、spring、nodejs 或其他方式实现 JWT 机制和目的之前充分了解它

标签: java spring-boot jwt token


【解决方案1】:

如果你正确配置它,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/

【讨论】:

    【解决方案2】:

    Ref - Spring Boot + Refresh Expired JWT Implementation

    一旦 JWT 过期,用户/系统将调用另一个 url,假设 /refreshtoken。与此请求一起,过期的 JWT 也应该被传递。然后,服务器将返回一个可供用户/系统使用的新 JWT。

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2020-02-17
      • 2015-06-02
      • 1970-01-01
      • 2020-07-17
      • 2021-11-23
      • 2020-11-09
      • 2021-01-19
      • 2021-07-11
      相关资源
      最近更新 更多