[英]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: 因此,结论是:
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.