【发布时间】:2025-11-23 10:25:02
【问题描述】:
我有一个使用 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> 并向根发出请求时,我的 java 控制器开始工作并显示视图,而用户从未看到登录屏幕。为什么这种模式适用于 <servlet-mapping>(将请求映射到 spring servlet)之类的东西,而不是作为安全约束?
我在 chrome 和 firefox 中都尝试过,并多次重启。
【问题讨论】:
-
您是否使用 /* 进行根上下文配置?
-
@aksappy 不,因为 /* 作为 url 模式意味着“捕获所有请求”,我不想要那个,只有根上下文。例如,/otherpage.do 应该在没有授权的情况下继续工作。
-
按照规范,你做的是对的。
标签: java servlets jboss web.xml security-constraint