如下內容引用自http://wiki.jikexueyuan.com/project/jsp/security.html:html
JavaServer Pages和Servlets有幾種可用的機制可使Web開發人員用來保護應用程序。資源能夠經過在應用程序部署描述中對它們進行識別而且爲它們分配一個角色來聲明式地保護它們。java
有幾種級別的身份驗證是可用的,從使用基本標示符的基本驗證到複雜的使用證書的密碼驗證。web
1、基本角色的驗證apache
Servlet規範中的認證機制使用的是一項被稱爲基於角色的安全技術。該想法是經過角色來建立角色和限制資源,而不是限制用戶級別的資源。tomcat
能夠定義在文件tomcat-users.xml中定義不一樣的角色,該文件位於Tomcat的主頁目錄中的conf.中。此文件的一個示例以下所示:安全
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="role1" password="tomcat" roles="role1"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="admin" password="secret" roles="admin,manager"/> </tomcat-users>
這個文件在用戶名稱、密碼和角色之間定義了一個簡單的映射。請注意,一個給定的用戶可能有多個角色,例如,在「tomcat」角色中的用戶名=「both」,角色是「role1」。服務器
一旦識別和定義了不一樣的角色,一個基於角色的安全限制能夠經過使用<security-constraint>元素被放置在不一樣的Web應用程序中,該元素在WEB-INF目錄中的web.xml文件中是可用的。cookie
下面是web.xml中一個簡單的示例:session
<web-app> ... <security-constraint> <web-resource-collection> <web-resource-name> SecuredBookSite </web-resource-name> <url-pattern>/secured/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <description> Let only managers use this app </description> <role-name>manager</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>manager</role-name> </security-role> <login-config> <auth-method>BASIC</auth-method> </login-config> ... </web-app>
以上條目將意味着:app
任何經過/secured/*對一個URL匹配的HTTP GET或者POST請求都將被安全限制所接受。
一個有着管理員角色的人是能夠訪問被保護的資源的。
如今,若是試圖瀏覽任何包含/security目錄的URL,它將顯示一個詢問用戶名和密碼的對話框。若是提供一個用戶「admin」和密碼「secrer」,那麼能夠經過/secured/*訪問上面的URL,由於已經定義了用戶爲管理員角色,而該角色是有權訪問該資源的。
2、基於表單的身份驗證
當時使用表單身份驗證方法時,必須提供一個登陸表單來提示用戶輸入用戶名和密碼。下面是一個簡單登陸頁面login.jsp的代碼,用來建立一個相同目的的表單:
<html> <body bgcolor="#ffffff"> <form method="POST" action="j_security_check"> <table border="0"> <tr> <td>Login</td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="j_password"></td> </tr> </table> <input type="submit" value="Login!"> </center> </form> </body> </html>
在這裏,必須確保登陸表單中必須包含以j_username和j_password命名的表單元素。在<form>標籤中的動做必須是j_security_check。POST必須以表單的方法來使用。同時必須修改<login-config>標籤來指定auth-method做爲表單:
<web-app> ... <security-constraint> <web-resource-collection> <web-resource-name> SecuredBookSite </web-resource-name> <url-pattern>/secured/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <description> Let only managers use this app </description> <role-name>manager</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>manager</role-name> </security-role> <login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/error.jsp</form-error-page> </form-login-config> </login-config> ... </web-app>