【发布时间】:2011-06-28 17:26:25
【问题描述】:
RESTEasy 配置(使用 2.*)或 jax-rs 中是否有方法不允许 http 访问任何基于 REST 的 Web 服务?我只想在 https 下提供 Web 服务端点。
【问题讨论】:
RESTEasy 配置(使用 2.*)或 jax-rs 中是否有方法不允许 http 访问任何基于 REST 的 Web 服务?我只想在 https 下提供 Web 服务端点。
【问题讨论】:
在 tomcat 中,它基于每个端口完成。设置它似乎需要 3 个步骤。
1) 创建 KeyStore 文件。我用java生成这个命令如下
Keytool –genkey –alias presto –keypass prestoAdmin –keystore presto.bin –storepass prestoAdmin
将presto.bin文件复制到tomcat的webapps目录中
2) 为tomcat设置server.xml
<Connector port=”PORT_TO_BE_SCURED” maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile../webapps/presto.bin " keystorePass="prestoAdmin"
clientAuth="false" sslProtocol="TLS"/>
3) 配置 Web 服务以使用安全连接。将以下内容添加到 web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>securedapp</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
我从 http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html 那里提取了这个
【讨论】:
我是这样做的:
HttpServletRequest httpServletRequest =
ResteasyProviderFactory.getContextData(HttpServletRequest.class);
HttpServletResponse httpServletResponse =
ResteasyProviderFactory.getContextData(HttpServletResponse.class);
if (!httpServletRequest.isSecure())
{
try
{
httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "Use HTTPS");
}
catch (IOException e)
{
throw new WebApplicationException(e);
}
}
这是纯 RESTEasy 解决方案,您可以在处理请求之前将此代码放在任何地方。
我使用了tapestry-resteasy 集成并使用tapestry service advisors 实现了这一点。
【讨论】:
我认为这个配置不应该在 RESTEasy 端,而应该在 servlet 容器或 Web 服务器上。
例如,如果您运行 Tomcat,则在 server.xml 中从 8080 端口删除连接器并为 8443 端口定义一个。所以Tomcat不会再接受http流量了。
【讨论】: