@@ -742,7 +742,9 @@ public Set<HttpMethod> getAllow() {
742
742
* @throws IllegalArgumentException if either {@code user} or
743
743
* {@code password} contain characters that cannot be encoded to ISO-8859-1
744
744
* @since 5.1
745
+ * @see #setBasicAuth(String)
745
746
* @see #setBasicAuth(String, String, Charset)
747
+ * @see #encodeBasicAuth(String, String, Charset)
746
748
* @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
747
749
*/
748
750
public void setBasicAuth (String username , String password ) {
@@ -759,24 +761,33 @@ public void setBasicAuth(String username, String password) {
759
761
* @throws IllegalArgumentException if {@code username} or {@code password}
760
762
* contains characters that cannot be encoded to the given charset
761
763
* @since 5.1
764
+ * @see #setBasicAuth(String)
765
+ * @see #setBasicAuth(String, String)
766
+ * @see #encodeBasicAuth(String, String, Charset)
762
767
* @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
763
768
*/
764
769
public void setBasicAuth (String username , String password , @ Nullable Charset charset ) {
765
- Assert .notNull (username , "Username must not be null" );
766
- Assert .notNull (password , "Password must not be null" );
767
- if (charset == null ) {
768
- charset = StandardCharsets .ISO_8859_1 ;
769
- }
770
-
771
- CharsetEncoder encoder = charset .newEncoder ();
772
- if (!encoder .canEncode (username ) || !encoder .canEncode (password )) {
773
- throw new IllegalArgumentException (
774
- "Username or password contains characters that cannot be encoded to " + charset .displayName ());
775
- }
770
+ setBasicAuth (encodeBasicAuth (username , password , charset ));
771
+ }
776
772
777
- String credentialsString = username + ":" + password ;
778
- byte [] encodedBytes = Base64 .getEncoder ().encode (credentialsString .getBytes (charset ));
779
- String encodedCredentials = new String (encodedBytes , charset );
773
+ /**
774
+ * Set the value of the {@linkplain #AUTHORIZATION Authorization} header to
775
+ * Basic Authentication based on the given {@linkplain #encodeBasicAuth
776
+ * encoded credentials}.
777
+ * <p>Favor this method over {@link #setBasicAuth(String, String)} and
778
+ * {@link #setBasicAuth(String, String, Charset)} if you wish to cache the
779
+ * encoded credentials.
780
+ * @param encodedCredentials the encoded credentials
781
+ * @throws IllegalArgumentException if supplied credentials string is
782
+ * {@code null} or blank
783
+ * @since 5.2
784
+ * @see #setBasicAuth(String, String)
785
+ * @see #setBasicAuth(String, String, Charset)
786
+ * @see #encodeBasicAuth(String, String, Charset)
787
+ * @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
788
+ */
789
+ public void setBasicAuth (String encodedCredentials ) {
790
+ Assert .hasText (encodedCredentials , "'encodedCredentials' must not be null or blank" );
780
791
set (AUTHORIZATION , "Basic " + encodedCredentials );
781
792
}
782
793
@@ -1781,6 +1792,41 @@ public static String formatHeaders(MultiValueMap<String, String> headers) {
1781
1792
.collect (Collectors .joining (", " , "[" , "]" ));
1782
1793
}
1783
1794
1795
+ /**
1796
+ * Encode the given username and password into Basic Authentication credentials.
1797
+ * <p>The encoded credentials returned by this method can be supplied to
1798
+ * {@link #setBasicAuth(String)} to set the Basic Authentication header.
1799
+ * @param username the username
1800
+ * @param password the password
1801
+ * @param charset the charset to use to convert the credentials into an octet
1802
+ * sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}.
1803
+ * @throws IllegalArgumentException if {@code username} or {@code password}
1804
+ * contains characters that cannot be encoded to the given charset
1805
+ * @since 5.2
1806
+ * @see #setBasicAuth(String)
1807
+ * @see #setBasicAuth(String, String)
1808
+ * @see #setBasicAuth(String, String, Charset)
1809
+ * @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
1810
+ */
1811
+ public static String encodeBasicAuth (String username , String password , @ Nullable Charset charset ) {
1812
+ Assert .notNull (username , "Username must not be null" );
1813
+ Assert .doesNotContain (username , ":" , "Username must not contain a colon" );
1814
+ Assert .notNull (password , "Password must not be null" );
1815
+ if (charset == null ) {
1816
+ charset = StandardCharsets .ISO_8859_1 ;
1817
+ }
1818
+
1819
+ CharsetEncoder encoder = charset .newEncoder ();
1820
+ if (!encoder .canEncode (username ) || !encoder .canEncode (password )) {
1821
+ throw new IllegalArgumentException (
1822
+ "Username or password contains characters that cannot be encoded to " + charset .displayName ());
1823
+ }
1824
+
1825
+ String credentialsString = username + ":" + password ;
1826
+ byte [] encodedBytes = Base64 .getEncoder ().encode (credentialsString .getBytes (charset ));
1827
+ return new String (encodedBytes , charset );
1828
+ }
1829
+
1784
1830
// Package-private: used in ResponseCookie
1785
1831
static String formatDate (long date ) {
1786
1832
Instant instant = Instant .ofEpochMilli (date );
0 commit comments