How to redirect from HTTP to HTTPS on Tomcat Server
On this page, we will learn how to redirect an application from HTTP to HTTPS on Tomcat Server automatically. But before directly jumping to the configuration, make sure you have installed and configured the self-signed SSL on the Tomcat server.
And if SSL is not configured yet, you can take help from our previous blog on How to configure SSL/HTTPS on Tomcat Server.
Step 1: Open the server.xml
file from ~{Tomcat-Installation-Directory}/conf
directory and set the redirect port to the HTTPS connector port for the HTTP connector.
<Connector
port="8090"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
/>
Step 2: Add the below configuration in ~{Tomcat-Installation-Directory}/conf/web.xml
file but make sure to add it after all the Servlet mapping tags.
<security-constraint>
<web-resource-collection>
<web-resource-name>All Web Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
Step 3: Now restart the Tomcat server and hit http://localhost:8090/docs/ on your browser and it will automatically redirected to https://localhost:8443/docs/.