2011-06-28 23 views
3

RESTEasy配置(使用2. *)或jax-rs是否有方法不允許http訪問任何基於REST的Web服務?我只想在https下提供Web服務端點。HTTPS和RESTEasy

回答

3

我是這樣做:

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來實現。

3

我相信這個配置不應該在RESTEasy方面,而應該在servlet容器或web服務器上。

例如,如果您運行Tomcat,請在server.xml中從8080端口移除連接器,併爲8443端口定義一個連接器。所以Tomcat不會再接受http流量。

4

在tomcat中,它在每個端口的基礎上完成。看起來有3個步驟來設置它。

1)創建KeyStore文件。我用java到根該命令如下

Keytool –genkey –alias presto –keypass prestoAdmin –keystore presto.bin –storepass prestoAdmin 

複製presto.bin文件到webapps Tomcat的

2)爲Tomcat

<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設置的server.xml的DIR )配置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