在spring-security-oauth2:2.4.0.RELEASE
类中,如OAuth2RestTemplate
、OAuth2ProtectedResourceDetails
和ClientCredentialsAccessTokenProvider
都被标记为弃用。
从这些类的javadoc中,它指向一个弹簧安全迁移指南,它暗示人们应该迁移到核心的security 5项目。然而,我很难找到如何在这个项目中实现我的用例。
所有文档和示例都讨论了如何与第三方OAuth提供程序集成,如果您希望对应用程序的传入请求进行身份验证,并且希望使用第三方OAuth提供程序验证身份。
在我的用例中,我只想用RestTemplate
向受OAuth保护的外部服务发出请求。目前,我使用客户端id和机密创建一个OAuth2ProtectedResourceDetails
,并将其传递给OAuth2RestTemplate
。我还在OAuth2ResTemplate
中添加了一个自定义OAuth2ResTemplate
,它只是向我正在使用的OAuth提供程序所需的令牌请求添加了一些额外的头。
在Spring-Security5文档中,我发现了一个提到自定义令牌请求的部分,但这似乎是在与第三方OAuth提供程序对传入请求进行身份验证的上下文中。目前尚不清楚如何将其与ClientHttpRequestInterceptor
之类的东西结合使用,以确保外部服务的每个传出请求首先获得令牌,然后添加到请求中。
另外,在上面链接的迁移指南中,有对OAuth2AuthorizedClientService
的引用,它说它对于在拦截器中使用是有用的,但是这看起来同样依赖于类似于ClientRegistrationRepository
这样的东西,如果您想要使用它来确保传入的请求经过身份验证,它就会维护第三方提供者的注册。
我是否可以利用Spring-Security5中的新功能注册OAuth提供程序,以获得一个令牌来添加来自我的应用程序的传出请求?
发布于 2022-11-09 22:57:17
这是OAuth2RestTemplate
的简单替代方案。下面的代码片段已经使用Spring 3.0.0-M4
进行了测试,并且不需要application.yml
配置。
SecurityConfig.java
@Bean
public ReactiveClientRegistrationRepository getRegistration() {
ClientRegistration registration = ClientRegistration
.withRegistrationId("custom")
.tokenUri("<token_URI>")
.clientId("<client_id>")
.clientSecret("<secret>")
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.build();
return new InMemoryReactiveClientRegistrationRepository(registration);
}
@Bean
public WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
InMemoryReactiveOAuth2AuthorizedClientService clientService = new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrations);
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations, clientService);
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
oauth.setDefaultClientRegistrationId("custom");
return WebClient.builder()
.filter(oauth)
.filter(errorHandler()) // This is an optional
.build();
}
public static ExchangeFilterFunction errorHandler() {
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
if (clientResponse.statusCode().is5xxServerError() || clientResponse.statusCode().is4xxClientError()) {
return clientResponse.bodyToMono(String.class)
.flatMap(errorBody -> Mono.error(new IllegalAccessException(errorBody)));
} else {
return Mono.just(clientResponse);
}
});
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0-M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<dependencies>
https://stackoverflow.com/questions/58982286
复制相似问题