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 theHttpServletRequest
instance 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-URL
is used as the default name. If the request includes theX-Forwarded-URL
HTTP field, its value is regarded as the original request URL.
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 theproto
andhost
parameters, the original request URL is reconstructed using the Request URL Construction logic with theHttpServletRequest
instance and the scheme specified by theproto
parameter and the host specified by thehost
parameter.
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
andX-Forwarded-Proto
, the original request URL is reconstructed using the Request URL Construction logic with theHttpServletRequest
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.
- If the
X-Forwarded-Proto
HTTP field is included, its value is used as the scheme. - If the
X-Forwarded-Protocol
HTTP field is included, its value is used as the scheme. - If the
X-Url-Scheem
HTTP field is included, its value is used as the scheme. - If the
X-Forwarded-Ssl
HTTP field is included and its value ison
,https
is used as the scheme. If the HTTP field is included but its value is noton
,http
is used as the scheme. - If the
Front-End-Https
HTTP field is included and its value ison
,https
is used as the scheme. If the HTTP field is included but its value is noton
,http
is 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
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 theproto
parameter, the resolution logic based on theForwarded
HTTP field will return null, and the next resolution logic (that uses de facto HTTP fields) will be attempted.
X-Forwarded-URL
settingThe 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 Summary
Constructors Constructor Description RequestUrlResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getHost()
Get the host assigned to this instance as a fixed value.String
getScheme()
Get the scheme assigned to this instance as a fixed value.String
getUrlFieldName()
Get the name of the HTTP field whose value represents the original request URL.String
resolve(javax.servlet.http.HttpServletRequest request)
Resolve the original request URL.RequestUrlResolver
setHost(String host)
Set a host as a fixed value.RequestUrlResolver
setScheme(String scheme)
Set a scheme as a fixed value.RequestUrlResolver
setUrlFieldName(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:
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.
-
-