標籤:resource auth pattern post 標識 int gui security dmi
要進行basic驗證是使用者名稱/口令機制,當瀏覽器要訪問受保護的資源時,伺服器會要求一個使用者名稱和口令,只有輸入了合法的使用者名稱和口令。伺服器才發送資源。使用者名稱和口令可以儲存在安全域中。安全域是標識一個Web應用程式的合法使用者名稱和口令的“資料庫”,其中還包含了與使用者相關的角色。
例子:使用basic和MemoryRealm登入
1、在tomcat下的/conf/tomcat-users.xml定義了角色和使用者,還有一些角色
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<user username="li" password="123456" roles="admin-gui,manager-gui" />
<role rolename="manager"/>
<user username="admin" password="123456" roles="manager"/>
</tomcat-user>
2、在web.xml中定義如下:
<security-constraint>
<web-resource-collection>
<web-resource-name>adminResource</web-resource-name>
<url-pattern>/AccountServlet</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description>allManager</description>
<role-name>manager</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>test</realm-name>
</login-config>
<security-role>
<role-name>manager</role-name>
</security-role>
其中<security-constraint>標籤設定受保護的資源和可以訪問的使用者角色;
<login-config>是驗證機制,訪問受保護的資源,彈出的對話方塊不同,它的<realm-name>test</realm-name>;是彈出的對話方塊中顯示的安全域的名
<security-role>是用來定義,使用的角色
這樣就可對資源進行保護,在訪問資源時,在彈出的對話方塊中輸入之前定義的使用者,密碼就可以訪問了
Java web的安全約束--Basic驗證