3 回答

TA贡献1869条经验 获得超4个赞
从 spring 5.1 开始,您应该使用 设置基本身份验证HttpHeaders#setBasicAuth
,如下所示:
webClient .get() .uri("https://example.com") .headers(headers -> headers.setBasicAuth("username", "password")) .exchange() ....
以前使用 的方法.filter(basicAuthentication("user", "password")
现在已弃用。

TA贡献1801条经验 获得超16个赞
HTTP 基本身份验证需要在Authorization
标头中以 Base64 格式编码的用户名和密码。此外,您不需要登录端点,因为此信息应随每个请求一起发送。
将 Basic Auth 标头添加到客户端中的每个调用,如下所示:
String basicAuthHeader = "basic " + Base64Utils.encodeToString((username + ":" + password).getBytes()) client.get().uri("/route/user/all") .accept(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, basicAuthHeader) .exchange() .flatMapMany(response -> response.bodyToFlux(User.class)) .subscribe(u -> System.out.println("All Users : " + u.getUsername() + ":" + u.getEmail() + ":" + u.getFullname()));

TA贡献1827条经验 获得超4个赞
Spring 提供了 API,用于通过ClientFilters向 WebClient 提供基本的身份验证参数。
Authorization您可以使用较少的自定义编码设置标头来获得相同的结果。
请从 spring 文档中查看下面的代码片段:
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
WebClient client = WebClient.builder()
.filter(basicAuthentication("user", "password"))
.build();
添加回答
举报