在web.xml中添加白名单安全限制

28

我在我的Struts2应用程序中使用Tomcat。 web.xml 包含以下特定条目:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>restricted methods</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>PUT</http-method>
        <http-method>DELETE</http-method>
        <http-method>TRACE</http-method>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>
<security-constraint>
   <web-resource-collection>
       <web-resource-name>no_access</web-resource-name>
       <url-pattern>/jsp/*</url-pattern>
   </web-resource-collection>
   <auth-constraint/>
</security-constraint>
    <security-constraint>
   <web-resource-collection>
       <web-resource-name>no_access</web-resource-name>
       <url-pattern>/myrrunner/*</url-pattern>
   </web-resource-collection>
   <auth-constraint/>
</security-constraint>

如何将上面被列入黑名单的部分更改为仅使用白名单部分... 例如,不是将PUTDELTE http方法列入黑名单,而是需要将其他方法列入白名单,但我不确定白名单语法和需要列入白名单的方法。

对于我上面的web.xml片段,如果有人可以为我提供与上述xml相对应的白名单,请让我表示感谢。

编辑:另外,我该如何真正验证解决方案是否有效?

谢谢


1
这对我来说有点不明确。你能否发布一个应该可用的资源清单? - palacsint
我需要将除PUT、DELETE和TRACE之外的所有HTTP方法列入白名单...我们该怎么做? - Mike
使用:http-method-omission - Umesh Bhutada
3个回答

25
我会尝试以下方法:
<security-constraint>
    <web-resource-collection>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <!-- no auth-constraint tag here -->
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>restricted methods</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
   <auth-constraint/>
</security-constraint>

第一个security-constraint没有任何auth-constraint,因此GET和POST方法对任何人都可用而无需登录。第二个约束其他HTTP方法对所有人都不可用。(我没有尝试过。)


嗨...我不想被列入黑名单...有没有办法白名单而不是限制? - Mike
1
第一个“security-constraint”是白名单。第二个只禁止其他(如第一个)“security-constraint”标签不允许的所有内容。 - palacsint
我建议注意使用HEAD方法时的安全漏洞CVE-2010-0738:http://www.fishnetsecurity.com/6labs/blog/jboss-jmx-console-authentication-bypass - Vadzim
由于某些原因,我无法让它正常工作...也许这是一个tomcat的bug。有人告诉我解决方法是安装apache httpd并使用jk将jsp定向到tomcat。 - Sun
我发现我正在使用4.0.6版本,而上述内容似乎在4.0.6中无法工作。 - Sun
显示剩余4条评论

17

Java EE 6的新功能,简化了应用程序的安全配置。您现在可以在web.xml中白名单与黑名单允许的HTTP方法:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Disable unneeded HTTP methods by 403 Forbidden them</web-resource-name>
        <url-pattern>*</url-pattern>
        <http-method-omission>GET</http-method-omission>
        <http-method-omission>HEAD</http-method-omission>
        <http-method-omission>POST</http-method-omission>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>

参考资料:https://docs.oracle.com/cd/E19798-01/821-1841/bncbk/index.html#6nmq2cpkb


3

对已有答案进行轻微修改(在第二个security-constraint中设置 url-pattern 映射到默认servlet "/")适用于JBoss和Weblogic,但不适用于Websphere:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Allowed methods</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <!-- no auth-constraint tag here -->
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Restricted methods</web-resource-name>
        <url-pattern>/</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>

在上述安全约束配置中,我不确定为什么WebSphere允许所有HTTP方法,而JBoss和Weblogic只允许GETPOST


在安全约束生效之前,发现了一篇关于启用WebSphere通用应用程序安全性的文章(http://stackoverflow.com/questions/5067917/websphere-security-constraint-in-web-xml-doesnt-work)。 - mendozal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接