get server name and port number of JSP page for HTTPS

烂漫一生 提交于 2019-12-13 04:43:28

问题


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>


来源:https://stackoverflow.com/questions/11115128/get-server-name-and-port-number-of-jsp-page-for-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!