Tomcat: how to get secure port number in Java?

I would like to add a link to a secure page of my application on one of my insecure pages. The tomcat secure port is configured in the server.xml file. In some deployments, these are 443, 8443, etc. So I need a way to read a secure port from tomcat configuration in order to use it in a link. Is it possible?

Alternatively, simply accessing the server.xml configuration (from the request context) and disassembling it yourself to find out the port number is also acceptable, but less desirable.

I understand that there can be several connectors and several safe ones, so I will leave this in my program logic to decide which one to choose. The problem is, how do I get this information?

Thanks!

+3
source share
2 answers

I am sure there is no API for this. Perhaps you can keep it custom by using the servlet environment setting in web.xml. The obvious drawback is that now you have 2 places in which the SSL port number is configured.

Another approach is to configure security in web.xml iirc something like

 <security-constraint>
    <web-resource-collection> 
        <web-resource-name>MyLoginPage</web-resource-name>
        <url-pattern>/login</url-pattern> 
        <http-method>GET</http-method> 
        <http-method>POST</http-method> 
    </web-resource-collection> 
    <user-data-constraint> 
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
     </user-data-constraint> 
 </security-constraint>

You can simply use regular links to the login page, and tomcat should automatically send redicts to the ssl connector, depending on which port is configured.

+3
source

The simplest solution is to search for the server.xml file in the init()servlet method , analyze it and save the port number. The servlet should be automatically loaded.

- script web.xml . , Tomcat , server.xml.

, , webapp , . , - , ( Tomcat).

+1

Source: https://habr.com/ru/post/1714419/


All Articles