?? 第一個acegi 程式 - 表單網頁.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0070)http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegPage.html -->
<HTML><HEAD><TITLE>第一個Acegi 程式 - 表單網頁</TITLE><LINK
href="第一個Acegi 程式 - 表單網頁.files/stdlayout.css" type=text/css rel=stylesheet><LINK
href="第一個Acegi 程式 - 表單網頁.files/print.css" type=text/css rel=stylesheet>
<META http-equiv=content-type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY>
<H3><A
href="http://caterpillar.onlyfun.net/GossipCN/index.html">http://caterpillar.onlyfun.net/GossipCN/index.html</A></H3>
<H1><A
href="http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/AcegiGossip.html">Acegi
Gossip: 第一個Acegi 程式 - 表單網頁</A></H1>完成了 <A
href="http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegConfig.html">第一個Acegi
程式 - 設定文件</A> 中的文件設定,接下來完成必要的表單網頁,首先是/acegilogin.jsp:<BR>
<UL>
<LI>acegilogin.jsp </LI></UL><PRE><%@page contentType="text/html"%><BR><%@page pageEncoding="UTF-8"%><BR><html><BR><head><BR><title>Acegi 范例網頁 - 登入</title><BR></head><BR><body><BR><h2>登入范例應用程式!</h2><BR><br /><BR><form action="<SPAN style="FONT-WEIGHT: bold">j_acegi_security_check</SPAN>" method="POST"><BR><table><BR> <tr><BR> <td>名稱:</td><BR> <td><input type='text' name='<SPAN style="FONT-WEIGHT: bold">j_username</SPAN>' value=''></td><BR> </tr><BR> <tr><BR> <td>密碼:</td><BR> <td><input type='password' name='<SPAN style="FONT-WEIGHT: bold">j_password</SPAN>'></td><BR> </tr><BR> <tr><BR> <td><input name="reset" type="reset"></td><BR> <td><input name="submit" type="submit"></td><BR> </tr><BR></table><BR></form><BR></body><BR></html></PRE><BR>主要注意到粗體字的部份,j_acegi_security_check為
<A
href="http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegConfig.html">第一個Acegi
程式 - 設定文件</A>
中authenticationProcessingFilter的filterProcessesUrl屬性所設定的名稱,而驗證時發送的使用者名稱與密碼,要以j_username與j_password請求參數送出。<BR><BR>如果想要在登入失敗時顯示錯誤訊息,可以檢查param.login_error是否為空,例如結合JSTL的話:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><c:if
test="${not empty param.login_error}"></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<font color="red">使用者名稱或密碼錯誤,請重新登入!<BR></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</font></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"></c:if></SPAN><BR></DIV><BR>您也許想顯示最后一次登入失敗的使用者名稱,這個訊息是儲存在session之中,可以使用AuthenticationProcessingFilter.ACEGI_SECURITY_LAST_USERNAME_KEY作為KEY來取得這個值,例如:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><%@
page</SPAN><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
import="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"%><BR><BR>...<BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"></SPAN></DIV><BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><input
type='text' name='j_username'</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<c:if test="${not empty param.login_error}"></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
value='<%=
session.getAttribute(AuthenticationProcessingFilter.ACEGI_SECURITY_LAST_USERNAME_KEY)
%>'</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</c:if></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">></SPAN><BR></DIV><BR>登入成功的話,可以顯示/protected/loginsuccess.jsp<BR>
<UL>
<LI>loginsuccess.jsp </LI></UL><PRE><%@page contentType="text/html"%><BR><%@page pageEncoding="UTF-8"%><BR><%@ page import="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"%><BR><BR><BR><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<BR> "http://www.w3.org/TR/html4/loose.dtd"><BR><BR><html><BR> <head><BR> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><BR> <title>登入成功</title><BR> </head><BR> <body><BR> <BR> <h1>歡迎 <%= session.getAttribute(AuthenticationProcessingFilter.ACEGI_SECURITY_LAST_USERNAME_KEY) %><BR> </h1><BR> <BR> </body><BR></html></PRE><BR>登入成功,但不是正確的角色時,會發生授權失敗的例外,這時會送往/protected/accessDenied.jsp:<BR>
<UL>
<LI>accessDenied.jsp </LI></UL><PRE><%@page contentType="text/html"%><BR><%@page pageEncoding="UTF-8"%><BR><BR><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<BR> "http://www.w3.org/TR/html4/loose.dtd"><BR><BR><html><BR> <head><BR> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><BR> <title>授權失敗</title><BR> </head><BR> <body><BR><BR> <h1>授權失敗</h1><BR> <BR> </body><BR></html></PRE><BR>接著,設置一個簡單的首頁,當使用者存取首頁時,直接轉頁至acegilogin.jsp:<BR>
<UL>
<LI>index.jsp </LI></UL><PRE> <%@ page language="java" pageEncoding="UTF-8"%> <BR> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <BR> <html> <BR> <head> <BR> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=acegilogin.jsp"> <BR> </head> <BR> <body> <BR> <p>載入中...</p> <BR> </body> <BR> </html> </PRE><BR>現在您可以啟動您的應用程式了,嘗試直接存取acegilogin.jsp并登入,關閉瀏覽器并清除快取,再嘗試直接存取/protected/下的資源,看看結果如何。<BR><BR></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -