简体   繁体   English

如何为我的OAuth2客户端实施client_credentials授予

[英]How do I implement the client_credentials grant for my OAuth2 Client

I recently implemented the client_credentials grant for my OAuth2 provider, which is based on Spring security OAuth2. 我最近为我的OAuth2提供者实现了client_credentials授予,该提供者基于Spring安全性OAuth2。 Than I moved to the client to implement the mechanism there. 然后我搬到了客户端,以在那里实现该机制。 I added the @EnableOAuth2Client annotation and set the following configuration: 我添加了@EnableOAuth2Client批注并设置以下配置:

spring:
  oauth2:
    client:
      id: myResource
      clientId: myClientId
      clientSecret: myClientSecret
      accessTokenUri: http://localhost:8080/oauth/token
      grantType: client_credentials

I'm not really clear on why I need to add the id setting. 我不清楚我为什么需要添加id设置。 According to the error message the provider manager needs to support it. 根据错误消息,提供程序管理器需要支持它。 This is the error I'm getting: 这是我得到的错误:

Unable to obtain a new access token for resource 'myResource'. 无法获取资源“ myResource”的新访问令牌。 The provider manager is not configured to support it. 提供者管理器未配置为支持它。

After searching the internet for a while I found that I need to add a global servlet of the DelegatingFilterProxy that delegates to a bean named "oauth2ClientContextFilter" 在互联网上搜索了一会儿之后,我发现我需要添加一个DelegatingFilterProxy的全局servlet,该servlet委托给一个名为“ oauth2ClientContextFilter”的bean。

https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/config/annotation/web/configuration/EnableOAuth2Client.java https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/config/annotation/web/configuration/ EnableOAuth2Client.java

I found some implementations on how to do that but they all use XML instead of annotation to set their configuration. 我发现了一些实现方法,但是它们都使用XML而不是注释来设置其配置。

<filter>
  <filter-name>myFilter</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>myFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#delegating-filter-proxy http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#delegating-filter-proxy

So in conclusion: 因此,结论是:

  1. Is this the right approach to setting up the client_credentials grant for my OAuth2 Client? 这是为我的OAuth2客户端设置client_credentials授予的正确方法吗?
  2. How can I set the DelegatingFilterProxy using annotation as stated in the @EnableOAuth2Client class? 如何使用@ EnableOAuth2Client类中所述的注释设置DelegatingFilterProxy?
  3. Why does it need to delegate to a bean named 'oauth2ClientContextFilter'? 为什么需要委托给名为“ oauth2ClientContextFilter”的bean?

Thanks in advance 提前致谢

So I managed to fix my problem. 所以我设法解决了我的问题。
After putting this on hold for a while I gave it another try. 搁置一会后,我再次尝试了一下。
It seems that I'm using the wrong ResourceDetails class for my OAuth2RestTemplate. 看来我的OAuth2RestTemplate使用了错误的ResourceDetails类。
So replacing 所以更换

AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();

with

ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();

fixed the problem. 解决了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring Boot Oauth2 授权服务器中 grant_type=client_credentials 时抛出错误凭据的任何异常 - How to throw any Exceptions for wrong credentials when grant_type=client_credentials in Spring Boot Oauth2 Authorization Server 使用授权类型“ client_credentials”时,OAuth2身份验证失败 - OAuth2 Authentication fails when using grant type “client_credentials” Spring Boot 2 Spring-Security 5 OAuth2 支持 client_credentials grant_type - Spring Boot 2 Spring-Security 5 OAuth2 support for client_credentials grant_type Oauth 2.0 与 grant_type=client_credentials? - Oauth 2.0 with grant_type=client_credentials? 春季安全oauth2 client_credentials仅流 - spring security oauth2 client_credentials flow only Spring OAuth2 client_credentials与预先认证的用户结合使用 - Spring OAuth2 client_credentials in combination with preauthenticated user PCF应用程式到应用程式Oauth 2 grant_type =&#39;client_credentials&#39;和范围uaa.resources。 本地测试 - PCF App to App Oauth 2 grant_type = 'client_credentials' and scope uaa.resources. Local testing Spring Boot 2和OAuth2客户端凭证不受支持的授予类型 - Spring Boot 2 and OAuth2 client-credentials unsupported grant type Spring OAuth2-客户端证书授予类型中的用户信息 - Spring OAuth2 - User info in Client Credentials grant type 为什么 Spring Boot WebClient OAuth2 (client_credentials) 要求为每个请求提供一个新令牌? - Why Spring Boot WebClient OAuth2 (client_credentials) asks for a new token for each request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM