Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring embedded Jetty 9 for X-FORWARDED-PROTO with Spring Boot

I am running a Spring Boot application in AWS. The application is running behind an Elastic Load Balancer (ELB). The ELB is configured to use https (port 443) to the outside world, but passes through http (port 8080) to the application. The ELB is configured to pass through the x-forwarded-proto header. I am using Jetty 9.0.0.M0, with Spring Boot 1.1.5 RELEASE.

I appear to be getting incorrect redirects sent back from the application via the ELB where the redirect responses are coming back as http, rather than https. Now, I read here that I should set the "forwarded" header to true using:

<Set name="forwarded">true</Set>

I can't see how to do this with the embedded version of Jetty in Spring Boot because there is no XML configuration file as part of my source.

I have looked at the EmbeddedServletContainerCustomizer infrastructure but I still can't get the right incantation to get this setup to work.

The application is built and tested outside of the AWS https environment, so the application needs to transparently work with http too. Directly hitting the application endpoints without going through the ELB works. It's just that the ELB to application route that's not working.

Any ideas?

like image 565
s5b Avatar asked Oct 13 '14 06:10

s5b


1 Answers

Had a similar issue myself and while researching stumbled across your question. I found this was quite easy to to programatically however isn't really explained in the Jetty docs.

The structure of the Jetty xml configuration files are matched by the structure of the java API so you can just replicate it in code.

So following the Jetty guide on how to configure using the XML configuration file here

I was able to configure the embedded server programatically like this:

    Server server = new Server( port );

    // Create HTTP Config
    HttpConfiguration httpConfig = new HttpConfiguration();

    // Add support for X-Forwarded headers
    httpConfig.addCustomizer( new org.eclipse.jetty.server.ForwardedRequestCustomizer() );

    // Create the http connector
    HttpConnectionFactory connectionFactory = new HttpConnectionFactory( httpConfig );
    ServerConnector connector = new ServerConnector(server, connectionFactory);

    // Make sure you set the port on the connector, the port in the Server constructor is overridden by the new connector
    connector.setPort( port );

    // Add the connector to the server
    server.setConnectors( new ServerConnector[] { connector } );
like image 186
3urdoch Avatar answered Sep 25 '22 20:09

3urdoch