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();
添加回答
舉報
