Workaround for “X-Forwarded” headers in Spring UriComponentsBuilder

When our application server user proxy, that doesn’t set “X-Forwarded” headers with original request port, then we can use some hack in our application. Just ignore port when it is equal 80 or 443, cause it is default. For example instead of "http://localhost:80/app", we will get "http://localhost/app". And "https://localhost:443/app" will output "https://localhost/app".

UriComponentsBuilder currentUrl = ServletUriComponentsBuilder
int port =;
currentUrl.port(port != 80 && port != 443 ? port : -1);
String currentUrlString = currentUrl.toUriString();

Inspired by Mat Banik strackoverflow answer –

How to deal with properties in Spring context tests?

public class MyTestConfig {

static {
System.setProperty("", "true"); 

/** properties loading */
public static PropertySourcesPlaceholderConfigurer properties() {
final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
return pspc;


@SpringApplicationConfiguration(classes = MyTestConfig.class)
public class MySpringTest {

Java SSH Client

String hello = new Shell.Plain(
  new SSH(
    "", 22,
    "yegor", "-----BEGIN RSA PRIVATE KEY-----..."
).exec("echo 'Hello, world!'");

Java console program + Maven

Use “Assembler Plugin”:

How it looks:

$ mvn package appassembler:assemble
$ sh target/appassembler/bin/app
Hello World!