【问题标题】:Adding parameters to webclient向 webclient 添加参数
【发布时间】:2021-12-28 06:43:42
【问题描述】:

我需要帮助!

这是我的网络客户端

private WebClient client = WebClient.create("http://address:port");

public Mono<RespDto> translate(AuthPrincipal principal, ReqDto dto, String token) {
    return client.get()
            .uri(uriBuilder -> uriBuilder
                    .path("/api")
                    .queryParam("id", dto.getId())
                    .queryParam("content", dto.getContent())
                    .queryParam("profile", dto.getProfile())
                    .build())
            .headers(h -> h.setBearerAuth(token.split(" ")[1]))
            .retrieve()
            .bodyToMono(RespDto.class);

}

这是我要检索的 api 的控制器

@GetMapping("")
public Mono<?> trans(@AuthenticationPrincipal @ApiIgnore AuthPrincipal principal,
                     @Valid ReqDto reqDto) {
    return service.function(principal, reqDto);
}

所以我的问题是服务函数检查主体 id 的权限,但我知道如何通过 webclient 传递 AuthPrincipal 对象...

请帮助菜鸟!

【问题讨论】:

  • 你没有。您传递您的凭据/令牌或向服务器验证您所需的任何内容。

标签: java spring-boot webclient


【解决方案1】:

您已经使用以下代码行提供承载身份验证令牌:.headers(h -&gt; h.setBearerAuth(token.split(" ")[1]))。如果这工作正常并且您能够针对目标 API 进行身份验证,那么 Spring 将在端点方法中注入 AuthPrincipal(假设您的 Spring Security 配置/自定义工作正常)。

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 2011-01-05
    • 2020-11-18
    • 2014-02-23
    • 2012-01-04
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多