Spring mvc 中有关 Shiro 1.2.3 配置问题

Spring 版本:3.2.x,  4.0.x

【问题说明】

首先介绍下配置出错情况:

(1)项目中,Spring3 and Spring4 的 applicationContext.xml aop 配置如下:

... ...
   <aop:aspectj-autoproxy expose-proxy="true"/>    
   <tx:annotation-driven transaction-manager="transactionManager"/>
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="query*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*"  read-only="true"/>
        </tx:attributes>
    </tx:advice>
   
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(* com.app..service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
... ...

 未加入 Shiro 之前,所有 applicationContext 的 getBean(...) 方法能正常获取到相应对象实例。 调用 getBeanDefinitionNames() 输出的 Beans 如下:

Bean NameBean Class
userDaoImplclass com.app.dao.impl.UserDaoImpl
resourceServiceImplclass com.app.service.impl.ResourceServiceImpl$$EnhancerBySpringCGLIB$$4ddf6901
roleServiceImplclass com.app.service.impl.RoleServiceImpl
userServiceImplclass com.app.service.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$e7bcd944

以上 bean 都采用 @Repository 或 @Service 的注解方式。

注意标红Class名称,这是因为被 CGLIB 动态代理。 

 

(2)根据 Shiro 示例,新增配置文件 applicationContext-security.xml,然后配置 Shiro 如下:

... ...

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
    </bean>

    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
     
    <bean id="formAuthenticationFilter"  class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"/>

... ...

 加入 Shiro 之后,发现有些地方 getBean(...) 出错,调用 getBeanDefinitionNames() 输出的 Beans 如下:

Bean NameBean Class
userDaoImplclass com.app.dao.impl.UserDaoImpl
resourceServiceImplclass $Proxy38
roleServiceImplclass com.app.service.impl.RoleServiceImpl
userServiceImplclass $Proxy32

可以注意到标红Class名称,这是由于配置 DefaultAdvisorAutoProxyCreator,又被 JDK 动态二次代理。 

 

【解决办法】 

两次都以 CGLIB 动态代理。在 AOP 配置加上 proxy-target-class 属性,值为 true。具体如下:

(1)在 applicationContext.xml 中配置

... ...
    <aop:config expose-proxy="true" proxy-target-class="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(* com.app..service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
... ...

(2)在 applicationContext-security.xml 配置 Shiro

... ...
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true"/>  
    </bean>
... ...

修改完成后,重新运行程序,getBean 恢复正常。

 

【参考资料】

spring的二次代理原因及如何排查  http://jinnianshilongnian.iteye.com/blog/1894465

 

转载于:https://www.cnblogs.com/FrankTang/p/springmvc_shiro_dynamicproxy.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值