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 Name Bean Class
userDaoImpl class com.app.dao.impl.UserDaoImpl
resourceServiceImpl class com.app.service.impl.ResourceServiceImpl$$EnhancerBySpringCGLIB$$4ddf6901
roleServiceImpl class com.app.service.impl.RoleServiceImpl
userServiceImpl class 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 Name Bean Class
userDaoImpl class com.app.dao.impl.UserDaoImpl
resourceServiceImpl class $Proxy38
roleServiceImpl class com.app.service.impl.RoleServiceImpl
userServiceImpl class $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

Spring mvc 中有关 Shiro 1.2.3 配置问题的更多相关文章

  1. 细说shiro之五:在spring框架中集成shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

  2. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  3. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  4. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  5. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  6. Spring MVC中处理静态资源的多种方法

    处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...

  7. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  8. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  9. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

随机推荐

  1. 怒刷BZOJ记录(一)1001~1037

    我实在是太弱了...不滚粗只能刷BZOJ了...这里来记录每天刷了什么题吧. 2015-7-9 : 1001[BeiJing2006]狼抓兔子                 | 最大流练习(呃..其 ...

  2. Linux Kernel 'MSR' Driver Local Privilege Escalation

    本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! // PoC exploit for /dev/cpu/*/msr, 32bit userland on a 64bit hos ...

  3. Linux Kernel 远程拒绝服务漏洞

    漏洞名称: Linux Kernel 远程拒绝服务漏洞 CNNVD编号: CNNVD-201307-309 发布时间: 2013-07-18 更新时间: 2013-07-18 危害等级:    漏洞类 ...

  4. Linux下的定时器:alarm()与setitimer()

    Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...

  5. 暴力求解——Equivalent Strings

    Submit Status Description Today on a lecture about strings Gerald learned a new definition of string ...

  6. Codelab for Android Design Support Library used in I/O Rewind Bangkok session

    At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...

  7. bzoj1588 [HNOI2002]营业额统计(Treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 11485  Solved: 4062[Submit][Sta ...

  8. Sum It Up(搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 include include include include using namespace std; ...

  9. C#中常用接口介绍

    1. IComparable接口 IComparable接口定义通用的比较方法.由类型使用的IComparable接口提供了一种比较多个对象的标准方式.如果一个类要实现与其它对象的比较, 则必须实现I ...

  10. 深入了解JavaScript中的for循环

    在ECMAScript5中,有三种for循环,分别是: 简单for循环 for-in forEach 在ES6中,新增了一种循环 for-of 简单for循环 const arr = [1, 2, 3 ...