Skip to content

Instantly share code, notes, and snippets.

@JonasGroeger
Last active January 18, 2022 18:21
Show Gist options
  • Save JonasGroeger/6ddacb9246c4a43876110093f3558869 to your computer and use it in GitHub Desktop.
Save JonasGroeger/6ddacb9246c4a43876110093f3558869 to your computer and use it in GitHub Desktop.
IT.N Proxy Guide

IT.N Proxy Guide

Ausgehende Anfragen (z.B. Webhooks, HTTP-Aufrufe) ins Internet müssen über einen Proxyserver gehen.

Das betrifft nicht die Response eines Request ⇄ Response Zyklus.

Konfiguration

Host: http-proxy.niedersachsen.de
Port: 8080

JVM-Global (Java, Groovy, Kotlin, ...)

Die folgende Konfiguration

Beispielaufruf einer .jar Datei mit Proxy:

java \
  -Dhttp.proxyHost="http-proxy.niedersachsen.de" -Dhttp.proxyPort=8080 \
  -Dhttps.proxyHost="http-proxy.niedersachsen.de" -Dhttps.proxyPort=8080 \
  -Dhttp.nonProxyHosts="localhost|127.*|[::1]|172.20.0.0/16" \
  -jar myjar.jar

Der Wert für http.nonProxyHosts oben ist der Default. Man könnte den hier also weglassen. Alle ausgehenden Aufrufe gehen dann über den Proxy.

Spring WebClient

Spring RestTemplate (deprecated)

Docker

Die Proxyeinstellungen betreffen natürlich auch Aufrufe zwischen Docker Containern. Möchte man das nicht (i.d.R. möchte man das nicht), so muss der Parameter (hier: JVM) http.nonProxyHosts befüllt werden. Hier kann man explizit die DNS-Namen der anderen Docker Container angeben oder alternativ auch das Subnetz:

$ docker network create foo
a26423790bd1cb8880602abd8be6e33a83d90138609970efb2997347380ba127

$ docker network inspect foo | jq -r '.[0].IPAM.Config[0].Subnet'
172.20.0.0/16

Die Konfiguration lautet dann -DnonProxyHosts="localhost|127.*|[::1]|172.20.0.0/16".

Referenzen

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfiguration {
@Bean
public WebClient proxyWebClient(WebClient.Builder webClientBuilder) {
return webClientBuilder.build();
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "app.http.proxy")
public class WebClientProxyConfiguration {
private String host;
private int port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.ProxyProvider;
@Component
public class WebClientProxyCustomizer implements WebClientCustomizer {
private final WebClientProxyConfiguration webClientProxyConfiguration;
@Autowired
public WebClientProxyCustomizer(WebClientProxyConfiguration webClientProxyConfiguration) {
this.webClientProxyConfiguration = webClientProxyConfiguration;
}
@Override
public void customize(WebClient.Builder webClientBuilder) {
HttpClient httpClient = HttpClient.create()
.proxy(proxy -> proxy
.type(ProxyProvider.Proxy.HTTP)
.host(webClientProxyConfiguration.getHost())
.port(webClientProxyConfiguration.getPort())
);
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
webClientBuilder.clientConnector(connector);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment