Class RequestUrlResolver


  • public class RequestUrlResolver
    extends Object
    A utility to resolve the original request URL.

    The resolve(HttpServletRequest) method follows the logic below, in this order, to resolve the original request URL.

    1. Resolve by configuration


      If fixed values for the scheme and host have been set using the setScheme(String) and setHost(String) methods, the original request URL is reconstructed using the Request URL Construction logic (explained later) with the HttpServletRequest instance and the specified scheme and host.


    2. Resolve by an HTTP field containing the request URL


      If the name of a custom HTTP field containing the original request URL has been set using setUrlFieldName(String) and the request includes this HTTP field, the field's value is regarded as the original request URL.


      If the name of such a custom HTTP field has not been provided, X-Forwarded-URL is used as the default name. If the request includes the X-Forwarded-URL HTTP field, its value is regarded as the original request URL.


    3. Resolve by the Forwarded HTTP Field


      If the request includes the Forwarded HTTP field (RFC 7239: Forwarded HTTP Extension) and the first "forwarded element" in the field includes both the proto and host parameters, the original request URL is reconstructed using the Request URL Construction logic with the HttpServletRequest instance and the scheme specified by the proto parameter and the host specified by the host parameter.


    4. Resolve by de facto HTTP Fields


      If the scheme and the host can be determined by using de facto HTTP fields such as X-Forwarded-Host and X-Forwarded-Proto, the original request URL is reconstructed using the Request URL Construction logic with the HttpServletRequest instance and the determined scheme and host.


      To be exact, the host and scheme are determined by the following logic.


      The host is determined by the X-Forwarded-Host HTTP field.


      The scheme is determined by the following logic in this order.


      1. If the X-Forwarded-Proto HTTP field is included, its value is used as the scheme.
      2. If the X-Forwarded-Protocol HTTP field is included, its value is used as the scheme.
      3. If the X-Url-Scheem HTTP field is included, its value is used as the scheme.
      4. If the X-Forwarded-Ssl HTTP field is included and its value is on, https is used as the scheme. If the HTTP field is included but its value is not on, http is used as the scheme.
      5. If the Front-End-Https HTTP field is included and its value is on, https is used as the scheme. If the HTTP field is included but its value is not on, http is used as the scheme.

    5. Resolve by HttpServletRequest


      The original request URL is reconstructed by calling the getRequestURL() and getQueryString() methods as follows. A question mark (?) is added only if the query string is not null.

       HttpServletRequest.getRequestURL()?HttpServletRequest.getQueryString()
       

    Request URL Construction logic

    This logic accepts an HttpServletRequest instance, a scheme and a host as input. These are used to reconstruct the original request URL as follows. A question mark (?) is added only if the query string is not null.

     {scheme}://{host}HttpServletRequest.getRequestURI()?HttpServletRequest.getQueryString()
     

    Note that if the reverse proxy has modified the path or query string, this logic cannot reconstruct the original request URL.

    The implementation of this logic returns null if either the scheme or host, or both, are null, meaning that the next resolution logic will be attempted. For example, if the first forwarded element in the Forwarded HTTP field does not include the proto parameter, the resolution logic based on the Forwarded HTTP field will return null, and the next resolution logic (that uses de facto HTTP fields) will be attempted.


    X-Forwarded-URL setting

    The following example shows how to set up X-Forwarded-URL in NGINX.

     proxy_set_header X-Forwarded-URL $scheme://$host$request_uri;
     
    Since:
    2.81
    • Constructor Detail

      • RequestUrlResolver

        public RequestUrlResolver()
    • Method Detail

      • getScheme

        public String getScheme()
        Get the scheme assigned to this instance as a fixed value. This value is used when specified along with a host.
        Returns:
        The scheme assigned to this instance.
      • setScheme

        public RequestUrlResolver setScheme​(String scheme)
        Set a scheme as a fixed value. This value is used when specified along with a host.
        Parameters:
        scheme - A scheme such as "https".
        Returns:
        this instance.
      • getHost

        public String getHost()
        Get the host assigned to this instance as a fixed value. This value is used when specified along with a scheme.
        Returns:
        The host assigned to this instance.
      • setHost

        public RequestUrlResolver setHost​(String host)
        Set a host as a fixed value. This value is used when specified along with a scheme.
        Parameters:
        host - A host such as "example.com".
        Returns:
        this instance.
      • getUrlFieldName

        public String getUrlFieldName()
        Get the name of the HTTP field whose value represents the original request URL. If this property is not set, the default value, "X-Forwarded-URL", is used.
        Returns:
        The name of the HTTP field whose value represents the original request URL.
      • setUrlFieldName

        public RequestUrlResolver setUrlFieldName​(String name)
        Set the name of the HTTP field whose value represents the original request URL. If this property is not set, the default value, "X-Forwarded-URL", is used.
        Parameters:
        name - The name of the HTTP field whose value represents the original request URL.
        Returns:
        this object.
      • resolve

        public String resolve​(javax.servlet.http.HttpServletRequest request)
        Resolve the original request URL.
        Parameters:
        request - An HTTP request.
        Returns:
        The resolved original request URL.