How to disable certain HTTP methods (PUT, DELETE, TRACE and OPTIONS) in JBOSS7

本文介绍了三种禁用特定HTTP方法的方法:使用RewriteValve全局配置、通过web.xml中的安全约束进行WAR级别的限制以及利用Apache httpd的mod_rewrite模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Resolution

Option 1 -Using RewriteValve (can apply globally)

You can use RewriteValve to disable the http methods. Take a look atdocumentation http://docs.jboss.org/jbossweb/2.1.x/rewrite.html.You will need one RewriteCond directive and one RewriteRule.

In your RewriteCond directive you could specify all methods with use of the REQUEST_METHOD servervariable, for example:

RewriteCond %{REQUEST_METHOD} ^(PUT|DELETE|TRACE|OPTIONS)$ [NC]

then your RewriteRule can mark those as forbidden (it immediately sends back aHTTP response of 403 (FORBIDDEN)), for example:

RewriteRule .* - [F]

For EAP6:

RewriteValve can be configured asglobal valve in domain.xml or standalone.xml. You can add the <rewrite> tag to the <virtual-server> configuration of the web subsystem.

.. ..

<subsystem xmlns="urn:jboss:domain:web:1.1"default-virtual-server="default-host" native="false">

    <connector name="http" protocol="HTTP/1.1"scheme="http" socket-binding="http"/>

    <virtual-server name="default-host"enable-welcome-root="true">

        <rewritepattern=".*" substitution="-" flags="F">

           <condition test="%{REQUEST_METHOD}"pattern="^(PUT|DELETE|TRACE|OPTIONS)$" flags="NC" />

    </rewrite>

    </virtual-server>

</subsystem>

.. ..

Option 2 - web.xml Security constraints(per WAR)

This can be done by adding security constraints to theapplication's web.xml. For example:

.. ..

<security-constraint>

    <web-resource-collection>

       <web-resource-name>NoAccess</web-resource-name>

       <url-pattern>/*</url-pattern>

         <http-method>DELETE</http-method>

         <http-method>PUT</http-method>

         <http-method>OPTIONS</http-method>

         <http-method>TRACE</http-method>

         <http-method>POST</http-method>

    </web-resource-collection>

    <auth-constraint/>

</security-constraint>

.. ..

In the above example, access the following http requests DELETE, PUT, OPTIONS, POST aredisabled by default.

You can also restrict all methods other than explicitlyallowed ones by doing like:

.. ..

<security-constraint>

    <web-resource-collection>

       <web-resource-name>NoAccess</web-resource-name>

       <url-pattern>/*</url-pattern>   

    </web-resource-collection>   

    <auth-constraint/>

</security-constraint>

<security-constraint> 

    <web-resource-collection>    

        <web-resource-name>AllowedMethods</web-resource-name>    

        <url-pattern>/*</url-pattern>    

          <http-method>GET</http-method>

         <http-method>POST</http-method>

         <http-method>HEAD</http-method>

    </web-resource-collection>

</security-constraint>

.. ..

See the Java ServletSpecification and also The Java EE 5Tutorial - "Declaring Security Requirements in a DeploymentDescriptor" for more information.

Option 3 -Using Apache httpd mod_rewrite in front of JBoss

If you are fronting JBoss with Apache httpd, you can alsoapply the above rewrite rules in the httpd.conf.:

For example:

RewriteEngine On

 

RewriteCond %{REQUEST_METHOD} ^(PUT|DELETE|TRACE|OPTIONS)$ [NC]

RewriteRule .* - [F]

To verify theabove configuration:

You can use curl command to test if the configuration change iseffective: For example:

curl -v -XTRACE http://hostname:port/appContext

curl -v -XDELETE http://hostname:port/appContex

在Web服务器中,如Tomcat,禁用特定HTTP方法是确保Web应用程序安全的重要步骤。特别是针对WebDAV所使用的DELETEPUTTRACEOPTIONS方法,这些方法若被恶意使用,可能导致未授权的文件操作或信息泄露。要禁用这些方法,可以通过配置安全约束(security-constraint)来实现。以下是详细的操作步骤: 参考资源链接:[禁用Tomcat中间件WebDAV方法的配置教程](https://wenku.youkuaiyun.com/doc/482kyg9xeh?spm=1055.2569.3001.10343) 首先,找到Tomcat服务器的配置文件`web.xml`。这个文件位于`conf`目录下,其中包含了所有Web应用程序的全局配置。 在`web.xml`文件的适当位置,添加以下安全约束配置段落: ```xml <security-constraint> <web-resource-collection> <web-resource-name>Disable Dangerous Methods</web-resource-name> <url-pattern>/*</url-pattern> <http-method-omission>DELETE</http-method-omission> <http-method-omission>PUT</http-method-omission> <http-method-omission>TRACE</http-method-omission> <http-method-omission>OPTIONS</http-method-omission> </web-resource-collection> <auth-constraint /> </security-constraint> ``` 在这个配置中,`<http-method-omission>`元素指定了要忽略的方法,即不允许这些方法被Web应用程序使用。`<auth-constraint />`表示不指定任何角色,意味着所有用户都将被禁止使用这些方法,而不需要进行身份验证。 在完成配置后,保存`web.xml`文件,并重启Tomcat服务器以使新的安全配置生效。这样,所有对服务器发起的DELETEPUTTRACEOPTIONS请求都将被拦截和拒绝。 为确保更全面的安全,建议采取以下额外措施: - 定期更新Tomcat到最新版本,以修补已知漏洞。 - 配置防火墙规则,限制对Tomcat服务器端口的访问。 - 启用SSL/TLS加密通信。 - 对敏感应用程序强制使用HTTPS,并确保SSL连接。 - 监控服务器日志,以便及时发现可疑活动。 了解如何在Tomcat中配置安全约束,是保护Java Web项目免受未授权访问威胁的关键。为了更深入地理解和实施安全最佳实践,推荐阅读《禁用Tomcat中间件WebDAV方法的配置教程》。该资料不仅详细解释了禁用WebDAV方法的步骤,还提供了其他安全加固措施,有助于构建更加安全和可靠的服务器环境。 参考资源链接:[禁用Tomcat中间件WebDAV方法的配置教程](https://wenku.youkuaiyun.com/doc/482kyg9xeh?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值