4

我从 Spring OAuth2 开始。到目前为止一切顺利,我已经通过配置保护了我的应用程序。但是我有一个问题,我的客户端不支持 HTTP 基本授权。

有没有办法为 /oauth/token 端点禁用 HTTP 基本身份验证?我想在 JSON 正文或请求标头中发送 client_id 和 client_secret。

我想工作的 curl 示例:

curl -X POST -H "Content-Type: application/json" -d '{
"username": "user",
"password": "pass",
"grant_type": "password",
"client_id": "test-client-id",
"client_secret": "test-client-secret"
}' "http://localhost:9999/api/oauth/token"

或者

curl -X POST -H "Content-Type: application/json" -H "X-Client-ID: test-client-id" -H "X-Client-Secret: test-client-secret" -d '{
"username": "user",
"password": "pass",
"grant_type": "password"
}' "http://localhost:9999/api/oauth/token"
4

1 回答 1

19

我终于弄明白了。如何禁用 HTTP Basich Auth 的方法是为客户端启用表单身份验证。只需将这些行添加到您的配置中。

@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
  @Override
  public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.allowFormAuthenticationForClients();
  }
}

现在您将能够像这样向 TokenEndpoint 发送成功的请求:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'username=user_username&password=user_password&grant_type=password&client_id=your_trusted_client_id&client_secret=your_trusted_client_secret' "http://localhost:8080/oauth/token"

但是仍然存在 ContentType application/json 问题。有人对此有解决方案吗?

于 2016-07-03T20:05:49.903 回答