【发布时间】:2017-09-07 11:16:57
【问题描述】:
我正在使用 Spring Security 的 OAuth2 服务器实现。我正在尝试使用 OAuth2“密码”授权类型从服务器的 /oauth/token 端点获取 access_token,方法是仅提供用户名和密码以及没有客户端密码的客户端 ID。
只要我在我的 HTTP 请求的 Authorization 标头中提供客户端 ID 和客户端密码,就可以正常工作,如下所示:
curl -u clientid:clientsecret http://myhost ... -d "grant_type=password&username=user&password=pw&client_id=OAUTH_CLIENT"
按照此处的建议:Spring OAuth2 disable HTTP Basic Auth for TokenEndpoint,我设法为/auth/token 端点禁用了 HTTP 基本身份验证。但是当我尝试像这样通过 cURL 获取 access_token 时:
curl http://myhost ... -d "grant_type=password&username=user&password=pw&client_id=OAUTH_CLIENT"
我收到了BadCredentialsException 并且可以看到消息:
身份验证失败:密码与存储的值不匹配
在我的服务器日志中。此时我有点恼火,因为据我了解,此消息仅在用户名和/或密码有问题时才会出现,而不是客户端 ID 和/或密码有问题。在 cURL 命令中额外提供客户端密码后,如下所示:
curl http://myhost ... -d "grant_type=password&username=user&password=pw&client_id=OAUTH_CLIENT&client_secret=SECRET"
一切又好了。
这是否意味着我必须以一种或另一种方式提供客户端密码才能访问/auth/token 端点?
PS:我知道关于安全性,通过 HTTP 基本身份验证保护此端点通常是一个好主意,但在某些用例中人们宁愿不这样做。
编辑:
我似乎找到了一种省略客户端密码的方法。这是我的 OAuth2 服务器配置(注意对 allowFormAuthenticationForClients() 和 autoApprove(true) 的调用):
@Configuration
@EnableAuthorizationServer
class OAuth2Config extends AuthorizationServerConfigurerAdapter {
private final AuthenticationManager authenticationManager;
public OAuth2Config(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(this.authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauth) throws Exception {
// allows access of /auth/token endpoint without HTTP Basic authentication
oauth.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("acme")
.autoApprove(true) // <- allows for client id only
.authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("openid");
}
}
编辑二:
这里的问题:Spring Security OAuth 2.0 - client secret always required for authorization code grant 与此问题密切相关,但处理的是 OAuth2 授权类型“授权代码”,这会导致与授权类型“密码”不同的工作流程。
【问题讨论】:
标签: spring spring-security oauth spring-security-oauth2