我有一个使用 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/