如何使用 spring-security-oauth2 和 WebClient 自定义 OAuth2 令牌请求的授权标头?

2024-05-18

我正在尝试通过 WebClient 调用升级到 spring security 5.5.1。 我发现oauth2clientId and secret现在 URL 已编码为AbstractWebClientReactiveOAuth2AccessTokenResponseClient,但我的令牌提供者不支持这一点(例如,如果秘密包含+字符仅当它作为+ not as %2B)。 我知道这被视为spring-security 方面的错误修复 https://github.com/spring-projects/spring-security/issues/9610),但我无法让令牌提供者轻松改变其行为。

所以我试图找到一种方法来解决这个问题。

[文档](https://docs.spring.io/spring-security/site/docs/current/reference/html5/#customizing-the-access-token-request https://docs.spring.io/spring-security/site/docs/current/reference/html5/#customizing-the-access-token-request)关于如何自定义访问令牌请求的内容在您使用 WebClient 配置时似乎并不适用(这是我的情况)。

为了删除 clientid/secret 编码,我必须扩展并复制大部分现有代码AbstractWebClientReactiveOAuth2AccessTokenResponseClient定制WebClientReactiveClientCredentialsTokenResponseClient因为其中大部分具有私有/默认可见性。 我在一个增强问题 https://github.com/spring-projects/spring-security/issues/10042在春季安全项目中。

是否有更简单的方法来自定义令牌请求的授权标头,以跳过 url 编码?


一些围绕定制的 API 肯定还有改进的空间,并且肯定来自社区的这些类型的问题/请求/问题将继续帮助突出这些领域。

关于AbstractWebClientReactiveOAuth2AccessTokenResponseClient特别是,目前无法覆盖内部方法来填充基本身份验证凭据Authorization标头。但是,您可以自定义WebClient用于进行 API 调用。如果在您的用例中可以接受(暂时,在解决行为更改和/或添加自定义选项时),您应该能够在WebClient.

这是一个将创建一个WebClient能够使用一个OAuth2AuthorizedClient:

@Configuration
public class WebClientConfiguration {

    @Bean
    public WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
        // @formatter:off
        ServerOAuth2AuthorizedClientExchangeFilterFunction exchangeFilterFunction =
                new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
        exchangeFilterFunction.setDefaultOAuth2AuthorizedClient(true);

        return WebClient.builder()
                .filter(exchangeFilterFunction)
                .build();
        // @formatter:on
    }

    @Bean
    public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
            ReactiveClientRegistrationRepository clientRegistrationRepository,
            ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
        // @formatter:off
        WebClientReactiveClientCredentialsTokenResponseClient accessTokenResponseClient =
                new WebClientReactiveClientCredentialsTokenResponseClient();
        accessTokenResponseClient.setWebClient(createAccessTokenResponseWebClient());

        ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
                ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
                        .clientCredentials(consumer ->
                                consumer.accessTokenResponseClient(accessTokenResponseClient)
                                        .build())
                        .build();

        DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
                new DefaultReactiveOAuth2AuthorizedClientManager(
                        clientRegistrationRepository, authorizedClientRepository);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
        // @formatter:on

        return authorizedClientManager;
    }

    protected WebClient createAccessTokenResponseWebClient() {
        // @formatter:off
        return WebClient.builder()
                .filter((clientRequest, exchangeFunction) -> {
                    HttpHeaders headers = clientRequest.headers();
                    String authorizationHeader = headers.getFirst("Authorization");
                    Assert.notNull(authorizationHeader, "Authorization header cannot be null");
                    Assert.isTrue(authorizationHeader.startsWith("Basic "),
                            "Authorization header should start with Basic");
                    String encodedCredentials = authorizationHeader.substring("Basic ".length());
                    byte[] decodedBytes = Base64.getDecoder().decode(encodedCredentials);
                    String credentialsString = new String(decodedBytes, StandardCharsets.UTF_8);
                    Assert.isTrue(credentialsString.contains(":"), "Decoded credentials should contain a \":\"");
                    String[] credentials = credentialsString.split(":");
                    String clientId = URLDecoder.decode(credentials[0], StandardCharsets.UTF_8);
                    String clientSecret = URLDecoder.decode(credentials[1], StandardCharsets.UTF_8);

                    ClientRequest newClientRequest = ClientRequest.from(clientRequest)
                            .headers(httpHeaders -> httpHeaders.setBasicAuth(clientId, clientSecret))
                            .build();
                    return exchangeFunction.exchange(newClientRequest);
                })
                .build();
        // @formatter:on
    }

}

