Use HTTPS only for certain pages in servlet based webapp

試著忘記壹切 提交于 2020-01-01 09:22:39

问题


I have a servlet based webapp running on Tomcat 6 server. The URL scheme is HTTPS. The entire site is currently being served on HTTPS. But what I would really like to do is setup HTTPS only for certain operations like purchase and login. Is there any configuration in Tomcat that can help me do this easily?

Are there any code changes required to persist session across HTTPS and HTTP?


回答1:


Really, ideally, this is configured in your web app's web.xml file. You simply specify certain URLs that should be secure as <security-constraint><web-resource-collection> and specify HTTPS requirement as <transport-guarantee> with value of CONFIDENTIAL. The container will manage redirects transparently. Simple.

<security-constraint>
  <web-resource-collection>
     <web-resource-name>My Secure Stuff</web-resource-name>
     <url-pattern>/some/secure/stuff/*</url-pattern>
     <url-pattern>/other/secure/stuff/*</url-pattern>
     ...
  </web-resource-collection>
  <user-data-constraint>
     <transport-guarantee>CONFIDENTIAL</transport-guarantee>
  </user-data-constraint>
</security-constraint>



回答2:


You just need to setup a HTTP connector and all your servlet will be available on HTTP also.

For operations requiring HTTPS, you need to enforce this yourself like this,

if (!request.isSecure()) {
    response.sendError(HttpServletResponse.SC_FORBIDDEN);
    return;
}

In our case, the login URL may be typed in by user so we redirect the user to HTTPS page if HTTP URL is entered.

If you are talking about Servlet sessions (JSESSIONID), you shouldn't have any issues sharing sessions between HTTP and HTTPS since Tomcat doesn't add "secure" flag to the cookies.



来源:https://stackoverflow.com/questions/1330187/use-https-only-for-certain-pages-in-servlet-based-webapp

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