Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTTP or HTTPS from Current URL

Tags:

java

jsp

I simply want to get whether the current URL is under HTTP or HTTPS. The issue is we have a number of self-signed certificates in our testing environments so validating the cert probabluy isn't an option. I've tried the following, but thry don't appear to work in all cases.

String protocol = request.getProtocol();
out.println(protocol); // prints out HTTP/1.1 on self signed servers

Boolean secure = ((HttpServletRequest)pageContext.getRequest()).isSecure();
if (secure) {
  out.println("secure")
} else {
  out.println("not secure")  // always fails
}

I suppose I could use getRequest and search the string for "https" - but I'm assuming there's a truly supported way of doing this.

like image 339
user1754738 Avatar asked Mar 10 '26 06:03

user1754738


1 Answers

It does not matter if self-signed or official certs are used. If SSL/TLS is NOT terminated at the deployment container 'isSecure()' will return 'false' by default. However some deployment containers like Tomcat can be configured to return 'true' for 'isSecure()' call even if SSL/TLS is NOT terminated at the deployment container. E.g. this is helpful if SSL is terminated at a (hardware-)loadbalancer and 'sendRedirect()' is used with relative URL.

like image 52
Bernhard Thalmayr Avatar answered Mar 12 '26 20:03

Bernhard Thalmayr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!