我只想对Jetty WebApp的上下文路径上的根目录进行密码保护。我的上下文路径是/MyApp,因此我需要密码才能访问:
http://localhost:8080/MyApp但不是为了:
http://localhost:8080/MyApp/cometd我当前的设置如下(请注意url-pattern):
<security-constraint>
<web-resource-collection>
<web-resource-name>Private Page</web-resource-name>
<url-pattern>/</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>moderator</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Test Realm</realm-name>
</login-config>根据/和/*一般的工作方式,我希望它能正常工作。我还看到了这个参考资料,我认为它建议这应该很有效:http://www.coderanch.com/t/364782/Servlets/java/there-key-difference-between-url
但是,对于我的情况,url模式:
<url-pattern>/</url-pattern>和
<url-pattern>/*</url-pattern>看起来都是一样的:
http://localhost:8080/MyApp 和
http://localhost:8080/MyApp/cometd都有密码保护。
当然,如果我更改为/nothingishere,就像一个健全的测试一样,除了/MyApp/nothingishere之外,没有任何东西是受密码保护的
有人知道如何只保护web的根目录吗?
发布于 2012-07-19 06:16:04
这是给你的答案:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Private Page</web-resource-name>
<url-pattern>/</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>moderator</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Public page</web-resource-name>
<url-pattern>/test/*</url-pattern>
</web-resource-collection>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Test Realm</realm-name>
</login-config>
</web-app>在此配置中,根目录受密码保护,而/test/...目录则没有。我想这就是你所要求的。
这个配置在Tomcat 7+和NetBeans中从头创建的一个新项目上进行了测试(如果您需要,我可以通过电子邮件将整个源代码发送给您)。
这是输出:

https://stackoverflow.com/questions/11509716
复制相似问题