users@jersey.java.net

[Jersey] Re: x-forwarded-proto and _at_Context

From: Robert DiFalco <robert.difalco_at_gmail.com>
Date: Fri, 9 May 2014 12:26:46 -0700

On Grizzly I do it like this:

@PreMatching
public class LoadBalancerRequestFilter implements ContainerRequestFilter {

    @Override
    public void filter( ContainerRequestContext ctx ) throws IOException {

        String scheme = getValue( ctx.getHeaders(), "x-forwarded-proto" );
        String port = getValue( ctx.getHeaders(), "x-forwarded-port" );
        if ( scheme == null && port == null )
            return;

        UriBuilder baseBuilder = ctx.getUriInfo().getBaseUriBuilder();
        UriBuilder requestBuilder = ctx.getUriInfo().getRequestUriBuilder();
        if ( scheme != null ) {
            baseBuilder.scheme( scheme );
            requestBuilder.scheme( scheme );
        }

        if ( port != null ) {
            int nPort = Integer.parseInt( port );
            baseBuilder.port( nPort );
            requestBuilder.port( nPort );
        }

        ctx.setRequestUri( baseBuilder.build(), requestBuilder.build() );
    }

    private String getValue( MultivaluedMap<String,String> headers, String
header ) {
        List<String> values = headers.get( header );
        if ( values == null || values.isEmpty() )
            return null;

        return values.get( 0 );
    }
}



On Fri, May 9, 2014 at 12:23 PM, Robert DiFalco <robert.difalco_at_gmail.com>wrote:

> Thanks Gili, I'm actually using Grizzly HTTP now. But I will keep that in
> mind if I switch.
>
>
> On Fri, May 9, 2014 at 11:52 AM, <cowwoc_at_bbs.darktech.org> wrote:
>
>> Hi Robert,
>>
>> FYI, on Jetty 9 you can do the following:
>>
>> 1. Add:
>>
>> RequestHeader set X-Forwarded-Proto "https" env=HTTPS
>>
>> to your httpd configuration.
>>
>> Source:
>> http://wiki.eclipse.org/Jetty/Howto/Configure_mod_proxy#Proxying_SSL_on
>> _Apache_to_HTTP_on_Jetty
>>
>> 2. Add "HttpConfiguration.addCustomizer(new
>> ForwardedRequestCustomizer());" to Jetty's code.
>>
>> You're done!
>>
>> The Request URL will now contain the forwarded URL.
>>
>> Gili
>>
>
>