基于Java 5 注释的Acegi方法保护

使用Java 5 Annotations 方式来实现方法级别的保护,配置文件如下:

<bean id="methodSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
   <property name="validateConfigAttributes" value="false" />
   <property name="authenticationManager" ref="authenticationManager" />
   <property name="accessDecisionManager" ref="accessDecisionManager" />
   <property name="objectDefinitionSource">
         <bean class="org.acegisecurity.intercept.method.MethodDefinitionAttributes">
	         <property name="attributes">
	           <!--  利用1.5Annotation的設定方式 -->
	           <bean class="org.acegisecurity.annotation.SecurityAnnotationAttributes" />
	         </property>
           </bean>
  </property>
</bean>

此外,在代码里面必使用Acegi Java 5 Security Annotations 来配置方法的保护属性。下面的例子使用 @Secured 注解来描述配置属性,如下示例:

 

import org.acegisecurity.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;

/**
 * 使用Java 5 注解来实现方法保护
 * 
 * @author Janwer
 *
 */
@Transactional
public interface WebService {
	@Transactional(readOnly=true)
	@Secured({"ROLE_CUSTOMER"})
	List<Subscribe> queryOrderStates(long regId, int subcribeStatus);


	@Transactional(readOnly=true)
	@Secured({"ROLE_MANAGER"})
	List<Subscribe> queryRegSubscribeState(Page page, boolean asc, int subcribeStatus);


	@Secured({"ROLE_CUSTOMER"})
	TransactionResult createSubscribeByAlipay(Map<String,String> m, Register r, int status, String alipayRecord);

}

你可能注意到了 validateConfigAttributes 属性在上面的 MethodSecurityInterceptor 示例中,当设置为 true (the default) ,在启动时 MethodSecurityInterceptor 将验证提供的所有配置属性是否有效。它每个配置属性的检查能通过 AccessDecisionManagerRunAsManager 来处理。假如它们都不能处理这些配置属性,那么一个异常将会被抛出。如果使用 Jakarta Commons 属性配置方法, 你应该将 validateConfigAttributes 设置为 false

 


注意:其实使用Java 5 的注释也应该设置为false ,否则在程序启动时,将有一个警告产生。并不影响它的正常使用。

WARN [org.acegisecurity.intercept.AbstractSecurityInterceptor.afterPropertiesSet:225]- Could not validate configuration attributes as the MethodDefinitionSource did not return a ConfigAttributeDefinition Iterator


请注意,当使用 BeanNameAutoProxyCreator 为安全过滤创建请求代理时,如果使用基于 CGLIB 的代理,那配置文件应当包含 proxyTargetClass 属性并设置为 true 。否则方法保护将不起作用。引用的是代理的呼叫,而不是实际代理的对象。注意采用这种方式需要 CGLIB 。如果直接使用 JDK1.5 动态代理时,可以注释掉这一属性,见如下示例:

<!-- 利用Spring的自动代理功能实现AOP代理 -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="interceptorNames">
       <list>
	  <value>methodSecurityInterceptor</value>
       </list>
  </property>
  <property name="beanNames">
       <list>
	 <!--  需要保护的方法  -->
	 <value>registerService</value>
	 <value>mgrService</value>
	 <value>subService</value>
	 </list>
   </property>
   <!--property name="proxyTargetClass" value="true"/-->
</bean>

注意:上面是引用Acegi 官方文档描述,但在我的实际使用中,因为我用的是Spring 2.0.6 且使用基于 JDK1.5 的动态代理,所以不再需要proxyTargetClass 属性设置。当然如是使用 CGLIB 代理的就按官方说明来设置。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值