此测试表明凭证已针对内部访问令牌响应进行解码WebClient:

@ExtendWith(MockitoExtension.class)
public class WebClientConfigurationTests {

    private WebClientConfiguration webClientConfiguration;

    @Mock
    private ExchangeFunction exchangeFunction;

    @Captor
    private ArgumentCaptor<ClientRequest> clientRequestCaptor;

    @BeforeEach
    public void setUp() {
        webClientConfiguration = new WebClientConfiguration();
    }

    @Test
    public void exchangeWhenBasicAuthThenDecoded() {
        WebClient webClient = webClientConfiguration.createAccessTokenResponseWebClient()
                .mutate()
                .exchangeFunction(exchangeFunction)
                .build();
        when(exchangeFunction.exchange(any(ClientRequest.class)))
                .thenReturn(Mono.just(ClientResponse.create(HttpStatus.OK).build()));

        webClient.post()
                .uri("/oauth/token")
                .headers(httpHeaders -> httpHeaders.setBasicAuth("aladdin", URLEncoder.encode("open sesame", StandardCharsets.UTF_8)))
                .retrieve()
                .bodyToMono(Void.class)
                .block();

        verify(exchangeFunction).exchange(clientRequestCaptor.capture());

        ClientRequest clientRequest = clientRequestCaptor.getValue();
        String authorizationHeader = clientRequest.headers().getFirst("Authorization");
        assertThat(authorizationHeader).isNotNull();
        String encodedCredentials = authorizationHeader.substring("Basic ".length());
        byte[] decodedBytes = Base64.getDecoder().decode(encodedCredentials);
        String credentialsString = new String(decodedBytes, StandardCharsets.UTF_8);
        String[] credentials = credentialsString.split(":");

        assertThat(credentials[0]).isEqualTo("aladdin");
        assertThat(credentials[1]).isEqualTo("open sesame");
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 spring-security-oauth2 和 WebClient 自定义 OAuth2 令牌请求的授权标头? 的相关文章

随机推荐

  • 如何用python脚本控制TP LINK路由器

    我想知道是否有一个工具可以让我连接到路由器并关闭它 然后从 python 脚本重新启动它 我知道如果我写 import os os system ssh l root 192 168 2 1 我可以通过 python 连接到我的路由器 但是
  • OpenCV Mat 和 Leptonica Pix 之间的转换

    我需要在 C 中在 OpenCV Mat 图像和 Leptonica Pix 图像格式之间进行转换 这用于 8 位灰度图像的二值化 我发现发现了 ikaliga的回答 https stackoverflow com a 25929320 2
  • 在 Swift 中使用 CommonCrypto 解密时出现问题

    我在一家Swift only加密 解密Extension for String and NSData 并且 crypt 部分的工作基于 Zaph 在链接问题中提供的答案 在 Swift 中使用 CCCrypt CommonCrypt 时出现
  • Python 中的哈希映射

    我想用Python实现HashMap 我想请求用户输入 根据他的输入 我从 HashMap 中检索一些信息 如果用户输入HashMap的某个键 我想检索相应的值 如何在 Python 中实现此功能 HashMap
  • 为什么 Double 不能隐式转换为 Decimal

    我不明白十进制和双精度的转换规则 这样做是合法的 decimal dec 10 double doub double dec 然而令我困惑的是 decimal 是 16 字节的数据类型 而 double 是 8 字节 因此将 double
  • Emma 不生成coverage.ec

    我设置了艾玛 它曾经对我有用 然后我们更改了源代码 现在它没有生成coverage ec根本不 它确实生成coverage em 测试临近结束时 出现错误消息 exec INSTRUMENTATION CODE 0 echo Downloa
  • ESP32:dsb1820 温度传感器给出恒定的负 127 读数

    我正在尝试使用连接到 esp32 微控制器的单个 dsb1820 温度传感器来获取温度读数 传感器连接到 esp32 的 GPIO 4 我打算将温度读数发送到云端 我面临的问题是温度读数总是给出值 127 我在网上某处读到 当 dsb182
  • TortoiseSVN 不要求身份验证?

    我已经在conf文件中设置了一个新的SVN存储库 运行SVNServe anon access none SVNServe 忠实地正确地做到了这一点 但是当我尝试使用 TortoiseSVN 浏览存储库时 它只是说不允许访问 它不应该要求我
  • 将 4 通道图像转换为 3 通道图像

    我正在使用 OpenCV 2 4 6 我正在尝试将 4 通道 RGB IplImage 转换为 4 通道 HSV 图像 下面是我的代码 给出错误 OpenCV 错误 未知函数断言失败 我认为 cvCvtColor 支持 3 通道图像 有没有
  • Minizinc:生成有效的转变

    希望有人能帮助我解决这个问题 最初的问题是生成有效的班次 如下所述 我有这样的数组 m m m o o l l m m m l m m m 具有固定长度 S 其中 m 是工作 o 是办公室 我自由了 我需要确保至少每 6m 就有两个 l 在
  • jQuery输入文件点击方法和IE上拒绝访问

    我尝试仅使用一个按钮作为输入文件 它在 Firefox Chrome Safari 中工作正常 但在 IE 中不行 提交表单时我总是收到 访问被拒绝 的消息 代码 input file click 有真正的解决方法吗 我在谷歌上浪费了大约2
  • 远程 Informix 11.5 命令行客户端

    Informix 11 5 是否附带了与 SQL Server 的 SQLCMD 类似的命令行工具 如果是 如何连接到远程服务器并使用它执行常规 SELECT INSERT UPDATE 查询 正如 Michal Niklas 所说 IBM
  • 如何使用 opencv.omnidir 模块对鱼眼图像进行去扭曲

    我正在尝试使用全向模块 http docs opencv org trunk db dd2 namespacecv 1 1omnidir html用于对鱼眼图像进行扭曲处理Python 我正在尝试适应这一点C 教程 http docs op
  • Linux 中的动态环境变量?

    Linux 中是否可以通过某种方式拥有动态环境变量 我有一个网络服务器 网站遵循以下布局 site qa production 我想要一个环境变量 例如 APPLICATION ENV 当我在 qa 目录中时设置为 qa 当我在生产目录中时
  • 使用 R 下载压缩数据文件、提取和导入数据

    EZGraphs 在 Twitter 上写道 很多在线 csv 都被压缩了 有没有办法下载 解压缩存档并使用 R 将数据加载到 data frame Rstats 我今天也尝试这样做 但最终只是手动下载 zip 文件 我尝试过类似的东西 f
  • 如何检查 NTAccount 对象代表组还是用户?

    使用返回的访问规则时 GetAccessRules True True GetType System Security Principal NTAccount 如何判断每个规则中引用的 NTAccount 对象是用户帐户还是组 Update
  • JSP/Servlet HTTP 404 错误处理

    我想在我的网络应用程序中处理 HTML 404 错误 我可以这样写
  • RMI 中的引用传递问题? [复制]

    这个问题在这里已经有答案了 有人可以告诉我我错在哪里 为什么这个 RMI 聊天应用程序不起作用 目标是通过远程对象或序列化对象实现客户端 服务器和逻辑之间的解耦 import javax swing import java awt even
  • Visual Studio 项目有简单的自动备份系统吗?

    我正在使用 Visual Studio 2008 Express 我希望 Visual Studio 或者可能是外接程序 将我的整个项目保存到某种自动增量存档或任何可以帮助我从灾难中恢复的文件中 我对 SVN 或复杂的版本控制系统没有太多需
  • 如何使用 spring-security-oauth2 和 WebClient 自定义 OAuth2 令牌请求的授权标头?

    我正在尝试通过 WebClient 调用升级到 spring security 5 5 1 我发现oauth2clientId and secret现在 URL 已编码为AbstractWebClientReactiveOAuth2Acce