我有一个基于Tomcat 6服务器的servlet Web应用程序。URL方案是HTTPS。整个站点目前都在HTTPS上提供服务。但我真正想做的是仅为购买和登录等某些操作设置HTTPS。Tomcat中是否有任何配置可以帮助我轻松完成此操作?
在HTTPS和HTTP之间持续会话是否需要进行任何代码更改?
在HTTPS和HTTP之间持续会话是否需要进行任何代码更改?
理想情况下,您的web应用程序的web.xml文件中应该配置这个。您只需将特定的URL指定为<security-constraint><web-resource-collection>
并将HTTPS要求指定为<transport-guarantee>
,值为CONFIDENTIAL
。容器会自动管理重定向。简单明了。
<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>
您只需要设置一个HTTP连接器,那么您的所有Servlet也将在HTTP上可用。
对于需要使用HTTPS的操作,您需要自己进行强制执行,如下所示:
if (!request.isSecure()) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}