📄 第一个acegi 程式 - 改用资料库作为验证来源.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0073)http://caterpillar.onlyfun.net/GossipCN/AcegiGossip/FirstAcegiFromDB.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>在先前的设定中,inMemoryDaoImpl将使用者讯息设定在userMap之中:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<bean id="inMemoryDaoImpl"
class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"></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="userMap"> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<value> </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
caterpillar=123456,ROLE_SUPERVISOR</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
user1=user1pwd,ROLE_USER</SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
user2=user2pwd,disabled,ROLE_USER </SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
</value> </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><BR></DIV><BR>您可以撰写一个属性档案/WEB-INF/users.properties:<BR>
<UL>
<LI>users.properties </LI></UL><PRE>caterpillar=123456,ROLE_SUPERVISOR<BR>user1=user1pwd,ROLE_USER<BR>user2=user2pwd,disabled,ROLE_USER</PRE><BR>然后改设定inMemoryDaoImpl的userProperties:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<bean id="inMemoryDaoImpl"
class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"></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="userProperties"></SPAN><BR
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">
<bean
class="org.springframework.beans.factory.config.PropertiesFactoryBean"></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="location" value="/WEB-INF/users.properties" /></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
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><BR></DIV><BR>如此在需要使用者讯息时,就可以从users.properties中提取。<BR><BR>如果想要将使用者的相关讯息储存在资料库中,例如使用以下的SQL在MySQL中建立使用者讯息:<BR>
<UL>
<LI>users.sql </LI></UL><PRE>CREATE DATABASE acegi;<BR>USE acegi;<BR><BR>CREATE TABLE USERS( <BR> USERNAME VARCHAR(50) NOT NULL PRIMARY KEY, <BR> PASSWORD VARCHAR(50) NOT NULL, <BR> ENABLED BIT NOT NULL<BR>);<BR><BR>INSERT INTO USERS(username,password,enabled) values('caterpillar' ,'123456', 1);<BR>INSERT INTO USERS(username,password,enabled) values('user1' ,'user1pwd', 1);<BR>INSERT INTO USERS(username,password,enabled) values('user2' ,'user2pwd', 0);<BR><BR>CREATE TABLE AUTHORITIES( <BR> USERNAME VARCHAR( 50 ) NOT NULL, <BR> AUTHORITY VARCHAR( 50 ) NOT NULL, <BR> CONSTRAINT FK_AUTHORITIES_USERS FOREIGN KEY(USERNAME) REFERENCES USERS(USERNAME) <BR>); <BR> <BR>INSERT INTO AUTHORITIES(USERNAME,AUTHORITY) values( 'caterpillar' , 'ROLE_SUPERVISOR');<BR>INSERT INTO AUTHORITIES(USERNAME,AUTHORITY) values( 'user1', 'ROLE_USER'); <BR>INSERT INTO AUTHORITIES(USERNAME,AUTHORITY) values( 'user2', 'ROLE_USER');</PRE><BR>您可以使用org.acegisecurity.userdetails.jdbc.JdbcDaoImpl作为userDetailsService,它需要一个DataSource,这可以使用Spring的DriverManagerDataSource,例如:<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><SPAN style="FONT-WEIGHT: bold"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="driverClassName"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <value>com.mysql.jdbc.Driver</value> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </property> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="url"> </SPAN><SPAN style="FONT-WEIGHT: bold"></SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <value>jdbc:mysql://localhost:3306/acegi</value> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </property> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="username"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <value>root</value> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </property> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="password"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <value>123456</value> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </property> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </bean> </SPAN><BR> <BR> <!-- 验证处理,使用表单 --><BR> <bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"> <BR> <!-- 验证管理员,处理验证资讯提供者 --><BR> <property name="authenticationManager" ref="authenticationManager"/> <BR> <!-- 验证失败URL --><BR> <property name="authenticationFailureUrl" value="/acegilogin.jsp"/> <BR> <!-- 验证成功预设URL --><BR> <property name="defaultTargetUrl" value="/protected/loginsuccess.jsp"/> <BR> <!-- 验证处理的提交位址 --><BR> <property name="filterProcessesUrl" value="/j_acegi_security_check"/> <BR> </bean><BR><BR> <!-- 验证管理员,管理验证资讯提供者 --><BR> <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"> <BR> <property name="providers"><!-- 可有多个提供者,其中一个验证通过即可以了 --> <BR> <list> <BR> <ref local="daoAuthenticationProvider"/> <BR> <ref local="rememberMeAuthenticationProvider"/><BR> </list> <BR> </property> <BR> </bean><BR><BR> <!-- 验证提供者,指定使用资料库来源中的验证资讯 --><BR> <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <BR> <property name="userDetailsService" ref="<SPAN style="FONT-WEIGHT: bold">jdbcDaoImpl</SPAN>"/><BR> </bean> <BR><BR><SPAN style="FONT-WEIGHT: bold"> <bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"> </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> <property name="dataSource" ref="dataSource"/></SPAN><SPAN style="FONT-WEIGHT: bold"></SPAN><SPAN style="FONT-WEIGHT: bold"></SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold"> </bean> </SPAN><BR> <BR> <!-- 发生验证错误或权限错误时的处理 --><BR> <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter"> <BR> <property name="authenticationEntryPoint"> <BR> <bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"> <BR> <property name="loginFormUrl" value="/acegilogin.jsp"/> <BR> <property name="forceHttps" value="false"/> <BR> </bean> <BR> </property> <BR> <property name="accessDeniedHandler"> <BR> <bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl"> <BR> <property name="errorPage" value="/accessDenied.jsp"/> <BR> </bean> <BR> </property> <BR> </bean> <BR><BR> <!-- FilterSecurityInterceptor 对 URI 进行保护 --><BR> <bean id="filterSecurityInterceptor"<BR> class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"><BR> <!-- 验证管理员 --><BR> <property name="authenticationManager" ref="authenticationManager" /><BR> <!-- 授权管理员 --><BR> <property name="accessDecisionManager" ref="accessDecisionManager" /><BR> <property name="objectDefinitionSource"><BR> <value><BR> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON<BR> PATTERN_TYPE_APACHE_ANT<BR> /protected/**=ROLE_SUPERVISOR,ROLE_USER<BR> </value><BR> </property><BR> </bean><BR> <BR> <!-- 授权管理员 --><BR> <bean id="accessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased"><BR> <!-- 是否全部弃权时视为通过 --><BR> <property name="allowIfAllAbstainDecisions" value="false" /><BR> <property name="decisionVoters"><BR> <list><BR> <bean class="org.acegisecurity.vote.RoleVoter" /><BR> </list><BR> </property><BR> </bean> <BR><BR> <!-- 利用cookie自动登入 --> <BR> <bean id="rememberMeProcessingFilter" <BR> class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter"> <BR> <property name="authenticationManager" ref="authenticationManager"/> <BR> <property name="rememberMeServices" ref="rememberMeServices"/> <BR> </bean> <BR> <bean id="rememberMeServices" <BR> class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices"> <BR> <property name="userDetailsService" ref="<SPAN style="FONT-WEIGHT: bold">jdbcDaoImpl</SPAN>"/> <BR> <property name="key" value="javauser"/> <BR> </bean><BR> <bean id="rememberMeAuthenticationProvider" <BR> class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"> <BR> <property name="key" value="javauser"/> <BR> </bean> <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> <bean id="httpSessionContextIntegrationFilter"<BR> class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" /><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,rememberMeProcessingFilter<BR> </value> <BR> </property> <BR> </bean> <BR></beans> </PRE><BR>当然,别忘了在您的Web应用程式的lib中,加入JDBC驱动程式程式库。
<BR><BR></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -