java - 上下文根的 Web.xml 安全约束不适用

标签 java servlets jboss web.xml security-constraint

我有一个使用 web.xml 来配置其安全性的 java webapp:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>webPages</web-resource-name>
        <description>All web resources</description>
        <url-pattern></url-pattern>
        <url-pattern>/admin/*</url-pattern>
        <http-method>POST</http-method>
        <http-method>GET</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admins</role-name>
    </auth-constraint>
    <user-data-constraint>
        <description>SSL not required</description>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

我希望/admin/* 下的所有页面都受到保护,这很有效。用户正确地首先看到一个登录屏幕,然后被重定向到原始请求的页面。

我还希望我的上下文根受到保护:http://host:port/context/但是,当我配置模式时 <url-pattern></url-pattern>并向 root 发出请求,我的 java Controller 刚刚开始工作并显示 View ,而用户从未看到登录屏幕。为什么这种模式适用于 <servlet-mapping> 之类的东西(将请求映射到 spring servlet)但不作为安全约束?

我在 chrome 和 firefox 中都试过并重新启动了多次。

最佳答案

您可以尝试白名单方法,这意味着只允许公共(public)资源访问。

Here is a better answer例如,但在您的情况下应该是这样的:

<security-constraint>
  <web-resource-collection>
    <web-resource-name>webPages</web-resource-name>
    <description>All web resources</description>
    <url-pattern>/</url-pattern>
    <http-method>POST</http-method>
    <http-method>GET</http-method>
  </web-resource-collection>
  <auth-constraint>
    <role-name>admins</role-name>
  </auth-constraint>
  <user-data-constraint>
    <description>SSL not required</description>
    <transport-guarantee>NONE</transport-guarantee>
  </user-data-constraint>
</security-constraint>
<security-constraint>   
  <web-resource-collection>
    <web-resource-name>Public Resources</web-resource-name>
    <url-pattern>/public/*</url-pattern>
    <url-pattern>/alsopublic</url-pattern>
    <url-pattern>...an so on...</url-pattern>
  </web-resource-collection>  
  <!-- to given public access don't set auth-constraint-->
</security-constraint>

编辑: 引用 servlet 3 specification

关于java - 上下文根的 Web.xml 安全约束不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38123425/

相关文章:

java - 当覆盖多个接口(interface)时,你能确定调用者的上下文吗?

apache - 配置 Tomcat 以利用浏览器缓存?

java - 在 JBoss v7.1 中使用 Seam v2.2.2.Final 和 JSF v1.2 – JSF 标签不会呈现 – 为什么?

java.lang.OutOfMemory错误: Java heap space in every 2-3 Hours

java - jdeveloper 11g 11112 出现错误

java - 用 Axis2 错误 XML 响应替换 JBoss 错误页面

Java和从int获取枚举的方法的命名

java - 如何从 Java 中的文件资源加载资源包?

java - 如何将 ComboBox 的选定项设置为另一个值?

java - 从 Java 类启动 Servlet,就像从 HTML 代码启动一样