首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OAuth2RestTemplate的Security 5替换

OAuth2RestTemplate的Security 5替换
EN

Stack Overflow用户
提问于 2019-11-21 19:20:03
回答 5查看 48.5K关注 0票数 56

spring-security-oauth2:2.4.0.RELEASE类中,如OAuth2RestTemplateOAuth2ProtectedResourceDetailsClientCredentialsAccessTokenProvider都被标记为弃用。

从这些类的javadoc中,它指向一个弹簧安全迁移指南,它暗示人们应该迁移到核心的security 5项目。然而,我很难找到如何在这个项目中实现我的用例。

所有文档和示例都讨论了如何与第三方OAuth提供程序集成,如果您希望对应用程序的传入请求进行身份验证,并且希望使用第三方OAuth提供程序验证身份。

在我的用例中,我只想用RestTemplate向受OAuth保护的外部服务发出请求。目前,我使用客户端id和机密创建一个OAuth2ProtectedResourceDetails,并将其传递给OAuth2RestTemplate。我还在OAuth2ResTemplate中添加了一个自定义OAuth2ResTemplate,它只是向我正在使用的OAuth提供程序所需的令牌请求添加了一些额外的头。

在Spring-Security5文档中,我发现了一个提到自定义令牌请求的部分,但这似乎是在与第三方OAuth提供程序对传入请求进行身份验证的上下文中。目前尚不清楚如何将其与ClientHttpRequestInterceptor之类的东西结合使用,以确保外部服务的每个传出请求首先获得令牌,然后添加到请求中。

另外,在上面链接的迁移指南中,有对OAuth2AuthorizedClientService的引用,它说它对于在拦截器中使用是有用的,但是这看起来同样依赖于类似于ClientRegistrationRepository这样的东西,如果您想要使用它来确保传入的请求经过身份验证,它就会维护第三方提供者的注册。

我是否可以利用Spring-Security5中的新功能注册OAuth提供程序,以获得一个令牌来添加来自我的应用程序的传出请求?

EN

Stack Overflow用户

发布于 2022-11-09 22:57:17

这是OAuth2RestTemplate的简单替代方案。下面的代码片段已经使用Spring 3.0.0-M4进行了测试,并且不需要application.yml配置。

SecurityConfig.java

代码语言:javascript
运行
复制
    @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

代码语言:javascript
运行
复制
    <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>
票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58982286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档