Skip to content

Commit 2cd2bab

Browse files
committed
Use HttpHeaders.setBasicAuth
Issue: gh-5612
1 parent afa2d9c commit 2cd2bab

File tree

2 files changed

+2
-26
lines changed

2 files changed

+2
-26
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@
3737
import reactor.core.publisher.Mono;
3838

3939
import java.net.URI;
40-
import java.nio.charset.StandardCharsets;
4140
import java.time.Clock;
4241
import java.time.Duration;
4342
import java.time.Instant;
44-
import java.util.Base64;
4543
import java.util.Collection;
4644
import java.util.Map;
4745
import java.util.Optional;
@@ -148,7 +146,7 @@ private Mono<OAuth2AuthorizedClient> refreshAuthorizedClient(ExchangeFunction ne
148146
.getProviderDetails().getTokenUri();
149147
ClientRequest request = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
150148
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
151-
.headers(httpBasic(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
149+
.headers(headers -> headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
152150
.body(refreshTokenBody(authorizedClient.getRefreshToken().getTokenValue()))
153151
.build();
154152
return next.exchange(request)
@@ -161,16 +159,6 @@ private Mono<OAuth2AuthorizedClient> refreshAuthorizedClient(ExchangeFunction ne
161159
.thenReturn(result));
162160
}
163161

164-
private static Consumer<HttpHeaders> httpBasic(String username, String password) {
165-
return httpHeaders -> {
166-
String credentialsString = username + ":" + password;
167-
byte[] credentialBytes = credentialsString.getBytes(StandardCharsets.ISO_8859_1);
168-
byte[] encodedBytes = Base64.getEncoder().encode(credentialBytes);
169-
String encodedCredentials = new String(encodedBytes, StandardCharsets.ISO_8859_1);
170-
httpHeaders.set(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials);
171-
};
172-
}
173-
174162
private boolean shouldRefresh(OAuth2AuthorizedClient authorizedClient) {
175163
if (this.authorizedClientService == null) {
176164
return false;

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@
4545
import javax.servlet.http.HttpServletRequest;
4646
import javax.servlet.http.HttpServletResponse;
4747
import java.net.URI;
48-
import java.nio.charset.StandardCharsets;
4948
import java.time.Clock;
5049
import java.time.Duration;
5150
import java.time.Instant;
52-
import java.util.Base64;
5351
import java.util.Collection;
5452
import java.util.Map;
5553
import java.util.Optional;
@@ -290,7 +288,7 @@ private Mono<OAuth2AuthorizedClient> refreshAuthorizedClient(ClientRequest reque
290288
.getProviderDetails().getTokenUri();
291289
ClientRequest refreshRequest = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
292290
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
293-
.headers(httpBasic(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
291+
.headers(headers -> headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
294292
.body(refreshTokenBody(authorizedClient.getRefreshToken().getTokenValue()))
295293
.build();
296294
return next.exchange(refreshRequest)
@@ -309,16 +307,6 @@ private Mono<OAuth2AuthorizedClient> refreshAuthorizedClient(ClientRequest reque
309307
.publishOn(Schedulers.elastic());
310308
}
311309

312-
private static Consumer<HttpHeaders> httpBasic(String username, String password) {
313-
return httpHeaders -> {
314-
String credentialsString = username + ":" + password;
315-
byte[] credentialBytes = credentialsString.getBytes(StandardCharsets.ISO_8859_1);
316-
byte[] encodedBytes = Base64.getEncoder().encode(credentialBytes);
317-
String encodedCredentials = new String(encodedBytes, StandardCharsets.ISO_8859_1);
318-
httpHeaders.set(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials);
319-
};
320-
}
321-
322310
private boolean shouldRefresh(OAuth2AuthorizedClient authorizedClient) {
323311
if (this.authorizedClientRepository == null) {
324312
return false;

0 commit comments

Comments
 (0)