当前位置: 首页 > 知识库问答 >
问题:

Spring Security是否提供开箱即用的功能来从资源服务器检索和使用OAuth2令牌

陶胤
2023-03-14

我们有一个Springboot应用程序(比如microservice-a),我们需要添加一个带有OAuth2令牌的HTTP授权头来访问另一个microservice(比如microservice-B)。另外,据说我们可以通过向同一个microservice-B发送带有Grant\u类型、作用域和基本身份验证用户名和密码的HTTP POST请求来检索这个OAuth2令牌。

现在我的疑问是,我们是否有任何来自Spring security的现成支持,可以在该OAuth2令牌过期时自动从microservice-B检索该令牌,并发送后续HTTP请求。或者这根本不是必需的,我应该首先通过从microservice-a发送一个正常的HTTP POST请求来检索OAuth2令牌,然后发送后续请求。(这样,每当我想发送请求或保存令牌并在到期时检索它时,我可能必须检索OAuth2令牌)

Microservice-A是“客户端”Microservice-B是“资源服务器”

共有1个答案

羊舌昆杰
2023-03-14

使用组织找到了解决方案。springframework。启动:spring-boot-starter-oauth2-client。以下是配置基于OAUTH2的WebClient所需的配置,该WebClient可以自动从auth server(Microservice-B)和access resource server(Microservice-B)获取OAUTH2令牌。

application.properties

oauth2.client.registration.pgw.scope=all
oauth2.client.registration.pgw.client-id=client
oauth2.client.registration.pgw.client-secret=secret
oauth2.client.provider.pgw.token-uri=http://localhost:8082/oauth/token

OAuth2Client配置。Java语言

@Configuration
public class Oauth2ClientConfiguration {
        
    @Bean
    public ReactiveClientRegistrationRepository getRegistration(
            @Value("${oauth2.client.provider.pgw.token-uri}") String tokenUri,
            @Value("${oauth2.client.registration.pgw.client-id}") String clientId,
            @Value("${oauth2.client.registration.pgw.client-secret}") String clientSecret,
            @Value("${oauth2.client.registration.pgw.scope}") String scope) {
        
        ClientRegistration registration = ClientRegistration
                                                .withRegistrationId(OAUTH2_CLIENT_REGISTRATION_ID)
                                                .tokenUri(tokenUri)
                                                .clientId(clientId)
                                                .clientSecret(clientSecret)
                                                .authorizationGrantType(CLIENT_CREDENTIALS)
                                                .scope(scope)
                                                .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(OAUTH2_CLIENT_REGISTRATION_ID);
        
        return WebClient.builder().filter(oauth).build();
    }

}

然后我们可以访问使用这个WebClient bean向资源服务器发送HTTP请求,资源服务器将负责获取和管理OAUTH2令牌

webClient.get().uri("http://localhost:8080/api/private").retrieve().bodyToMono(String.class).block();
 类似资料:
  • 我正在阅读这个教程页面:https://javaee.github.io/tutorial/security-intro006.html这解释了我应该可以通过以下网址查看主页(index.html)的SSL版本:https://localhost:8181。但是,当我将浏览器指向这个地址时,我收到一个错误,如图1所示。 我觉得第一次配置这个可能需要几个小时。有没有人有教程可以帮助我在Glassfi

  • 问题内容: 我们已经设置了OAuth2授权服务器,因此我需要创建一个相应的资源服务器(单独的服务器)。我们计划使用Spring Security OAuth2项目。他们关于设置资源服务器的文档: https://github.com/spring-projects/spring-security- oauth/wiki/oAuth2#resource-server-configuration 应该

  • 我们已经设置了一个OAuth2授权服务器,所以我需要创建一个相应的资源服务器(单独的服务器)。我们计划使用Spring Security OAuth2项目。他们关于设置资源服务器的文档: https://github.com/spring-projects/spring-security-oauth/wiki/oauth2#资源-服务器-配置 应该指向令牌处理bean。然而,令牌处理似乎是由服务器

  • 试图让UserDetailsService为我设置的oauth2资源服务器工作。我能够成功地对jwt进行身份验证,但是我所做的一切似乎都无法让它调用loadUserByUsername方法。它最初使用的是SAML,并且可以工作,但现在我切换到了Oauth2,我无法让它工作。 我在google上发现,我只需要用@service将类注册为bean,spring就会把它捡起来,但它不起作用。我还尝试通过

  • 如何使用spring oauth2和DB实现资源服务器和身份验证服务器,以及如何使用以下模式:https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql 测试数据会很棒!!!

  • 问题内容: 我正在尝试使用HTML5功能。它可以使用IE8完成,但是当我尝试使用它时,却出现了JavaScript错误 所以我的问题是:IE8 可以直接使用吗?这是我的代码: 问题答案: 它确实支持,尽管您需要处于IE8模式(这在IE7模式下不起作用)。 要检查您是否在IE8模式下工作,请加载开发人员控制台。在顶部,确保已选择IE8模式。标准模式也不错。 您还需要确保的一件事是您正在使用HTML5