⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 第一个acegi 程式 - 改用资料库作为验证来源.htm

📁 acegi+spring最新的分析
💻 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:&nbsp;第一个Acegi 程式 - 
改用资料库作为验证来源</A></H1>在先前的设定中,inMemoryDaoImpl将使用者讯息设定在userMap之中:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp; 
&lt;bean id="inMemoryDaoImpl" 
class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;property name="userMap"&gt;&nbsp;&nbsp;&nbsp; </SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;value&gt;&nbsp;&nbsp;&nbsp; </SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
user2=user2pwd,disabled,ROLE_USER&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/value&gt;&nbsp;&nbsp;&nbsp; </SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/property&gt;&nbsp;&nbsp;&nbsp; </SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; 
&lt;/bean&gt; </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">&nbsp;&nbsp; 
&lt;bean id="inMemoryDaoImpl" 
class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;property name="userProperties"&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;bean 
class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;property name="location" value="/WEB-INF/users.properties" /&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/bean&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/property&gt;</SPAN><BR 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN 
style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp; 
&lt;/bean&gt;</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>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<BR>&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;<BR>&lt;beans&gt;<BR><SPAN style="FONT-WEIGHT: bold">    &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;property name="driverClassName"&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">             &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;              </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;/property&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;property name="url"&gt;  </SPAN><SPAN style="FONT-WEIGHT: bold"></SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">             &lt;value&gt;jdbc:mysql://localhost:3306/acegi&lt;/value&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;/property&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;property name="username"&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">             &lt;value&gt;root&lt;/value&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;/property&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;property name="password"&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">             &lt;value&gt;123456&lt;/value&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">         &lt;/property&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">     &lt;/bean&gt;  </SPAN><BR>     <BR>    &lt;!-- 验证处理,使用表单 --&gt;<BR>    &lt;bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"&gt;  <BR>        &lt;!-- 验证管理员,处理验证资讯提供者  --&gt;<BR>        &lt;property name="authenticationManager" ref="authenticationManager"/&gt;  <BR>        &lt;!-- 验证失败URL --&gt;<BR>        &lt;property name="authenticationFailureUrl" value="/acegilogin.jsp"/&gt;  <BR>        &lt;!-- 验证成功预设URL --&gt;<BR>        &lt;property name="defaultTargetUrl" value="/protected/loginsuccess.jsp"/&gt;  <BR>        &lt;!-- 验证处理的提交位址 --&gt;<BR>        &lt;property name="filterProcessesUrl" value="/j_acegi_security_check"/&gt;  <BR>    &lt;/bean&gt;<BR><BR>    &lt;!-- 验证管理员,管理验证资讯提供者 --&gt;<BR>   &lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;  <BR>      &lt;property name="providers"&gt;&lt;!-- 可有多个提供者,其中一个验证通过即可以了 --&gt;  <BR>         &lt;list&gt;  <BR>            &lt;ref local="daoAuthenticationProvider"/&gt; <BR>            &lt;ref local="rememberMeAuthenticationProvider"/&gt;<BR>         &lt;/list&gt;  <BR>      &lt;/property&gt;  <BR>   &lt;/bean&gt;<BR><BR>   &lt;!-- 验证提供者,指定使用资料库来源中的验证资讯 --&gt;<BR>   &lt;bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;  <BR>        &lt;property name="userDetailsService" ref="<SPAN style="FONT-WEIGHT: bold">jdbcDaoImpl</SPAN>"/&gt;<BR>   &lt;/bean&gt; <BR><BR><SPAN style="FONT-WEIGHT: bold">   &lt;bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"&gt;  </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">       &lt;property name="dataSource" ref="dataSource"/&gt;</SPAN><SPAN style="FONT-WEIGHT: bold"></SPAN><SPAN style="FONT-WEIGHT: bold"></SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">   &lt;/bean&gt;  </SPAN><BR>    <BR>    &lt;!-- 发生验证错误或权限错误时的处理 --&gt;<BR>    &lt;bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter"&gt;  <BR>        &lt;property name="authenticationEntryPoint"&gt;  <BR>            &lt;bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"&gt;  <BR>                &lt;property name="loginFormUrl" value="/acegilogin.jsp"/&gt;  <BR>                &lt;property name="forceHttps" value="false"/&gt;  <BR>            &lt;/bean&gt;  <BR>        &lt;/property&gt;  <BR>        &lt;property name="accessDeniedHandler"&gt;  <BR>            &lt;bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl"&gt;  <BR>                &lt;property name="errorPage" value="/accessDenied.jsp"/&gt;  <BR>            &lt;/bean&gt;  <BR>        &lt;/property&gt;  <BR>    &lt;/bean&gt;    <BR><BR>    &lt;!-- FilterSecurityInterceptor 对 URI 进行保护 --&gt;<BR>    &lt;bean id="filterSecurityInterceptor"<BR>          class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;<BR>        &lt;!-- 验证管理员 --&gt;<BR>        &lt;property name="authenticationManager" ref="authenticationManager" /&gt;<BR>        &lt;!-- 授权管理员 --&gt;<BR>        &lt;property name="accessDecisionManager" ref="accessDecisionManager" /&gt;<BR>        &lt;property name="objectDefinitionSource"&gt;<BR>            &lt;value&gt;<BR>                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON<BR>                PATTERN_TYPE_APACHE_ANT<BR>                /protected/**=ROLE_SUPERVISOR,ROLE_USER<BR>            &lt;/value&gt;<BR>        &lt;/property&gt;<BR>    &lt;/bean&gt;<BR>    <BR>    &lt;!-- 授权管理员 --&gt;<BR>    &lt;bean id="accessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased"&gt;<BR>         &lt;!-- 是否全部弃权时视为通过 --&gt;<BR>         &lt;property name="allowIfAllAbstainDecisions" value="false" /&gt;<BR>         &lt;property name="decisionVoters"&gt;<BR>             &lt;list&gt;<BR>                 &lt;bean class="org.acegisecurity.vote.RoleVoter" /&gt;<BR>             &lt;/list&gt;<BR>         &lt;/property&gt;<BR>    &lt;/bean&gt;        <BR><BR>    &lt;!-- 利用cookie自动登入 --&gt;  <BR>    &lt;bean id="rememberMeProcessingFilter"  <BR>           class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter"&gt;  <BR>        &lt;property name="authenticationManager"  ref="authenticationManager"/&gt;  <BR>        &lt;property name="rememberMeServices" ref="rememberMeServices"/&gt;  <BR>    &lt;/bean&gt;      <BR>    &lt;bean id="rememberMeServices"  <BR>           class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices"&gt;  <BR>        &lt;property name="userDetailsService" ref="<SPAN style="FONT-WEIGHT: bold">jdbcDaoImpl</SPAN>"/&gt;  <BR>        &lt;property name="key" value="javauser"/&gt;  <BR>    &lt;/bean&gt;<BR>    &lt;bean id="rememberMeAuthenticationProvider"  <BR>           class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"&gt;  <BR>        &lt;property name="key" value="javauser"/&gt;  <BR>    &lt;/bean&gt;   <BR>    <BR>    &lt;!-- 登出处理 --&gt;  <BR>    &lt;bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter"&gt;  <BR>        &lt;constructor-arg value="/acegilogin.jsp"/&gt; &lt;!-- 登出后的显示页面 --&gt;  <BR>            &lt;constructor-arg&gt;  <BR>               &lt;list&gt;  <BR>                   &lt;bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler"/&gt;  <BR>               &lt;/list&gt;  <BR>        &lt;/constructor-arg&gt;  <BR>    &lt;/bean&gt; <BR><BR>    &lt;bean id="httpSessionContextIntegrationFilter"<BR>	  class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" /&gt;<BR>              <BR>    &lt;!-- Filter Chain --&gt;<BR>     &lt;bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"&gt;  <BR>       &lt;property name="filterInvocationDefinitionSource"&gt;  <BR>          &lt;value&gt;  <BR>            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON <BR>            PATTERN_TYPE_APACHE_ANT <BR>            /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,exceptionTranslationFilter,<BR>                 filterSecurityInterceptor,logoutFilter,rememberMeProcessingFilter<BR>         &lt;/value&gt; <BR>      &lt;/property&gt; <BR>    &lt;/bean&gt;    <BR>&lt;/beans&gt;   </PRE><BR>当然,别忘了在您的Web应用程式的lib中,加入JDBC驱动程式程式库。 
<BR><BR></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -