有这样一些场合,系统用户必须以其他角色身份去操作某些资源。例如,用户A要访问资源B,而用户A拥有的角色为AUTH_USER,资源B访问的角色必须为AUTH_RUN_AS_DATE,那么此时就必须使用户A拥有角色AUTH_RUN_AS_DATE才能访问资源B。尽管这种场合相对较少,但存在即合理,总会有需要的时候,要学会未雨绸缪。
      为了实现这一需求,Acegi为我们提供了Run-As认证服务。下面我们举例说明如何应用Run-As认证服务。

1、用于配置Run-As认证服务的接口与实现类

  1. public interface IRunAsDate {
  2. public void showDate();
  3. }
  4. public class RunAsDate implements IRunAsDate {
  5. private static final Log log = LogFactory.getLog(RunAsDate.class);
  6. /* (non-Javadoc)
  7. * @see sample.service.IRunAsDate#showDate()
  8. */
  9. public void showDate() {
  10. log.info("当前日期: " + new Date());
  11. }
  12. }
public interface IRunAsDate {
public void showDate();
} public class RunAsDate implements IRunAsDate {
private static final Log log = LogFactory.getLog(RunAsDate.class); /* (non-Javadoc)
* @see sample.service.IRunAsDate#showDate()
*/
public void showDate() {
log.info("当前日期: " + new Date());
}
}

2、对showDate方法进行授权
      设置访问showDate方法必须拥有AUTH_RUN_AS_DATE角色,同时暴露IRunAsDate接口。

  1. <bean id="runAsDateImpl"
  2. class="org.springframework.aop.framework.ProxyFactoryBean">
  3. <property name="proxyInterfaces">
  4. <value>sample.service.IRunAsDate</value>
  5. </property>
  6. <property name="interceptorNames">
  7. <list>
  8. <idref local="runAsDateSecurity" />
  9. <idref local="runAsDateTarget" />
  10. </list>
  11. </property>
  12. </bean>
  13. <bean id="runAsDateTarget" class="sample.service.impl.RunAsDate"></bean>
  14. <bean id="runAsDateSecurity"
  15. class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
  16. <property name="alwaysReauthenticate" value="true" />
  17. <property name="authenticationManager" ref="authenticationManager" />
  18. <property name="accessDecisionManager"
  19. ref="httpRequestAccessDecisionManager" />
  20. <property name="objectDefinitionSource">
  21. <value>
  22. sample.service.IRunAsDate.showDate=AUTH_RUN_AS_DATE
  23. </value>
  24. </property>
  25. </bean>
<bean id="runAsDateImpl"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>sample.service.IRunAsDate</value>
</property>
<property name="interceptorNames">
<list>
<idref local="runAsDateSecurity" />
<idref local="runAsDateTarget" />
</list>
</property>
</bean> <bean id="runAsDateTarget" class="sample.service.impl.RunAsDate"></bean> <bean id="runAsDateSecurity"
class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
<property name="alwaysReauthenticate" value="true" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager"
ref="httpRequestAccessDecisionManager" />
<property name="objectDefinitionSource">
<value>
sample.service.IRunAsDate.showDate=AUTH_RUN_AS_DATE
</value>
</property>
</bean>

其中,alwaysReauthenticate为true表示每次操作都需要进行身份的验证。在默认情况下,RunAsManagerImpl构建的RunAsUserToken认证对象都是已认证状态。因此,只有设置alwaysReauthenticate为true时,才会触发RunAsImplAuthenticationProvider的认证操作。

3、配置RunAsImplAuthenticationProvider
      RunAsManagerImpl实例会基于现有的的已认证对象创建新的RunAsUserToken认证类型,而RunAsImplAuthenticationProvider要负责这一认证类型的认证工作。与其他认证提供者一样,必须将其加入authenticationManager中。

  1. <bean id="runAsImplAuthenticationProvider"
  2. class="org.acegisecurity.runas.RunAsImplAuthenticationProvider">
  3. <property name="key" value="javaee" />
  4. </bean>
  5. <bean id="authenticationManager"
  6. class="org.acegisecurity.providers.ProviderManager">
  7. <property name="providers">
  8. <list>
  9. ……
  10. <!-- 配置与daoAuthenticationProvider类似 -->
  11. <ref bean="runAsImplAuthenticationProvider" />
  12. </list>
  13. </property>
  14. </bean>
