web.xml 并混合无授权和授权

web.xml and mixing no-auth and auth

我希望在单个 Web 应用程序中,部分使用身份验证,部分完全开放(或者更具体地说,不使用基于容器的身份验证)。

应用程序中使用基于容器的身份验证的部分位于 URL /,而开放的部分位于 URL /openpages。 (是的,我知道如果反过来可能会更容易,但不想打开应用程序的源代码)

这是我尝试 web.xml:

<web-app>
    ....
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>closedpages</web-resource-name>
            <url-pattern>/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
    </login-config>
</web-app>

因为我的 url-pattern 声明 / 而不是 /* 我认为它应该有效。但事实并非如此。无论我访问 http://myhost/ 还是 http://myhost/openpages/,我都会收到 HTTP 身份验证提示。只有 http://myhost/ 应该触发 HTTP 身份验证提示。

我的理解是,<security-constraint> 未明确涵盖的所有内容都是开放的,对吧?因此,/openpages/ 不应使用任何身份验证。

更多信息:我真的不喜欢 <login-config> 是在 webapp 级别指定的,而不是在每个安全约束级别指定的。这肯定会削弱灵活性吗?

默认情况下,security-constraint 未明确涵盖的所有内容都不会打开,为此使用 whitelisting(简而言之:没有 auth-constraintsecurity-constraint 可用于没有登录的任何人)。

另请注意,url-pattern 值并不完全直观,请参阅 this answer and an example of a multiple url-pattern matching problem in this question 中的详细信息(其中 /testresource.xml 同时匹配 /**.xml) .