Use HttpHeaders.setBasicAuth

Issue: gh-5612
This commit is contained in:
Rob Winch 2018-07-30 15:15:30 -05:00
parent afa2d9cbc7
commit 2cd2bab818
2 changed files with 2 additions and 26 deletions

View File

@ -37,11 +37,9 @@ import org.springframework.web.reactive.function.client.ExchangeFunction;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
@ -148,7 +146,7 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements
.getProviderDetails().getTokenUri();
ClientRequest request = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.headers(httpBasic(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
.headers(headers -> headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
.body(refreshTokenBody(authorizedClient.getRefreshToken().getTokenValue()))
.build();
return next.exchange(request)
@ -161,16 +159,6 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements
.thenReturn(result));
}
private static Consumer<HttpHeaders> httpBasic(String username, String password) {
return httpHeaders -> {
String credentialsString = username + ":" + password;
byte[] credentialBytes = credentialsString.getBytes(StandardCharsets.ISO_8859_1);
byte[] encodedBytes = Base64.getEncoder().encode(credentialBytes);
String encodedCredentials = new String(encodedBytes, StandardCharsets.ISO_8859_1);
httpHeaders.set(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials);
};
}
private boolean shouldRefresh(OAuth2AuthorizedClient authorizedClient) {
if (this.authorizedClientService == null) {
return false;

View File

@ -45,11 +45,9 @@ import reactor.core.scheduler.Schedulers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
@ -290,7 +288,7 @@ public final class ServletOAuth2AuthorizedClientExchangeFilterFunction implement
.getProviderDetails().getTokenUri();
ClientRequest refreshRequest = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.headers(httpBasic(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
.headers(headers -> headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
.body(refreshTokenBody(authorizedClient.getRefreshToken().getTokenValue()))
.build();
return next.exchange(refreshRequest)
@ -309,16 +307,6 @@ public final class ServletOAuth2AuthorizedClientExchangeFilterFunction implement
.publishOn(Schedulers.elastic());
}
private static Consumer<HttpHeaders> httpBasic(String username, String password) {
return httpHeaders -> {
String credentialsString = username + ":" + password;
byte[] credentialBytes = credentialsString.getBytes(StandardCharsets.ISO_8859_1);
byte[] encodedBytes = Base64.getEncoder().encode(credentialBytes);
String encodedCredentials = new String(encodedBytes, StandardCharsets.ISO_8859_1);
httpHeaders.set(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials);
};
}
private boolean shouldRefresh(OAuth2AuthorizedClient authorizedClient) {
if (this.authorizedClientRepository == null) {
return false;