如何在 spring oauth2 的 ClientRegistration 中设置任何属性

How to set any attribute in ClientRegistration in spring oauth2

因为我不能在 application.yml 中保密客户,所以它被保存在保险库中并从那里得到解决。但是,我可以看到 ClientRegistration 是最终的 class ,因此一旦 bean 已经初始化,它的客户端密码就不能在以后设置。

在这种情况下,我如何设置秘密并在所有引用的 bean 中使用 ClientRegistration 的新对象。

我正在尝试实现类似下面的内容,但不知道如何在 webclient 或其他参考位置设置 enrichedClientRegistration。

@Slf4j
@Configuration
public class WebClientConfig {

    @Bean
    WebClient authWebClient(ClientRegistrationRepository clientRegistrations,
                                    OAuth2AuthorizedClientRepository authorizedClients,
                                    PasswordResolver passwordResolver) {
        var clientRegistration = clientRegistrations.findByRegistrationId("myApp");
        log.info("Before client secret is {}",clientRegistration.getClientSecret());
        var clientSecret = passwordResolver.resolve(clientRegistration.getClientSecret());
        log.info("Resolved client secret is {}", clientSecret);
        var enrichedClientRegistration=ClientRegistration.withClientRegistration(clientRegistration)
                .clientSecret(clientSecret)
                .build();
        log.info("After client secret is {}",clientRegistrations.findByRegistrationId("myApp").getClientSecret());
        var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
        oauth.setDefaultClientRegistrationId("myApp");

        return WebClient.builder()
                .apply(oauth.oauth2Configuration())
                .build();
    }
}

由于 ClientRegistration 是注入到 ClientRegistrationRepository 中的最终 class,因此您需要按照 spring 文档中给出的示例完全覆盖 ClientRegistrationRepository。

https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/jc.html#jc-oauth2login-completely-override-autoconfiguration