我想使用客户端凭据授权生成访问令牌https://docs.spring.io/spring-security/site/docs/5.4.6/reference/html5/#oauth2client-客户信誉补助金
我们使用的是SpringSecurity 5.4.6
我已导入这些依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
这是一个资源服务器,它还必须根据cron计划发出服务器到服务器的身份验证请求。这就是为什么我需要 resource-server
及 oauth2-client
依赖关系。
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://auth.pingone.com/...
client:
provider:
pingidentity:
token-uri: https://auth.pingone.com/.../as/token
registration:
pingidentity:
client-id: "69bxxxxx..."
client-secret: "Xxxxx"
scope: openid
authorization-grant-type: "client_credentials"
@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository ) {
OAuth2AuthorizedClientProvider authorizedClientProvider =
OAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials()
.build();
DefaultOAuth2AuthorizedClientManager authorizedClientManager =
new DefaultOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository );
authorizedClientManager.setAuthorizedClientProvider( authorizedClientProvider );
return authorizedClientManager;
}
protected void setSecurityContext() {
OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId( "pingidentity" )
.principal( "69bbfc70-8fcf-4740-a11a-b86562d1c9f8" )
// .attributes( attrs -> {
// attrs.put( HttpServletRequest.class.getName(), servletRequest );
// attrs.put( HttpServletResponse.class.getName(), servletResponse );
// } )
.build();
OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize( authorizeRequest );
OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
}
我得到了这个错误:java.lang.illegalargumentexception:servletrequest不能在org.springframework.util.assert.notnull(assert.java:201)处为null,在org.springframework.security.oauth2.client.web.defaultoauth2authorizedclientmanager.authorize(defaultoauth2authorizedclientmanager.java:144)处为nullcom.edlogics.heathrow.api.scheduling.jobs.abstractschedulerjob.setsecuritycontext(abstractschedulerjob.java:42)位于com.edlogics.heathrow.api.scheduling.jobs.CheckFormisingCacheEntriesJob.execute(CheckFormisingCacheEntriesJob.java:32),位于org.quartz.siml.simplethreadpool$workerthread.run(simplethreadpool.java:573)
如何在web请求之外使用客户端凭据授权?
暂无答案!
目前还没有任何答案,快来回答吧!