Spring-Security-Oauth2:访问此资源需要完全身份验证

Har*_*ara 30 java unauthorized oauth-2.0 spring-security-oauth2

我正在尝试使用spring-security-oauth2.0基于Java的配置.我的配置已完成,但是当我在tomcat上部署应用程序并点击/oauth/token访问令牌的url时,会Oauth生成以下错误:

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

我的配置在Git中心,请点击链接

代码很大,所以请参考git.我正在使用chrome postman客户端发送请求.以下是我的要求.

POST /dummy-project-web/oauth/token HTTP/1.1
Host: localhost:8081
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id=abc%40gmail.com&client_secret=12345678 
Run Code Online (Sandbox Code Playgroud)

错误就像,URL是安全的Oauth,但在配置中,我给予访问此URL的所有权限.这个问题究竟是什么?

Gar*_*ryF 22

client_idclient_secret,默认情况下,应该在Authorization头,而不是窗体-urlencoded体.

  1. 连接你的client_idclient_secret它们之间的冒号:abc@gmail.com:12345678.
  2. Base 64编码结果: YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
  3. 设置Authorization标头: Authorization: Basic YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==


man*_*ekq 17

默认情况下,Spring OAuth需要基本的HTTP身份验证.如果要使用基于Java的配置将其关闭,则必须允许客户端的表单身份验证,如下所示:

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


小智 5

原因是默认情况下/oauth/token端点通过基本访问身份验证进行保护.

您需要做的就是将Authorization标题添加到您的请求中.

您可以使用类似于curl发出以下命令的工具轻松测试它:

curl.exe --user abc@gmail.com:12345678 http://localhost:8081/dummy-project-web/oauth/token?grant_type=client_credentials