get server name and port number of JSP page for HT

2019-08-18 01:50发布

问题:

i need to make the URL for my form action but the form should be submitted under https currently the underlying system running on 9002 port for the HTTPS.I can not use the following option in my JSP page

<form:form action="${request.contextPath}/springSecurity/login"
method="post" commandName="loginForm" target="guestFrame"> 

since when in HTTP the context path is coming as HTTP and the system will throw an exception as form should be submitted by HTTPS. i can not hard code the action URL as currently its under develoment as the host name is localhost, even the HTTPS port is configurable so its even can not be hard-coded.

Is there any way i can create the URL in my JSP using JSTL or any other way so as to i can submit the form under HTTPS

回答1:

HttpServletRequest.getServerName() will give the server name. But, there is no way to get the https port. You should be using the default 443 if you want to fix it in your approach.

But the good solution is to use the Security Transport Guarantee for the login url, such that the server will automatically redirect the springSecurity/login page to https.

Add this entry to the web.xml which will automatically redirect the springSecurity/login to https.

<security-constraint>
    <web-resource-collection>
        <web-resource-name>secure</web-resource-name>
        <url-pattern>/springSecurity/login</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>