<security-constraint> 的子元素 <http-method> 是可選的,如果沒有 <http-method> 元素,這表示將禁止所有 HTTP 方法訪問相應的資源。
子元素 <auth-constraint> 需要和 <login-config> 相配合使用,但可以被單獨使用。如果沒有 <auth-constraint> 子元素,這表明任何身份的用戶都可以訪問相應的資源。也就是說,如果 <security-constraint> 中沒有 <auth-constraint> 子元素的話,配置實際上是不起中用的。如果加入了 <auth-constraint> 子元素,但是其內容為空,這表示所有身份的用戶都被禁止訪問相應的資源。
web.xml代碼:
子元素 <auth-constraint> 需要和 <login-config> 相配合使用,但可以被單獨使用。如果沒有 <auth-constraint> 子元素,這表明任何身份的用戶都可以訪問相應的資源。也就是說,如果 <security-constraint> 中沒有 <auth-constraint> 子元素的話,配置實際上是不起中用的。如果加入了 <auth-constraint> 子元素,但是其內容為空,這表示所有身份的用戶都被禁止訪問相應的資源。
web.xml代碼:
1.<security-constraint> 2. <display-name> 3. baseporject</display-name> 4. <web-resource-collection> 5. <web-resource-name>baseproject</web-resource-name> 6. <url-pattern>*.jsp</url-pattern> 7. <url-pattern>*.do</url-pattern> 8. <http-method>GET</http-method> 9. <http-method>PUT</http-method> 10. <http-method>HEAD</http-method> 11. <http-method>TRACE</http-method> 12. <http-method>POST</http-method> 13. <http-method>DELETE</http-method> 14. <http-method>OPTIONS</http-method> 15. </web-resource-collection> 16. <auth-constraint> 17. <description> 18. baseproject</description> 19. <role-name>All Role</role-name> 20. </auth-constraint> 21. <user-data-constraint> 22. <transport-guarantee>NONE</transport-guarantee> 23. </user-data-constraint> 24.</security-constraint> 25.<login-config> 26. 27.<security-constraint> 28. <display-name> 29. baseporject</display-name> 30. <web-resource-collection> 31. <web-resource-name>baseproject</web-resource-name> 32. <url-pattern>*.jsp</url-pattern> 33. <url-pattern>*.do</url-pattern> 34. <http-method>GET</http-method> 35. <http-method>PUT</http-method> 36. <http-method>HEAD</http-method> 37. <http-method>TRACE</http-method> 38. <http-method>POST</http-method> 39. <http-method>DELETE</http-method> 40. <http-method>OPTIONS</http-method> 41. </web-resource-collection> 42. <auth-constraint> 43. <description> 44. baseproject</description> 45. <role-name>All Role</role-name> 46. </auth-constraint> 47. <user-data-constraint> 48. <transport-guarantee>NONE</transport-guarantee> 49. </user-data-constraint> 50.</security-constraint> 51.<login-config>Xml代碼 52.<!--四種驗證方式,附在最后有說明--> 53. <auth-method>FORM</auth-method> 54. <form-login-config> 55. <form-login-page>/login.html</form-login-page> 56. <form-error-page>/error.html</form-error-page> 57. </form-login-config> 58.</login-config> 59.<security-role> 60. <role-name>All Role</role-name> 61.</security-role> 62. 63.<!--四種驗證方式,附在最后有說明--> 64. <auth-method>FORM</auth-method> 65. <form-login-config> 66. <form-login-page>/login.html</form-login-page> 67. <form-error-page>/error.html</form-error-page> 68. </form-login-config> 69.</login-config> 70.<security-role> 71. <role-name>All Role</role-name> 72.</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。
4 四種認證類型:
-
代碼:
1.BASIC:HTTP規范,Base64 2.<web-app> 3. ...... 4. <login-config> 5. <auth-method>BASIC</auth-method> 6. </login-config> 7. ...... 8.</web-app> 9. 10.DIGEST:HTTP規范,數據完整性強一些,但不是SSL 11.<web-app> 12. ...... 13. <login-config> 14. <auth-method>DIGEST</auth-method> 15. </login-config> 16. ...... 17.</web-app> 18. 19.CLIENT-CERT:J2EE規范,數據完整性很強,公共鑰匙(PKC) 20.<web-app> 21. ...... 22. <login-config> 23. <auth-method>CLIENT-CERT</auth-method> 24. </login-config> 25. ...... 26.</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>
