在 Spring Security OAuth2 的用户名-密码授权中使用刷新令牌请求新的访问令牌 [英] Request new access token using refresh token in username-password grant in Spring Security OAuth2

查看:32
本文介绍了在 Spring Security OAuth2 的用户名-密码授权中使用刷新令牌请求新的访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用用户名-密码授权从我们的身份验证服务器获取访问令牌.我们希望在访问令牌过期之前使用提供的刷新令牌刷新访问令牌,直到用户注销或关闭客户端应用程序.

We're using the username-password grant to obtain an access token from our auth server. We want to refresh the access token before it expires using the provided refresh token until the user logs out or closes the client app.

但是,我找不到任何有关如何发出此刷新令牌请求的示例..

However I just cannot find any examples of how to issue this refresh token request..

为了获得令牌,我们调用如下:

To obtain the token we call something like:

curl -v --data "grant_type=password&username=user&password=pass&client_id=my_client" http://localhost:8080/oauth/token

所以为了刷新我希望调用看起来像这样:

So to refresh I'd expect the call to look like this:

curl -v --data "grant_type=refresh_token&access_token=THE_ACCESS_TOKEN&refresh_token=THE_REFRESH_TOKEN" http://localhost:8080/oauth/token

或者也许

curl -v -H "Authorization: Bearer THE_ACCESS_TOKEN" --data "grant_type=refresh_token&refresh_token=THE_REFRESH_TOKEN" http://localhost:8080/oauth/token

但它只会给我一个 401..

But it will just give me a 401..

哦,是的,也许我需要添加 clientId?我无法使用客户端机密,因为没有(请参阅上面获取令牌的请求).毕竟身份验证是使用用户名和密码完成的..

Oh yeah, maybe I need to add the clientId? I cannot use the client secret, because there is none (see above request to obtain the token). Authentication is done using username and password after all..

我认为我们的服务器配置正确,所以我不会在这里发布.如果我的示例请求之一应该有效并且您需要查看重要的配置部分,我将添加它们.

I think we have the server configuration right, so I'll not post it here. If one of my example requests should work and you need to see the important config parts I'll add them.

谢谢!

推荐答案

正如我所说,我们不使用客户端机密,因为我们不能在 Javascript 客户端应用程序中使用它.在使用用户名密码授权时,无论如何都不需要它.(请参阅我们请求访问令牌的方式).确实,我已经接近解决方案了,终于想通了:

So as I said, we don't use a client secret, because we cannot have that hanging around in the Javascript client app. And it's not needed anyway, when using the username-password grant. (See the way we request the access token). Indeed I was close to the solution and finally figured it out:

curl -v --data "grant_type=refresh_token&client_id=THE_CLIENT_ID&refresh_token=THE_REFRESH_TOKEN" http://localhost:8080/oauth/token

所以不需要访问令牌或客户端密码.

so no need for the access token or the client secret.

总而言之,它感觉足够安全.

Over all it feels safe enough.

  • 我们不会在客户端应用端存储任何机密.
  • 用户总是需要密码才能登录并且只能看到他们的资源.
  • 我们将刷新令牌的有效性限制在一个现实的时间,比如工作日或其他时间,这样即使它被破坏,攻击者的窗口也会受到限制,同时仍然允许用户在很长一段时间内方便地保持与资源服务器的连接会议.

这篇关于在 Spring Security OAuth2 的用户名-密码授权中使用刷新令牌请求新的访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