Class RequestUrlResolver
- java.lang.Object
-
- com.authlete.jaxrs.util.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.Resolve by configuration
If fixed values for the scheme and host have been set using the
setScheme(String)andsetHost(String)methods, the original request URL is reconstructed using the Request URL Construction logic (explained later) with theHttpServletRequestinstance and the specified scheme and host.
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-URLis used as the default name. If the request includes theX-Forwarded-URLHTTP field, its value is regarded as the original request URL.
Resolve by the
ForwardedHTTP Field
If the request includes the
ForwardedHTTP field (RFC 7239: Forwarded HTTP Extension) and the first "forwarded element" in the field includes both theprotoandhostparameters, the original request URL is reconstructed using the Request URL Construction logic with theHttpServletRequestinstance and the scheme specified by theprotoparameter and the host specified by thehostparameter.
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-HostandX-Forwarded-Proto, the original request URL is reconstructed using the Request URL Construction logic with theHttpServletRequestinstance 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-HostHTTP field.
The scheme is determined by the following logic in this order.
- If the
X-Forwarded-ProtoHTTP field is included, its value is used as the scheme. - If the
X-Forwarded-ProtocolHTTP field is included, its value is used as the scheme. - If the
X-Url-ScheemHTTP field is included, its value is used as the scheme. - If the
X-Forwarded-SslHTTP field is included and its value ison,httpsis used as the scheme. If the HTTP field is included but its value is noton,httpis used as the scheme. - If the
Front-End-HttpsHTTP field is included and its value ison,httpsis used as the scheme. If the HTTP field is included but its value is noton,httpis used as the scheme.
- If the
Resolve by HttpServletRequest
The original request URL is reconstructed by calling the
getRequestURL()andgetQueryString()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
HttpServletRequestinstance, 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
ForwardedHTTP field does not include theprotoparameter, the resolution logic based on theForwardedHTTP field will return null, and the next resolution logic (that uses de facto HTTP fields) will be attempted.
X-Forwarded-URLsettingThe following example shows how to set up
X-Forwarded-URLin NGINX.proxy_set_header X-Forwarded-URL $scheme://$host$request_uri;
- Since:
- 2.81
-
-
Constructor Summary
Constructors Constructor Description RequestUrlResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetHost()Get the host assigned to this instance as a fixed value.StringgetScheme()Get the scheme assigned to this instance as a fixed value.StringgetUrlFieldName()Get the name of the HTTP field whose value represents the original request URL.Stringresolve(javax.servlet.http.HttpServletRequest request)Resolve the original request URL.RequestUrlResolversetHost(String host)Set a host as a fixed value.RequestUrlResolversetScheme(String scheme)Set a scheme as a fixed value.RequestUrlResolversetUrlFieldName(String name)Set the name of the HTTP field whose value represents the original request URL.
-
-
-
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:
thisinstance.
-
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:
thisinstance.
-
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:
thisobject.
-
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.
-
-