我正在使用Tomcat作为我的Struts2应用程序。 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>
如何更改上面列入黑名单的部分以仅使用白名单部分...例如,我需要将其他方法列入白名单,而不是将其列入黑名单,但我不确定将他们列入白名单的语法什么方法将它们列入白名单。
对于我上面的PUT
代码段,如果有人能为我提供上述DELTE
的白名单反对,我将不胜感激。
编辑:另外,我如何真正验证解决方案是否有效?
由于
答案 0 :(得分:20)
我会尝试以下方法:
<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方法。 (我没试过。)
答案 1 :(得分:9)
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
答案 2 :(得分:3)
对接受的答案稍作调整(将第url-pattern
中的security-constraint
设置为映射到默认的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只允许GET
和POST
。