📄 第一个acegi 程式 - 登出、自动cookies登入.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0079)http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegLogoutCookies.html -->
<HTML><HEAD><TITLE>第一个Acegi 程式 - 登出、自动Cookies登入</TITLE><LINK
href="第一个Acegi 程式 - 登出、自动Cookies登入.files/stdlayout.css" type=text/css
rel=stylesheet><LINK href="第一个Acegi 程式 - 登出、自动Cookies登入.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 程式 - 登出、自动Cookies登入</A></H1>在完成 <A
href="http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegPage.html">第一个Acegi
程式 - 表单网页</A>
之后,您已经可以使用Acegi来保护您所设定的资源,为了方便起见,您应该提供一个登出的链结,让使用者可以登出,让目前的对话失效。<BR><BR>这可以在/WEB-INF/loginsuccess.jsp上加上一个登出的链结,动作的目的地是j_acegi_logout:<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><a href="<SPAN style="FONT-WEIGHT: bold">../j_acegi_logout</SPAN>">登出</a><BR> </body><BR></html></PRE><BR>要进行登出,可以设定org.acegisecurity.ui.logout.LogoutFilter,指定登出后的显示页面,以及执行登出动作的处理者:<BR>
<UL>
<LI>acegi-config.xml </LI></UL><PRE><?xml version="1.0" encoding="UTF-8"?><BR><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><BR><beans><BR> ... 先前设定相同 <BR><BR><SPAN style="FONT-WEIGHT: bold"> <!-- 登出处理 --> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <constructor-arg value="/acegilogin.jsp"/> <!-- 登出后的显示页面 --> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <constructor-arg> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <list> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler"/> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </list> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </constructor-arg> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </bean> </SPAN><BR> <BR> <!-- Filter Chain --><BR> <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"> <BR> <property name="filterInvocationDefinitionSource"> <BR> <value> <BR> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON <BR> PATTERN_TYPE_APACHE_ANT <BR> /**=authenticationProcessingFilter,exceptionTranslationFilter,filterSecurityInterceptor,<SPAN style="FONT-WEIGHT: bold">logoutFilter</SPAN><BR> </value> <BR> </property> <BR> </bean> <BR></beans> </PRE><BR>这样就OK了,登入之后,您就可以看到登出的链结,按下该链结后就可自动进行登出。<BR><BR>如果要执行Cookies自动登入,则可以使用org.acegisecurity.ui.rememberme.RememberMeProcessingFilter,您要提供验证管理员与RememberMeServices,并指定Cookie名称,例如:<BR>
<UL>
<LI>acegi-config.xml </LI></UL><PRE><?xml version="1.0" encoding="UTF-8"?><BR><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><BR><beans><BR> ....<BR><BR> <!-- 验证管理员,管理验证资讯提供者 --><BR> <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"> <BR> <property name="providers"><!-- 可有多个提供者,其中一个验证通过即可以了 --> <BR> <list> <BR> <ref local="daoAuthenticationProvider"/> <BR><SPAN style="FONT-WEIGHT: bold"> <ref local="rememberMeAuthenticationProvider"/></SPAN><BR> </list> <BR> </property> <BR> </bean><BR><BR> .....<BR><BR><SPAN style="FONT-WEIGHT: bold"> <!-- 利用cookie自动登入 --> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <bean id="rememberMeProcessingFilter" </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="authenticationManager" ref="authenticationManager"/> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="rememberMeServices" ref="rememberMeServices"/> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </bean> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <bean id="rememberMeServices" </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="userDetailsService" ref="inMemoryDaoImpl"/> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="key" value="javauser"/> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </bean></SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <bean id="rememberMeAuthenticationProvider" </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="key" value="javauser"/> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </bean> </SPAN><BR> <BR> <!-- 登出处理 --> <BR> <bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter"> <BR> <constructor-arg value="/acegilogin.jsp"/> <!-- 登出后的显示页面 --> <BR> <constructor-arg> <BR> <list> <BR> <bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler"/> <BR> </list> <BR> </constructor-arg> <BR> </bean> <BR> <BR> ...<BR><BR> <!-- Filter Chain --><BR> <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"> <BR> <property name="filterInvocationDefinitionSource"> <BR> <value> <BR> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON <BR> PATTERN_TYPE_APACHE_ANT <BR> /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,exceptionTranslationFilter,<BR> filterSecurityInterceptor,logoutFilter,<SPAN style="FONT-WEIGHT: bold">rememberMeProcessingFilter</SPAN><BR> </value> <BR> </property> <BR> </bean> <BR></beans> </PRE><BR>要进行自动登入,您必须在登入时发送_acegi_security_remember_me请求参数,例如修改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="j_acegi_security_check" method="POST"><BR><table><BR> <tr><BR> <td>名称:</td><BR> <td><input type='text' name='j_username' value=''></td><BR> </tr><BR> <tr><BR> <td>密码:</td><BR> <td><input type='password' name='j_password'></td><BR> </tr><BR> <tr><BR> <td><BR><SPAN style="FONT-WEIGHT: bold"> <input type="checkbox" name="_acegi_security_remember_me"></SPAN><BR> </td><BR> <td>2周内记得我</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><BR><BR></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -