2012-02-24 94 views
2

運行Tomcat 7,我試圖在Tomcat服務器上配置/conf/web.xml以保護一些具有基本身份驗證的URL並提供一些其他URL以供公共訪問。Tomcat 7 - 多個安全約束不起作用

將tomcat-users.xml中包含以下角色和用戶:

<role rolename="test-ui"/> 
<user username="paul" password="password" roles="test-ui"/> 

我加入了以下部分雄貓/conf/web.xml

<security-constraint> 
    <web-resource-collection> 
    <web-resource-name>Public access</web-resource-name> 
    <url-pattern>/docs/*</url-pattern> 
    </web-resource-collection> 
</security-constraint> 

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Protected access</web-resource-name> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>test-ui</role-name> 
    </auth-constraint> 
</security-constraint> 

<security-role> 
    <description>Protected access</description> 
    <role-name>test-ui</role-name> 
</security-role> 

<login-config> 
    <auth-method>BASIC</auth-method> 
</login-config> 

所以有兩個「安全約束「元素,公共的不包含'auth-constraint',這實際上應該表示沒有必要的認證。

當我打開網址 http://localhost:8080

Tomcat的要求進行身份驗證。 這是好的,但是當我打開網址 http://localhost:8080/docs/

的Tomcat也要求進行身份驗證和我的理解,這是設置爲「非安全」 URL - 如此公開acccess,但它並沒有這樣的表現。

我在配置中出了什麼問題,或者這種情況不應該像這樣工作?

謝謝。 保羅

+0

您是否嘗試將第二個衝突移動到頂端? – home 2012-02-24 16:21:45

+0

謝謝,但是,我試過了。效果是一樣的。沒有身份驗證,我無法訪問/文檔。 – 2012-02-24 16:25:00

回答

0

您需要在<安全約束>的< AUTH約束>節點,即使它是空例如< auth-constraint/>

+1

不,空的節點表示禁止訪問,其中節中的表示不受限制的訪問。至少這在Java Servlet Specification Version 3.0中有說明。 – 2012-03-02 08:40:47

0

如果安全約束不存在,容器必須允許未經身份驗證的訪問這些URL。安全約束是可選的。

+0

你能提供一些支持細節嗎? – 2013-10-03 15:15:07