Browse Source

Add setBearerAuth method

Issue: SPR-16997
pull/1474/merge
Arjen Poutsma 6 years ago
parent
commit
2ac6a15f6f
  1. 15
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 9
      spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

15
spring-web/src/main/java/org/springframework/http/HttpHeaders.java

@ -747,7 +747,20 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable @@ -747,7 +747,20 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
String credentialsString = username + ":" + password;
byte[] encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(charset));
String encodedCredentials = new String(encodedBytes, charset);
set(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials);
set(AUTHORIZATION, "Basic " + encodedCredentials);
}
/**
* Set the value of the {@linkplain #AUTHORIZATION Authorization} header to
* the given Bearer token.
* @param token the Base64 encoded token
* @since 5.1
* @see <a href="https://tools.ietf.org/html/rfc6750">RFC 6750</a>
*/
public void setBearerAuth(String token) {
Assert.notNull(token, "Token must not be null");
set(AUTHORIZATION, "Bearer " + token);
}
/**

9
spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

@ -549,4 +549,13 @@ public class HttpHeadersTests { @@ -549,4 +549,13 @@ public class HttpHeadersTests {
headers.setBasicAuth(username, password);
}
@Test
public void bearerAuth() {
String token = "foo";
headers.setBearerAuth(token);
String authorization = headers.getFirst(HttpHeaders.AUTHORIZATION);
assertEquals("Bearer foo", authorization);
}
}

Loading…
Cancel
Save