mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-26 22:02:41 +00:00
Use HttpHeaders.setBasicAuth
Issue: gh-5612
This commit is contained in:
parent
afa2d9cbc7
commit
2cd2bab818
@ -37,11 +37,9 @@ import org.springframework.web.reactive.function.client.ExchangeFunction;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -148,7 +146,7 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements
|
|||||||
.getProviderDetails().getTokenUri();
|
.getProviderDetails().getTokenUri();
|
||||||
ClientRequest request = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
|
ClientRequest request = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
|
||||||
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
|
.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()))
|
.body(refreshTokenBody(authorizedClient.getRefreshToken().getTokenValue()))
|
||||||
.build();
|
.build();
|
||||||
return next.exchange(request)
|
return next.exchange(request)
|
||||||
@ -161,16 +159,6 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements
|
|||||||
.thenReturn(result));
|
.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) {
|
private boolean shouldRefresh(OAuth2AuthorizedClient authorizedClient) {
|
||||||
if (this.authorizedClientService == null) {
|
if (this.authorizedClientService == null) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -45,11 +45,9 @@ import reactor.core.scheduler.Schedulers;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -290,7 +288,7 @@ public final class ServletOAuth2AuthorizedClientExchangeFilterFunction implement
|
|||||||
.getProviderDetails().getTokenUri();
|
.getProviderDetails().getTokenUri();
|
||||||
ClientRequest refreshRequest = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
|
ClientRequest refreshRequest = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
|
||||||
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
|
.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()))
|
.body(refreshTokenBody(authorizedClient.getRefreshToken().getTokenValue()))
|
||||||
.build();
|
.build();
|
||||||
return next.exchange(refreshRequest)
|
return next.exchange(refreshRequest)
|
||||||
@ -309,16 +307,6 @@ public final class ServletOAuth2AuthorizedClientExchangeFilterFunction implement
|
|||||||
.publishOn(Schedulers.elastic());
|
.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) {
|
private boolean shouldRefresh(OAuth2AuthorizedClient authorizedClient) {
|
||||||
if (this.authorizedClientRepository == null) {
|
if (this.authorizedClientRepository == null) {
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user