|
17 | 17 | package org.springframework.web.reactive.function.client; |
18 | 18 |
|
19 | 19 | import java.nio.charset.Charset; |
| 20 | +import java.nio.charset.StandardCharsets; |
20 | 21 | import java.util.function.Function; |
21 | 22 | import java.util.function.Predicate; |
22 | 23 |
|
| 24 | +import org.jspecify.annotations.Nullable; |
23 | 25 | import reactor.core.publisher.Mono; |
24 | 26 |
|
25 | 27 | import org.springframework.core.io.buffer.DataBufferUtils; |
|
34 | 36 | * @author Rob Winch |
35 | 37 | * @author Arjen Poutsma |
36 | 38 | * @author Sam Brannen |
| 39 | + * @author Kai Zander |
37 | 40 | * @since 5.0 |
38 | 41 | */ |
39 | 42 | public abstract class ExchangeFilterFunctions { |
@@ -76,14 +79,35 @@ public static ExchangeFilterFunction statusError(Predicate<HttpStatusCode> statu |
76 | 79 | * Return a filter that applies HTTP Basic Authentication to the request |
77 | 80 | * headers via {@link HttpHeaders#setBasicAuth(String)} and |
78 | 81 | * {@link HttpHeaders#encodeBasicAuth(String, String, Charset)}. |
| 82 | + * <p>{@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1} is used to convert |
| 83 | + * the credentials into an octet sequence. |
79 | 84 | * @param username the username |
80 | 85 | * @param password the password |
81 | 86 | * @return the filter to add authentication headers with |
| 87 | + * @see #basicAuthentication(String, String, Charset) |
82 | 88 | * @see HttpHeaders#encodeBasicAuth(String, String, Charset) |
83 | 89 | * @see HttpHeaders#setBasicAuth(String) |
84 | 90 | */ |
85 | 91 | public static ExchangeFilterFunction basicAuthentication(String username, String password) { |
86 | | - String encodedCredentials = HttpHeaders.encodeBasicAuth(username, password, null); |
| 92 | + return basicAuthentication(username, password, null); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Return a filter that applies HTTP Basic Authentication to the request |
| 97 | + * headers via {@link HttpHeaders#setBasicAuth(String)} and |
| 98 | + * {@link HttpHeaders#encodeBasicAuth(String, String, Charset)}. |
| 99 | + * @param username the username |
| 100 | + * @param password the password |
| 101 | + * @param charset the charset to use to convert the credentials into an octet |
| 102 | + * sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}. |
| 103 | + * @return the filter to add authentication headers with |
| 104 | + * @since 7.0.8 |
| 105 | + * @see #basicAuthentication(String, String) |
| 106 | + * @see HttpHeaders#encodeBasicAuth(String, String, Charset) |
| 107 | + * @see HttpHeaders#setBasicAuth(String) |
| 108 | + */ |
| 109 | + public static ExchangeFilterFunction basicAuthentication(String username, String password, @Nullable Charset charset) { |
| 110 | + String encodedCredentials = HttpHeaders.encodeBasicAuth(username, password, charset); |
87 | 111 | return (request, next) -> |
88 | 112 | next.exchange(ClientRequest.from(request) |
89 | 113 | .headers(headers -> headers.setBasicAuth(encodedCredentials)) |
|
0 commit comments