2017-06-13 66 views
0

我在web.xml中JSP Tomcat安全約束總是失敗

<security-constraint> 
    <web-resource-collection> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>rolename</role-name> 
    </auth-constraint> 
</security-constraint> 
<login-config> 
    <auth-method>BASIC</auth-method> 
</login-config> 
<security-role> 
    <role-name>rolename</role-name> 
</security-role> 

以下,也有我的tomcat-users.xml文件如下:

<role rolename="rolename"/> 
<user username="username" password="password" roles="rolename"/> 

認證請求當我嘗試導航到我的本地主機站點時會出現對話框,但即使我輸入了正確的憑證,對話框也會刷新本身,並且沒有任何反應。

爲什麼這不起作用的原因?看起來不像認證方法所建議的那麼基本。

+0

您是否在使用IDE?還是你直接運行Tomcat? –

+0

我正在使用NetBeans 8.2。我已經將上面的代碼部署到了我的網絡服務器上,並且它可以正常工作。所以看起來像在本地主機開發機器上需要一些不同的東西。我試圖通過命令行來啓動和停止服務器,以排除IDE。 –

回答

1

設置看起來是正確的,當你將你配置管理器或管理腳本角色的現有用戶和密碼的服務器時使用NetBeans,例如,如果您有:

enter image description here

如果您使用的是誰上臺嵌入Netbeans的有Tomcat的文件緩存的地方Tomcat服務器,如:

C:\用戶\用戶\應用程序數據\漫遊\ NetBeans的\ 8.2 \ Apache的Tomcat的8.0 .27.0_base \ CONF

enter image description here

如果你使用Eclipse IDE中的緩存文件的tomcat停留在文件夾中:

C:\項目\工作區\服務器\ Tomcat的V8.5服務器在本地主機,配置

enter image description here

+0

非常感謝這個,我使用的是NetBeans,是的,它使用自己的tomcat-users.xml文件,這解釋了爲什麼它不起作用,因爲我的用戶和角色不在那裏。 –

0

也許你忘了將安全角色標籤放在安全約束標籤的級別。

<security-role> 
     <role-name>rolename</role-name> 
    </security-role> 

    <security-constraint> 
     <web-resource-collection> 
      <url-pattern>/*</url-pattern> 
     </web-resource-collection> 
     <auth-constraint> 
      <role-name>rolename</role-name> 
     </auth-constraint> 
    </security-constraint> 
    <login-config> 
     <auth-method>BASIC</auth-method> 
    </login-config> 

看看這裏的一個例子

http://docs.oracle.com/javaee/5/tutorial/doc/bncbe.html

+0

我在security-constraint之外添加了security-role標籤(與您的示例中的級別相同),但這仍然無法解決問題。 –