?? 第一個acegi 程式 - 表單網頁.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0072)http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegConfig.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://www.acegisecurity.org/">Acegi官方網站</A>,下載Acegi程式庫,在撰寫這份教學文件之時,所使用的版本是1.0.3。<BR><BR>Acegi相關類別之間的依賴關系,可以藉由IoC容器來協助建立,在這邊您可以使用
<A
href="http://caterpillar.onlyfun.net/GossipCN/SpringGossip/SpringGossip.html">Spring</A>
的IoC容器功能,您可以在下載的Acegi檔案中,找到acegi-security-sample-tutorial.war,將之使用解壓縮軟體解開,可以在WEB-INF\lib下,找到所需的Spring程式庫,在Acegi
1.0.3中的acegi-security-sample-tutorial.war所搭配的是Spring
1.2.8。<BR><BR>要完成您第一個Acegi程式,您需要以下的程式庫:<BR>
<UL>
<LI>acegi-security-1.0.3.jar
<LI>spring-1.2.8.jar
</LI></UL><BR>為了在Web應用程式中,使用Acegi搭配Spring的Context資訊,來完成Acegi的依賴關系配置,您要在web.xml中設定
org.springframework.web.context.ContextLoaderListener,并在Context參數中,指定設定檔案位置與名稱,而Acegi主要透過Filter
Chain來達到請求的檢查、驗證、授權、登出等動作,您可以在web.xml中如下設定:<BR>
<UL>
<LI>web.xml </LI></UL><PRE><?xml version="1.0" encoding="UTF-8"?><BR><BR><web-app xmlns="http://java.sun.com/xml/ns/j2ee"<BR> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<BR> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"<BR> version="2.4"><BR><BR> <display-name>Acegi 范例</display-name><BR> <BR> <!-- 指定Acegi資訊的設定檔--><BR> <context-param><BR> <param-name>contextConfigLocation</param-name><BR> <param-value><SPAN style="FONT-WEIGHT: bold">/WEB-INF/acegi-config.xml</SPAN></param-value><BR> </context-param><BR> <BR> <!-- Acegi 的 Filter Chain 代理 --><BR> <filter><BR> <filter-name>Acegi Filter Chain Proxy</filter-name><BR> <filter-class><BR> org.acegisecurity.util.FilterToBeanProxy<BR> </filter-class><BR> <init-param><BR> <param-name>targetClass</param-name><BR> <param-value><BR> org.acegisecurity.util.FilterChainProxy<BR> </param-value><BR> </init-param><BR> </filter><BR><BR> <filter-mapping><BR> <filter-name>Acegi Filter Chain Proxy</filter-name><BR> <url-pattern>/*</url-pattern><BR> </filter-mapping><BR><BR> <!-- 取得Spring的Context --><BR> <listener><BR> <listener-class><BR> org.springframework.web.context.ContextLoaderListener<BR> </listener-class><BR> </listener><BR></web-app></PRE><BR>org.acegisecurity.util.FilterToBeanProxy建立
org.acegisecurity.util.FilterChainProxy實例,并將請求轉交給FilterChainProxy來處理,接下來就是配置acegi-config.xml的內容,在這邊先假設一個需求情境,您想要對Web應用程式中的/protected/下所有資源進行保護,如果使用者試圖存取/protected/下的資源,就先將其送至/acegilogin.jsp進行登入,登入成功后顯示使用者所請求的資源,使用者也可以直接連接/acegilogin.jsp,登入成功后,預設顯示/loginsuccess.jsp,您可以在acegi-config.xml中加入以下的設定:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><!--
驗證處理,使用表單 --></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<bean id="authenticationProcessingFilter" <BR>
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
</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
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<property name="authenticationManager" ref="authenticationManager"/>
</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<!-- 驗證失敗URL --></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<property name="authenticationFailureUrl" value="/acegilogin.jsp"/>
</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<!-- 驗證成功預設URL --></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<property name="defaultTargetUrl"
value="/protected/loginsuccess.jsp"/> </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
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<property name="filterProcessesUrl" value="<SPAN
style="COLOR: rgb(255,0,0)">/j_acegi_security_check</SPAN>"/>
</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</bean>
</SPAN><BR></DIV><BR>可以看到在這邊使用了AuthenticationProcessingFilter來處理驗證,實際對使用者的驗證是交給驗證管理員,也就是authenticationManager屬性中所設定的實例,可以在acegi-config.xml中加入:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<!-- 驗證管理員,管理驗證資訊提供者 --></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<bean id="authenticationManager"
class="org.acegisecurity.providers.ProviderManager"> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<property name="providers"><!-- 可有多個提供者,其中一個驗證通過即可以了 -->
</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<list> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<ref local="daoAuthenticationProvider"/> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</list> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</property> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</bean></SPAN></DIV><BR><BR>驗證管理員管理驗證提供者,也就是實際提供使用者名稱、密碼、角色資訊的物件,來源可以是資料庫或設定文件中的訊息,基于來源的不同,您可以使用
org.acegisecurity.providers.dao.DaoAuthenticationProvider,并指定其
userDetailsService屬性,設定驗證訊息來源:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<!-- 驗證提供者,指定使用記憶體來源中的驗證資訊 --></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<bean id="daoAuthenticationProvider"
class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<property name="userDetailsService" ref="inMemoryDaoImpl"/></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -