I am writing a code which task is to retrieve a requested URL or full path. I've written this code:
HttpServletRequest request;//obtained from other functions
String uri = request.getRequestURI();
if (request.getQueryString() != null)
uri += "?" + request.getQueryString();
So, when I browse http://google.com?q=abc
it is OK (correct).
But there is problem when I browse https://google.com
. The value of uri
is http://google.com:443google.com:443
, So the program doesn't only when HTTPS
is used.
And the output is same for request.getRequestURL().toString()
.
What is the solution?
Simply Use:
The fact that a
HTTPS
request becomesHTTP
when you tried to construct the URL on server side indicates that you might have a proxy/load balancer (nginx
,pound
, etc.) offloading SSL encryption in front and forward to your back end service in plainHTTP
.If that's case, check,
Host
,X-forwarded-proto
,X-forwarded-for
, etc).Tomcat
) is set up to recognize the proxy in front. For example,Tomcat
requires addingsecure="true" scheme="https" proxyPort="443"
attributes to itsConnector
Tomcat
automatically replacesscheme
,remoteAddr
, etc. values when you addRemoteIpValve
to itsEngine
. (see Configuration guide, JavaDoc) so you don't have to process these headers in your code manually.Incorrect proxy header values could result in incorrect output when
request.getRequestURI()
orrequest.getRequestURL()
attempts to construct the originating URL.By design,
getRequestURL()
gives you the full URL, missing only the query string.In
HttpServletRequest
, you can get individual parts of the URI using the methods below:.getScheme()
will give you"https"
if it was ahttps://domain
request..getServerName()
givesdomain
onhttp(s)://domain
..getServerPort()
will give you the port.Use the snippet below:
This snippet above will get the full URI, hiding the port if the default one was used, and not adding the
"?"
and the query string if the latter was not provided.Proxied requests
Note, that if your request passes through a proxy, you need to look at the
X-Forwarded-Proto
header since the scheme might be altered:Also, a common header is
X-Forwarded-For
, which show the original request IP instead of the proxys IP.If you are responsible for the configuration of the proxy/load balancer yourself, you need to ensure that these headers are set upon forwarding.