<security-constraint> 的子元素 <http-method> 是可選的,若是沒有 <http-method> 元素,這表示將禁止全部 HTTP 方法訪問相應的資源。
子元素 <auth-constraint> 須要和 <login-config> 相配合使用,但能夠被單獨使用。若是沒有 <auth-constraint> 子元素,這代表任何身份的用戶均可以訪問相應的資源。也就是說,若是 <security-constraint> 中沒有 <auth-constraint> 子元素的話,配置其實是不起中用的。若是加入了 <auth-constraint> 子元素,可是其內容爲空,這表示全部身份的用戶都被禁止訪問相應的資源。
--------------------------------------------------------------------------------------------------------------------------------------------web.xml 中的代碼
Xml代碼
<security-constraint>
<display-name>
baseporject</display-name>
<web-resource-collection>
<web-resource-name>baseproject</web-resource-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
<http-method>GET</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>TRACE</http-method>
<http-method>POST</http-method>
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint>
<description>
baseproject</description>
<role-name>All Role</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<security-constraint>
<display-name>
baseporject</display-name>
<web-resource-collection>
<web-resource-name>baseproject</web-resource-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
<http-method>GET</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>TRACE</http-method>
<http-method>POST</http-method>
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint>
<description>
baseproject</description>
<role-name>All Role</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>Xml代碼
<!--四種驗證方式,附在最後有說明-->
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>All Role</role-name>
</security-role>
<!--四種驗證方式,附在最後有說明-->
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>All Role</role-name>
</security-role> 這裏的security-constriaint元素的用途是用來指示服務器使用何種驗證方法了.此元素在web.xml中應該出如今login- config的緊前面。它包含是個可能的子元素,分別是:web-resource-collection、auth-constraint、user- data-constraint和display-name。下面各小節對它們進行介紹。
1. web-resource-collection 此元素肯定應該保護的資源,全部security-constraint元 素都必須包含至少一個web- resource-collection項.此元素由一個給出任意標識名稱的web-resource-name元素、一個肯定應該保護URL 的url-pattern元素、一個指出此保護所適用的HTTP命令(GET、POST等,缺省爲全部方法)的http-method元素和一個提供資料 的可選description元素組成。
重要的是應該注意到,url-pattern僅適用於直接訪問這些資源的客戶機。特別是,它不適合於經過MVC體系結構利用RequestDispatcher來訪問的頁面,或者不適合於利用相似jsp:forward的手段來訪問的頁面。
2. auth-constraint元素卻指出哪些用戶應該具備受保護資源的訪問權。此元素應該包含一個或多個標識具備訪問權限的用戶類別role-name元素,以及包含(可選)一個描述角色的description元素。
3. user-data-constraint
這個可選的元素指出在訪問相關資源時使用任何傳輸層保護。它必須包含一個transport-guarantee子元素(合法值爲NONE、 INTEGRAL或CONFIDENTIAL),而且可選地包含一個description元素。transport-guarantee爲NONE值將 對所用的通信協議不加限制。INTEGRAL值表示數據必須以一種防止截取它的人閱讀它的方式傳送。雖然原理上(而且在將來的HTTP版本中),在 INTEGRAL和CONFIDENTIAL之間可能會有差異,但在當前實踐中,他們都只是簡單地要求用SSL。
=========================================================
四種認證類型:
BASIC:HTTP規範,Base64
<web-app>
......
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
......
</web-app>
DIGEST:HTTP規範,數據完整性強一些,但不是SSL
<web-app>
......
<login-config>
<auth-method>DIGEST</auth-method>
</login-config>
......
</web-app>
CLIENT-CERT:J2EE規範,數據完整性很強,公共鑰匙(PKC)
<web-app>
......
<login-config>
<auth-method>CLIENT-CERT</auth-method>
</login-config>
......
</web-app>
FORM:J2EE規範,數據完整性很是弱,沒有加密,容許有定製的登錄界面。
<web-app>
......
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
......
</web-app>
這裏的 FORM 方式須要說明的是 登陸頁面的固定的元素:login.html
<form name="loginform" method="post" action="j_security_check">
<INPUT name="j_username" type="text">
<INPUT name="j_password" TYPE="password">
<input type="submit" value="登 錄" >
</form>
form 的action 必須是j_security_check, method="post", 用戶名 name="j_username" , 密碼name="j_password" 這些都是固定的元素html