Spring-Security-Oauth2:访问此资源需要完全身份validation
我正在尝试将spring-security-oauth2.0
与基于Java的配置一起使用。 我的配置已完成,但是当我在tomcat上部署应用程序并点击/oauth/token
url访问令牌时, Oauth
生成以下错误:
Full authentication is required to access this resource unauthorized
我的配置在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
错误就像, Oauth
的URL是安全的,但在配置中,我给予访问此URL的所有权限。 这个问题究竟是什么?
默认情况下, client_id
和client_secret
应该位于Authorization标头中,而不是form-urlencoded主体。
- 连接你的
client_id
和client_secret
,它们之间有一个冒号:abc@gmail.com:12345678
。 - Base 64编码结果:
YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
- 设置授权标题:
Authorization: Basic YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
默认情况下,Spring OAuth需要基本的HTTP身份validation。 如果要使用基于Java的配置将其关闭,则必须允许客户端的表单身份validation,如下所示:
@Configuration @EnableAuthorizationServer protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.allowFormAuthenticationForClients(); } }
原因是默认情况下/oauth/token
端点通过基本访问身份validation进行保护。
您需要做的就是将Authorization
标头添加到您的请求中。
您可以通过发出以下命令,使用curl
工具轻松测试它:
curl.exe --user abc@gmail.com:12345678 http://localhost:8081/dummy-project-web/oauth/token?grant_type=client_credentials
使用Spring OAuth 2.0.7-RELEASE,以下命令适用于我
curl -v -u abc@gmail.com:12345678 -d "grant_type=client_credentials" http://localhost:9999/uaa/oauth/token
它也适用于Chrome POSTMAN,只需在“基本身份validation”选项卡中确保客户端和密码,将方法设置为“POST”并在“表单数据”选项卡中添加授权类型。
在application.properties
设置management.security.enabled=false
为我解决了这个问题。