將 FORM 身份驗證用於 Web 應用程序時,將提供一個自定義登錄屏幕(Web 瀏覽器在響應 Web 應用程序資源請求時顯示)和一個錯誤屏幕(在登錄失敗時顯示)。可以使用 HTML 頁、JSP 或 Servlet 生成登錄屏幕。基於表單登錄的優點是:可以完全控制這些屏幕,因此可以將它們設計得滿足應用程序或企業策略/準則的要求。
登錄屏幕提示用戶輸入用戶名和密碼。圖 3-4 所示爲使用
JSP 生成的典型登錄屏幕,而清單 3-5 所示則爲源代碼。
<html> <head>) <title>Security WebApp login page</title> </head> <body bgcolor="#cccccc"> <blockquote> <img src=BEA_Button_Final_web.gif align=right> <h2>Please enter your user name and password:</h2> <p> <form method="POST" action="j_security_check"> <table border=1> <tr> <td>Username:</td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="j_password"></td> </tr> <tr> <td colspan=2 align=right><input type=submit value="Submit"></td> </tr> </table> </form> </blockquote> </body> </html>
注意:
action="j_security_check"
name="j_username"
name="j_password"
爲約定寫法,必須這樣寫
<html> <head> <title>Login failed</title> </head> <body bgcolor=#ffffff> <blockquote> <img src=/security/BEA_Button_Final_web.gif align=right> <h2>Sorry, your user name and password were not recognized.</h2> <p><b> <a href="/security/welcome.jsp">Return to welcome page</a> or <a href="/security/logout.jsp">logout</a> </b> </blockquote> </body> </html>
要開發提供 FORM 身份驗證的 Web 應用程序,請執行下列步驟:
創建web.xml
部署描述符。請在該文件中包括以下信息(請參閱清單 3-7):定義歡迎文件。歡迎文件的名稱爲welcome.jsp
。爲計劃要保護的每組 URL 資源定義安全約束。每組 URL 資源共享一個通用的 URL。通常,HTML 頁、JSP 和 Servlet 等 URL 資源受最大的保護,但其他類型的 URL 資源也受支持。在清單 3-7 中,URL 模式指向 /admin/edit.jsp,從而保護位於 Web 應用程序的admin
子目錄中的edit.jsp
文件;定義可以訪問 URL 資源的 HTTP 方法 (GET
);並定義安全角色名admin
。注意: 安全角色名稱中不能包含連字符。在管理控制檯中,無法修改含連字符的安全角色名稱。另外,BEA 建議對安全角色名使用以下約定:它們應該獨一無二。 定義要使用的身份驗證類型以及將應用安全約束的安全領域。在本例中,指定了FORM
類型,但未指定領域,因此,領域是默認領域,這意味着安全約束將應用到 WebLogic Server 實例啓動時激活的安全領域。定義一個或多個安全角色並將它們映射到安全約束。在我們的示例中,只在安全約束中定義了一個安全角色admin
,因此在此處只定義一個安全角色名稱。但是,實際上可以定義任意數量的安全角色。<?xml version='1.0' encoding='UTF-8'?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<web-app>
<welcome-file-list> <welcome-file>welcome.jsp</welcome-file> </welcome-file-list>
<security-constraint> <web-resource-collection> <web-resource-name>AdminPages</web-resource-name> <description> These pages are only accessible by authorized administrators. </description> <url-pattern>/admin/edit.jsp</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <description> These are the roles who have access. </description> <role-name> admin </role-name> </auth-constraint> <user-data-constraint> <description> This is how the user data must be transmitted. </description> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint>
<login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/fail_login.html</form-error-page> </form-login-config> </login-config>
<security-role> <description> An administrator </description> <role-name> admin </role-name> </security-role> </web-app>
創建weblogic.xml
部署描述符。在該文件中,將安全角色名稱映射到用戶和組。清單 3-8 給出了一個示例weblogic.xml
文件,該文件將在web.xml
文件的 <security-role> 標記中定義的admin
安全角色映射到一個名爲supportGroup
的組。使用此配置時,WebLogic Server 將只允許supportGroup
組中的用戶訪問受保護的 WebLogic 資源。但是,可以使用管理控制檯來修改 Web 應用程序的安全角色,以便其他組可以訪問受保護的 WebLogic 資源。<?xml version='1.0' encoding='UTF-8'?> <weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<weblogic-web-app>
<security-role-assignment> <role-name>admin</role-name> <principal-name>supportGroup</principal-name> </security-role-assignment>
</weblogic-web-app>
創建一個 Web 應用程序文件,該文件在用戶通過輸入 URL 請求受保護的 Web 應用程序資源時將生成歡迎屏幕。清單 3-9 顯示了一個示例welcome.jsp
文件。圖 3-3 顯示的是歡迎屏幕。<html> <head> <title>Security login example</title> </head>
<% String bgcolor; if ((bgcolor=(String)application.getAttribute("Background")) == null) { bgcolor="#cccccc"; } %>
<body bgcolor=<%="/""+bgcolor+"/""%>>
<blockquote> <img src=BEA_Button_Final_web.gif align=right> <h1> Security Login Example </h1>
<p> Welcome <%= request.getRemoteUser() %>!
<p> If you are an administrator, you can configure the background color of the Web Application. <br> <b><a href="admin/edit.jsp">Configure background</a></b>.
<% if (request.getRemoteUser() != null) { %> <p> Click here to <a href="logout.jsp">logout</a>. <% } %>
</blockquote> </body> </html>
注意: | 在清單 3-3 中,請注意,JSP 將調用一個 API (request.getRemoteUser() ) 來獲取登錄用戶的用戶名。可改用不同的
API weblogic.security.Security.getCurrentSubject() 。要使用該 API 獲取用戶的名稱,請將它與SubjectUtils API 結合使用,如下所示: |
String username = weblogic.security.SubjectUtils.getUsername( weblogic.security.Security.getCurrentSubject());
啓動 WebLogic Server,然後定義有權訪問 URL 資源的用戶和組。在weblogic.xml
文件(請參閱清單 3-8)中,<role-name> 標記將admin
定義爲有權訪問edit.jsp
文件的組,並將用戶joe
定義爲該組的成員。因此,請使用管理控制檯定義admin
組,定義用戶joe
,並將joe
添加到admin
組中。也可以定義其他用戶並將其添加到組中,這些用戶也將有權訪問受保護的 WebLogic 資源。有關添加用戶和組的信息,請參閱“使用角色和策略確保 WebLogic 資源安全”中的用戶、組和安全角色。部署 Web 應用程序並使用在上一步中定義的用戶訪問受保護的 Web 應用程序資源。有關部署說明,請參閱部署 Web 應用程序。打開 Web 瀏覽器並輸入以下 URL:輸入用戶名和密碼。即顯示歡迎屏幕。