📄 第一个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 + -