<bean id="runAsImplAuthenticationProvider"
class="org.acegisecurity.runas.RunAsImplAuthenticationProvider">
<property name="key" value="javaee" />
</bean> <bean id="authenticationManager"
class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
……
<!-- 配置与daoAuthenticationProvider类似 -->
<ref bean="runAsImplAuthenticationProvider" />
</list>
</property>
</bean>

4、配置RunAsManagerImpl
      RunAsManagerImpl中的key必须与RunAsImplAuthenticationProvider中的key一致,从而保证RunAsManagerImpl 与RunAsImplAuthenticationProvider协同工作。在前面章节中,对于匿名认证与Remember-Me认证中也需要提供类似的key属性值。
      RunAsManagerImpl的rolePrefix属性默认值为ROLE_。由于我们配置的资源需要的角色为AUTH_RUN_AS_DATE,故在此我们将前缀设置为AUTH_。

  1. <bean id="runAsManagerImpl"
  2. class="org.acegisecurity.runas.RunAsManagerImpl">
  3. <property name="key" value="javaee" />
  4. <property name="rolePrefix" value="AUTH_" />
  5. </bean>
  6. <bean id="contactManagerSecurity"
  7. class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
  8. <property name="authenticationManager" ref="authenticationManager" />
  9. <property name="accessDecisionManager"
  10. ref="httpRequestAccessDecisionManager" />
  11. <property name="runAsManager" ref="runAsManagerImpl" />
  12. <property name="objectDefinitionSource">
  13. <value>
  14. ……
  15. sample.service.IContactManager.getAll=AUTH_FUNC_ContactManager.getAll,RUN_AS_DATE
  16. ……
  17. </value>
  18. </property>
  19. </bean>
<bean id="runAsManagerImpl"
class="org.acegisecurity.runas.RunAsManagerImpl">
<property name="key" value="javaee" />
<property name="rolePrefix" value="AUTH_" />
</bean> <bean id="contactManagerSecurity"
class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager"
ref="httpRequestAccessDecisionManager" />
<property name="runAsManager" ref="runAsManagerImpl" />
<property name="objectDefinitionSource">
<value>
……
sample.service.IContactManager.getAll=AUTH_FUNC_ContactManager.getAll,RUN_AS_DATE
……
</value>
</property>
</bean>

我们对getAll方法配置了RUN_AS_DATE角色,默认时RunAsManagerImpl会从授权信息中获得前缀为”RUN_AS”的角色,同时构建新的授权信息,将rolePrefix添加到角色中,即组成类似AUTH_RUN_AS_DATE的角色。
 注意,Run-As认证服务只是临时性替换了现有用户的身份,这一点要比较重视。

5、数据库脚本
 本例采用了MySQL数据库,脚本在WebRoot/db目录下。

6、其他说明
开发环境:
MyEclipse 5.0GA
Eclipse3.2.1
JDK1.5.0_10
tomcat5.5.23
acegi-security-1.0.7
Spring2.0

Jar包:
acegi-security-1.0.7.jar
commons-codec.jar
jstl.jar(1.1)
spring.jar(2.0.8)
standard.jar
commons-logging.jar(1.0)
c3p0-0.9.0.jar
log4j-1.2.13.jar
mysql-connector-java-3.1.10-bin.jar

菜鸟-手把手教你把Acegi应用到实际项目中(12)-Run-As认证服务的更多相关文章

  1. 菜鸟-手把手教你把Acegi应用到实际项目中(8)-扩展UserDetailsService接口

    一个能为DaoAuthenticationProvider提供存取认证库的的类,它必须要实现UserDetailsService接口: public UserDetails loadUserByUse ...

  2. 菜鸟-手把手教你把Acegi应用到实际项目中(10)-保护业务方法

    前面已经讲过关于保护Web资源的方式,其中包括直接在XML文件中配置和自定义实现FilterInvocationDefinitionSource接口两种方式.在实际企业应用中,保护Web资源显得非常重 ...

  3. 菜鸟-手把手教你把Acegi应用到实际项目中(1.1)

    相信不少朋友们对于学习Acegi的过程是比较痛苦的,而且可能最初一个例子都没能真正运行起来.即使能运行起来,对于里面那么多的配置,更搞不清楚为什么要那么配,多配一个和少配一个究竟有什么区别? 最终头都 ...

  4. 菜鸟-手把手教你把Acegi应用到实际项目中(11)-切换用户

    在某些应用场合中,我们可能需要用到切换用户的功能,从而以另一用户的身份进行相关操作.这一点类似于在Linux系统中,用su命令切换到另一用户进行相关操作.      既然实际应用中有这种场合,那么我们 ...

  5. 菜鸟-手把手教你把Acegi应用到实际项目中(7)-缓存用户信息

    首先讲讲EhCache.在默认情况下,即在用户未提供自身配置文件ehcache.xml或ehcache-failsafe.xml时,EhCache会依据其自身Jar存档包含的ehcache-fails ...

  6. 菜鸟-手把手教你把Acegi应用到实际项目中(5)

    在实际企业应用中,用户密码一般都会进行加密处理,这样才能使企业应用更加安全.既然密码的加密如此之重要,那么Acegi(Spring Security)作为成熟的安全框架,当然也我们提供了相应的处理方式 ...

  7. 菜鸟-手把手教你把Acegi应用到实际项目中(4)

    今天就讲个ConcurrentSessionFilter. 在Acegi 1.x版本中,控制并发HttpSession和Remember-Me认证服务不能够同时启用,它们之间存在冲突问题,这是该版本的 ...

  8. 菜鸟-手把手教你把Acegi应用到实际项目中(6)

    在企业应用中,用户的用户名.密码和角色等信息一般存放在RDBMS(关系数据库)中.前面几节我们采用的是InMemoryDaoImpl,即基于内存的存放方式.这节我们将采用RDBMS存储用户信息. Us ...

  9. 菜鸟-手把手教你把Acegi应用到实际项目中(1.2)

    7) daoAuthenticationProvider 进行简单的基于数据库的身份验证.DaoAuthenticationProvider获取数据库中的账号密码并进行匹配,若成功则在通过用户身份的同 ...

随机推荐

  1. LintCode "Heapify"

    My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...

  2. WSUS目录本地迁移

    生产环境中有一台win2003 server,安装了Microsoft Windows Server Update Services 3.0,作为所有windows server的内网补丁更新服务器, ...

  3. activiti自定义流程之整合(二):使用angular js整合ueditor创建表单

    注:整体环境搭建:activiti自定义流程之整合(一):整体环境配置 基础环境搭建完毕,接下来就该正式着手代码编写了,在说代码之前,我觉得有必要先说明一下activit自定义流程的操作. 抛开自定义 ...

  4. 回到顶部缓动效果代码 --- tween动画函数库

    function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...

  5. php CI框架nginx 配置

    # ci     server {        listen       80;        server_name  my.clb.com;        root /var/website/c ...

  6. (WPF) MVVM: ComboBox Binding, XML 序列化

    基本思路还是在View的Xmal里面绑定ViewModel的属性,虽然在View的后台代码中也可以实现binding,但是还是在Xmal里面相对的代码量要少一些. 此例子要实现的效果就是将一个List ...

  7. 想当然是编程最大的坑,记更新删除过期cookie无效有感

    一般来说只要设置了cookie的过期时间,就可以实现删除cookie的作用. 可是我尝试了设置过期时间,清除cookie内容都无效. 最后才发现,我根本没有执行到那段设置过期的代码. 刚开始是因为登出 ...

  8. HashMap的实现

    HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此 ...

  9. 创建immutable类

    不可变对象(immutable objects) 那么什么是immutable objects?什么又是mutable Objects呢? immutable Objects就是那些一旦被创建,它们的 ...

  10. IREP_SOA Integration SOAP概述(概念)

    20150827 Created By BaoXinjian