Spring安全性Oauth2资源所有者密码凭证授权

use*_*077 4 security passwords spring oauth

刚刚在我的eclipse IDE中安装了spring security oauth2.我试图实现的服务将由第二方用户通过其安装的应用程序使用,因此我选择使用密码授予类型.根据我对Oauth2的理解,以下请求应该适用于演示sparklr2服务,而无需我使用用户名和密码参数.即

POST http://localhost:8080/sparklr2/oauth/token?grant_type=password&client_id=my-trusted-client&scope=trust&username=marissa&password=koala
Run Code Online (Sandbox Code Playgroud)

但我一直在

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
Run Code Online (Sandbox Code Playgroud)

我在这个请求中遗漏了什么,或者我需要在回购中启用一些东西

alo*_*_50 7

虽然问题有点陈旧,但我想就此发表我的研究结果.

确实,对于Spring OAuth,您需要指定一个客户端ID才能访问令牌端点,但是没有必要为密码授予类型指定客户端密钥.

下一行是没有任何客户端密钥的密码授予类型的授权服务器客户端的示例.你只需要在你的课程中添加它们AuthorizationServerConfigurerAdapter:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {    
        clients.inMemory()
            .withClient("clientId")
            .authorizedGrantTypes("password")
            .authorities("ROLE_CLIENT")
            .scopes("read");
    }
 }
Run Code Online (Sandbox Code Playgroud)

此外,确实可以避免令牌端点中的HTTP基本身份验证,并将我们的client_id添加为POST调用中的另一个请求参数.

要实现这一点,您只需要将这些行添加到与以前相同的类中:

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.allowFormAuthenticationForClients();
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以通过这种方式调用令牌端点,这看起来更符合Stormpath网页中的示例

POST http://localhost:8080/sparklr2/oauth/token?grant_type=password&client_id=clientId&scope=read&username=marissa&password=koala
Run Code Online (Sandbox Code Playgroud)


Chr*_*e L 6

似乎Spring OAuth2不支持无秘密OAuth2客户端的密码授予类型.这可能是根据OAuth2规范:http://tools.ietf.org/html/rfc6749#section-4.3.2,虽然规范似乎表明客户端身份验证并不总是必需的(这对我来说不是很清楚) ).

这意味着当使用密码授予类型调用令牌端点时,您需要传入客户端ID和密码(使用基本身份验证),这也意味着如果客户端没有秘密,则无法使用密码授予(您可能仍然可以使用隐式流程).

在sparklr2中,my-trusted-client没有定义的秘密,这就是您的呼叫失败的原因.

如果要查看密码授予类型,您可以尝试my-trusted-client-with-secret:

curl -u my-trusted-client-with-secret:somesecret "http://localhost:8080/sparklr2/oauth/token?grant_type=password&username=marissa&password=koala"
Run Code Online (Sandbox Code Playgroud)