Spring OAuth2 禁用 TokenEndpoint 的 HTTP 基本身份验证

Posted

技术标签:

【中文标题】Spring OAuth2 禁用 TokenEndpoint 的 HTTP 基本身份验证【英文标题】:Spring OAuth2 disable HTTP Basic Auth for TokenEndpoint 【发布时间】:2016-11-05 02:48:54 【问题描述】:

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

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

我想工作的例子:

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"

【问题讨论】:

【参考方案1】:

我终于明白了。如何禁用 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 应用程序/json 问题。有人有这个解决方案吗?

【讨论】:

我认为使用 JSON 作为输入格式的问题是,如果您更改它,您的服务器将不再符合 OAuth2,因为 RFC 标准规定请求必须以 url 形式发送-编码格式:tools.ietf.org/html/rfc6749#appendix-B 我不认为 RFC 指定请求中的 Content-Type 必须只有一个选项。提供的解决方案允许并排使用 application/json 和 url 编码。当然应该有一个选项来启用/禁用每个客户端的 JSON。 我阅读的时候并没有很专心,但是我读过的文章在x-www-form-urlencoded 请求上似乎很紧张。见tools.ietf.org/html/rfc6749#section-4.3.2

以上是关于Spring OAuth2 禁用 TokenEndpoint 的 HTTP 基本身份验证的主要内容,如果未能解决你的问题,请参考以下文章

spring security oauth2 禁用基于 jsessionid 的会话

Spring OAuth2 禁用 TokenEndpoint 的 HTTP 基本身份验证

Spring Boot Security - 如果用户未通过 Oauth2 进行身份验证,如何禁用对 swagger ui 页面的端点的访问

CORS 干扰 Spring Security oauth2

在 Spring Boot OAuth2 中跳过 OAuth 用户批准

spring security oauth2 client_credentials 仅流程