在 Spring Security Oauth2 版本 2.0.7.RELEASE 中请求多个范围

Requesting multiple scopes in Spring Security Oauth2 version 2.0.7.RELEASE

我们有一个正在使用 spring-security-oauth2:1.0 的应用程序。我试图将其更改为较新的版本,spring-security-oauth2:2.0.7.RELEASE。如果我没有指定 scope 或者如果我指定我的单一范围,应用程序工作正常。我在请求多个范围时遇到问题,例如 read,write,它曾经在以前的版本中工作。

我请求的客户端拥有所有 read,write and trust 权限。

当我使用 spring-security-oauth2:1.0 时,为了获得令牌,我曾经像

这样进行 get 调用

http://localhost:8080/oauth/token?grant_type=password&client_id=ws&client_secret=secret&scope=read,write&username=user@abc.com&password=temp123

如果您看到范围参数 scope=read,write,通过这种方式请求,我曾经获得范围为 read and write 的令牌。

如果我尝试对 Oauth2 版本 2.0.7.RELEASE(虽然有 POST 请求)做同样的事情,我会得到 Invalid Scope 异常,因为 tokenRequest 正在接受read,write 作为一个范围。我请求的客户端具有 read,write and trust 权限,但 read,write 不是其中之一。

如果我尝试使用 scope=writescope=read,它工作正常,因为 readwrite 是客户端范围的一部分。

如果我想在 OAuth2 2.0.7.RELEASE 中请求多个范围,我该怎么做?

我找到了正确的方法。您必须使用 + 来分隔范围,而不是逗号分隔范围。

例如:read+writewrite+trust

所以下面的 POST 请求工作正常。

http://localhost:8080/oauth/token?grant_type=password&client_id=ws&client_secret=secret&scope=read+write&username=user@abc.com&password=temp123

希望对其他人有所帮助:)