gpt4 book ai didi

web-applications - 仅将 jetty url-pattern 匹配到根目录

转载 作者:行者123 更新时间:2023-12-04 07:25:07 25 4
gpt4 key购买 nike

我只想密码保护我的 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-pattern>/</url-pattern>


<url-pattern>/*</url-pattern>

似乎表现得完全一样:两者
http://localhost:8080/MyApp 


http://localhost:8080/MyApp/cometd

都受密码保护。

当然,如果我更改为/nothingishere,作为健全性测试,除了/MyApp/nothingishere 之外,没有任何东西是受密码保护的

有谁知道如何只保护 web servlet 的根目录?

最佳答案

这是给你的答案:

<?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 中创建的新项目上进行测试(如果您需要,我可以通过电子邮件将整个源发送给您)。

这是输出:
output

关于web-applications - 仅将 jetty url-pattern 匹配到根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com