【问题标题】:How do I ensure https is used for only some web services in apache tomcat如何确保 https 仅用于 apache tomcat 中的某些 Web 服务
【发布时间】:2014-04-29 10:19:40
【问题描述】:
我在 apache tomcat 中部署了一些 Web 服务,并且所有 Web 服务都可以通过 http 和 https 访问。我希望很少有 Web 服务只能通过 https 而不是 http 访问。是否可以在 apache tomcat 或 cxf 或 spring 框架中进行配置。
对此的任何帮助都会对我非常有用。
洛克希
【问题讨论】:
标签:
spring
web-services
tomcat
tomcat7
cxf
【解决方案1】:
要检查 HTTP 请求是否安全,只需调用 ServletRequest.isSecure()。然后在检查后您可以重定向到仅安全的版本,反之亦然。
以上所有内容都可以在很多地方完成,但在我看来,最简单的方法是编写一个servlet filter,它可以透明地处理上述检查和重定向。
注意:如果您的 Tomcat 位于 Apache 或负载均衡器之后,并且 SSL 在此过程中提前终止,则上述方法可能不起作用。
【解决方案2】:
在 web.xml 中将 <transport-guarantee> 设置为 CONFIDENTIAL 允许您对某些页面要求 SSL。
看起来像这样:
<security-constraint>
<web-resource-collection>
<web-resource-name>SSL forwarding</web-resource-name>
<url-pattern>/secured</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>
CONFIDENTIAL
</transport-guarantee>
</user-data-constraint>
</security-constraint>
希望这会有所帮助。