Adapt to changed preemtive auth with HttpClient5.

See #66
This commit is contained in:
Mark Paluch
2025-06-13 14:25:34 +02:00
parent 9472418743
commit 65033315e5
2 changed files with 18 additions and 28 deletions

View File

@@ -14,6 +14,7 @@
<properties>
<spring-plugin.version>2.0.0.RELEASE</spring-plugin.version>
<jgit.version>7.3.0.202506031305-r</jgit.version>
<lombok.version>1.18.38</lombok.version>
</properties>
@@ -141,13 +142,13 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.13.3.202401111512-r</version>
<version>${jgit.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.gpg.bc</artifactId>
<version>5.13.3.202401111512-r</version>
<version>${jgit.version}</version>
</dependency>
<dependency>

View File

@@ -15,10 +15,9 @@
*/
package org.springframework.data.release.utils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hc.client5.http.ContextBuilder;
import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
@@ -27,12 +26,8 @@ import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHost;
import org.springframework.data.util.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
/**
@@ -74,21 +69,15 @@ public class HttpComponentsClientHttpRequestFactoryBuilder {
*/
public HttpComponentsClientHttpRequestFactory build() {
Lazy<CloseableHttpClient> lazy = Lazy
.of(() -> HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build());
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
setHttpClient(lazy.get());
return super.createRequest(uri, httpMethod);
}
};
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setHttpContextFactory((httpMethod, uri) -> {
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
return context;
ContextBuilder contextBuilder = ContextBuilder.create();
contextBuilder.useAuthCache(authCache);
return contextBuilder.build();
});
return factory;
@@ -97,16 +86,16 @@ public class HttpComponentsClientHttpRequestFactoryBuilder {
private static void addPreemptiveAuth(BasicCredentialsProvider credsProvider, AuthCache authCache, String requestUrl,
HttpBasicCredentials credentials) {
try {
HttpHost host = HttpHost.create(requestUrl);
HttpHost host = HttpHost.create(URI.create(requestUrl));
credsProvider.setCredentials(new AuthScope(host),
new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword().toCharArray()));
UsernamePasswordCredentials userPassword = new UsernamePasswordCredentials(credentials.getUsername(),
credentials.getPassword().toCharArray());
credsProvider.setCredentials(new AuthScope(host), userPassword);
authCache.put(host, new BasicScheme());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
BasicScheme basicScheme = new BasicScheme();
basicScheme.initPreemptive(userPassword);
authCache.put(host, basicScheme);
}
}