Skip to content

Commit

Permalink
Restore Proxy-Authorization header for RestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Nov 6, 2024
1 parent 13963c3 commit b6414d6
Showing 1 changed file with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,36 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Optional;
import java.util.regex.Pattern;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import com.c4_soft.springaddons.rest.SpringAddonsRestProperties.RestClientProperties.ClientHttpRequestFactoryProperties;

/**
* <p>
* A wrapper around {@link SimpleClientHttpRequestFactory} that sends the request through an HTTP or
* SOCKS proxy when it is enabled and when the request URI does not match the NO_PROXY pattern
* SOCKS proxy when it is enabled and when the request URI does not match the NO_PROXY pattern.
* </p>
* <p>
* When going through a proxy, the Proxy-Authorization header is set if username and password are
* non-empty.
* </p>
*
* @author Jérôme Wacongne &lt;ch4mp#64;c4-soft.com&gt;
*/
public class SpringAddonsClientHttpRequestFactory implements ClientHttpRequestFactory {
private final Optional<Pattern> nonProxyHostsPattern;
private final SimpleClientHttpRequestFactory proxyDelegate;
private final SimpleClientHttpRequestFactory noProxyDelegate;
private final ClientHttpRequestFactory proxyDelegate;
private final ClientHttpRequestFactory noProxyDelegate;

public SpringAddonsClientHttpRequestFactory(SystemProxyProperties systemProperties,
ClientHttpRequestFactoryProperties addonsProperties) {
Expand All @@ -35,11 +46,7 @@ public SpringAddonsClientHttpRequestFactory(SystemProxyProperties systemProperti
this.noProxyDelegate = from(addonsProperties);

if (proxySupport.isEnabled()) {
this.proxyDelegate = from(addonsProperties);
final var address =
new InetSocketAddress(proxySupport.getHostname().get(), proxySupport.getPort());
final var proxy = new Proxy(protocolToProxyType(proxySupport.getProtocol()), address);
this.proxyDelegate.setProxy(proxy);
this.proxyDelegate = new ProxyAwareClientHttpRequestFactory(proxySupport, addonsProperties);
} else {
this.proxyDelegate = this.noProxyDelegate;
}
Expand Down Expand Up @@ -81,4 +88,33 @@ private static SimpleClientHttpRequestFactory from(
return requestFactory;
}

public static class ProxyAwareClientHttpRequestFactory implements ClientHttpRequestFactory {
private final SimpleClientHttpRequestFactory delegate;
private final @Nullable String username;
private final @Nullable String password;

public ProxyAwareClientHttpRequestFactory(ProxySupport proxySupport,
ClientHttpRequestFactoryProperties properties) {
this.username = proxySupport.getUsername();
this.password = proxySupport.getPassword();
this.delegate = SpringAddonsClientHttpRequestFactory.from(properties);
final var address =
new InetSocketAddress(proxySupport.getHostname().get(), proxySupport.getPort());
final var proxy = new Proxy(protocolToProxyType(proxySupport.getProtocol()), address);
this.delegate.setProxy(proxy);
}

@SuppressWarnings("null")
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
final var request = delegate.createRequest(uri, httpMethod);
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
final var base64 = Base64.getEncoder()
.encodeToString((username + ':' + password).getBytes(StandardCharsets.UTF_8));
request.getHeaders().set(HttpHeaders.PROXY_AUTHORIZATION, "Basic %s".formatted(base64));
}
return request;
}
}

}

0 comments on commit b6414d6

Please sign in to comment.