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