以下內容引用自http://wiki.jikexueyuan.com/project/jsp/security.html:
JavaServer Pages和Servlets有幾種可用的機制可以使Web開發人員用來保護應用程序。資源可以通過在應用程序部署描述中對它們進行識別並且為它們分配一個角色來聲明式地保護它們。
有幾種級別的身份驗證是可用的,從使用基本標示符的基本驗證到復雜的使用證書的密碼驗證。
一、基本角色的驗證
Servlet規范中的認證機制使用的是一項被稱為基於角色的安全技術。該想法是通過角色來創建角色和限制資源,而不是限制用戶級別的資源。
可以定義在文件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文件中是可用的。
下面是web.xml中一個簡單的示例:
<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>
以上條目將意味着:
-
任何通過/secured/*對一個URL匹配的HTTP GET或者POST請求都將被安全限制所接受。
-
一個有着管理員角色的人是可以訪問被保護的資源的。
- 最后,login-config元素是用來描述身份驗證的BASIC形式。
現在,如果試圖瀏覽任何包含/security目錄的URL,它將顯示一個詢問用戶名和密碼的對話框。如果提供一個用戶“admin”和密碼“secrer”,那么可以通過/secured/*訪問上面的URL,因為已經定義了用戶為管理員角色,而該角色是有權訪問該資源的。
二、基於表單的身份驗證
當時使用表單身份驗證方法時,必須提供一個登錄表單來提示用戶輸入用戶名和密碼。下面是一個簡單登錄頁面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>