javaspring-bootjettyload-balancingproxy-protocol

Accept Proxy Protocol V2 Traffic with Jetty in Spring Boot


I am trying to configure Jetty 9.4.39.v20210325 in java Spring Boot 2.4.5 to accept proxy protocol V2 traffic. I want to do this programmatically in a spring configuration class. This is the method I wrote in the SharedConfiguration.java configuration class based on the Proxy Protocol section on the bottom of this page.

@Bean
  public ConfigurableServletWebServerFactory
      jettyCustomizer() {
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.addServerCustomizers(server -> {
      ProxyConnectionFactory proxyConnectionFactory = new ProxyConnectionFactory();
      ServerConnector serverConnector = new ServerConnector(server, proxyConnectionFactory);
      server.addConnector(serverConnector);
    });
    return factory;
  }

The HTTP traffic is coming from an AWS EC2 network load balancer (NLB) and the balancer has proxy protocol V2 traffic enabled.

I am getting this response when I activate an endpoint in my service with the proxy protocol traffic:

Bad Message 400
reason: Illegal character CNTL=0x0

Does anyone know how I can get my service to accept this proxy protocol V2 traffic? I'm unsure if I am configuring Jetty correctly to do so.

Thanks!


Solution

  • I have since figured out the problem with my configuration, I was adding a new server connector instead of changing the one already in the Jetty server.

    Here is the correct approach:

    @Bean
    public ConfigurableServletWebServerFactory jettyCustomizer() {
      JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
      factory.addServerCustomizers(server -> {
        ServerConnector serverConnector = (ServerConnector) server.getConnectors()[0];
        serverConnector.addFirstConnectionFactory(new ProxyConnectionFactory());
        });
      return factory;
    }
    

    Here I am getting the active server connector and mutating it to also contain a ProxyConnectionFactory which allows Jetty to accept the proxy protocol traffic.