首先在 tomcat-users.xml中配置css
<tomat-users> <role rolename="Admin"/> <role rolename="Member"/> <role rolename="Guest"/> <user username="Stupid Wolf" password="admin" roles="Admin, Member, Guest"/> </tomcat-users>
接着在 web.xml中配置html
<security-role> <role-name>Admin</role-name> </security-role> <!--受權時,容器將把開發商特定的role信息映射到這裏的找到的<role-name>中 -->
定義資源/方法約束web
<security-constraint> <web-resource-collection> <web-resource-name>Resources</web-resource-name> <url-pattern>/url of files</url-pattern> <http-method>POST</http-method> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>Admin</role-name> </auth-constraint> </security-constraint>
在web.xml添加tomcat
<login-config> <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.html error.html的代碼分別爲jsp
<!DOCTYPE html> <html> <head> <title>login.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form name="f1" id="f1" action="j_security_check" method="post"> <table> <tr> <td>Login:</td> <td><input type="text" name="j_username" id="login"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="j_password" id="password"></td> </tr> <tr> <td colspan="2"><input type="submit"></td> </tr> </table> </form> </body> </html>
<!DOCTYPE html> <html> <head> <title>error.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <meta charset="UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> error! 身份認證錯誤 <br> </body> </html>
最後,咱們也就完成了未經認證的客戶請求一個沒有傳輸保證的受限資源
post
附上結果截圖:this
其中 Security/test.jsp爲一個受限的資源url
當未經受權的用戶訪問test.jsp時,會彈出一個身份認證的表單spa
輸入正確的身份以後,訪問成功,不然,訪問失敗code