Spring Security OAuth - 未为Null资源配置提供程序管理器

Pac*_*ver 12 java curl oauth spring-mvc spring-security-oauth2

我试图使用Spring Secruity的OAuth API从外部发布的API获取访问令牌.

这个curl命令有效(它的内容就是我获取访问令牌所需的全部内容):

curl -X POST \
https://api.app.com/v1/oauth/token \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d'grant_type=client_credentials&client_id=bcfrtew123&client_secret=Y67493012'
Run Code Online (Sandbox Code Playgroud)

运行此curl命令后,能够从外部服务获取访问令牌.

使用Spring Security OAuth API时:

<dependency>
   <groupId>org.springframework.security.oauth</groupId>
     <artifactId>spring-security-oauth2</artifactId>
     <version>2.1.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

设置我的SpringMVC Controller的方法如下:

@RequestMapping(value = "/getAccessToken", method = RequestMethod.POST, consumes="application/x-www-form-urlencoded")
public OAuth2AccessToken getAccessToken(@RequestParam(value="client_id", required=true) String clientId, @RequestParam(value="client_secret", required=true) String clientSecret) throws Exception {
    String tokenUri = "https://api.app.com/v1/oauth/token";

    ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();

    resourceDetails.setAccessTokenUri(tokenUri);
    resourceDetails.setClientId(clientId);
    resourceDetails.setClientSecret(clientSecret);
    resourceDetails.setGrantType("client_credentials");
    resourceDetails.setScope(Arrays.asList("read", "write"));

    DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();

    oauth2RestTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);

    OAuth2AccessToken token = oauth2RestTemplate.getAccessToken();
    return token;
}
Run Code Online (Sandbox Code Playgroud)

当我从本地tomcat实例调用getAccessToken调用时:

access_denied 
error_description=Unable to obtain a new access token for resource 'null'. 
The provider manager is not configured to support it.
Run Code Online (Sandbox Code Playgroud)

问题(S):

  1. 我在这里错过了什么?这需要一些注释吗?是否有未设置或需要的属性?

(请注意,内容类型必须是"application/x-www-form-urlencoded"......)

  1. 如何使用Spring Security OAuth API模拟工作curl命令?

  2. 它可能是我在RequestParameters中设置的默认值吗?

  3. 如果成功,我该如何设置它以便在发出任何请求之前始终预先加载访问令牌?

Abb*_*ese 7

真正的原因是,你正在使用ResourceOwnerPasswordResourceDetails一个"client_credentials"访问令牌请求。我们不能互换ResourceOwnerPasswordResourceDetailsClientCredentialsResourceDetails

ClientCredentialsResourceDetails你需要设置AccessTokenUri, ClientId, ClientSecretgrantType。在ResourceOwnerPasswordResourceDetails,您需要提供Username and PasswordAccessTokenUri, ClientId, ClientSecretGrantType

(一些 authserver 确实接受password没有username and password.. 的令牌请求,但我会说这是错误的)

参考:使用我们自己的解决方案保护现有 API


Osa*_*lla 0

这可能是因为服务器无法识别您发布到该特定网址的内容类型。在您的 CURL 请求中,尝试包含使用 http 标头的自定义控制器的“content-type: application/x-www-form-urlencoded”。 HTTP 标头

此外,您还没有为resourceDetails设置用户名和密码。resourceDetails.setUserName("用户"); resourceDetails.setUserName("密码");

如果这些不起作用,请尝试提取使用 application/x-www-form-urlencoded 编码的请求,并通过 RestTemplate 将其作为字符串传递,您可以获得令牌。

如果您需要额外支持,请告诉我。

尝试下面的代码,该代码给出令牌和字符串响应。

在此输入图像描述