原文:https://www.cnblogs.com/liruiloveparents/p/9392159.html

shiro中部分SpringCache失效问题

 

1、问题抛出

今天在做Springboot和shiro集成时,发现一个严重的问题。部分service的缓存和事务失效,debug代码时,发现这些有问题的service实例都不是代理生成的,所以事务和缓存就失效了(事务和缓存依赖代理类实现)。继续查问题,发现这些有问题的service全部被shiro的realm所依赖,所以怀疑是shiro影响了

所以做一下测试:

shiro中用到的ResourceService

public class LocalRealmService extends RealmService {
@Autowired
private ResourceService resourceService;
...
}

controller也调用ResourceService

@RestController
@RequestMapping(value = "/test")
public class CacheController {
private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private ResourceService resourceService;
}

结果发现resourceService的实例如图:

发现问题:resourceService的实例不是代理即缓存注解和事务全部生效(缓存和事务都是代理完成的)

当我把resourceService从realm依赖中删除时,在controller引用时resourceService的实例就是“代理”即缓存和事务生效

结论:只要被shiro的realm所依赖的service,代理会全部失效(暂时没撸源码,还不知原理,知道的童鞋可以说下,谢了)

2、解决的方案

常用的解决方式有三种:

第一种:这是网上比较多的

就是realm中不要依赖service,依赖dao

第二种:在依赖的service上添加@Lazy注解

延迟加载,就是在实例化shiro的realm时,不去实例化service的bean,等到用的时候再从spring容器中去取对应的Bean

public class LocalRealmService extends RealmService {

    @Lazy
@Autowired
private ResourceService resourceService;
...
}

这种解决方案让我感觉到:这里是不是存在多个上下文,或者不是spring?这里有待后续考证。。。

第三种:shiro在实例化securityManager时,先不设置realm,等到容器加载完再设置

这种方式与第二种类似,只不过无需在每个service属性上增加@Lazy注解

SecurityMangaerd的实例化

/**
* 注释掉realm
*/
@Bean("securityManager")
public DefaultWebSecurityManager securityManager(/*@Qualifier("realm") BootRealm BootRealm,*/
SessionManager sessionManager,
CacheManager shiroCacheManager) {
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
//注释掉
//manager.setRealm(hyBootRealm);
}

容器加载完设置realm:这里有多重方案,主要列举两种

I、利用spring监听

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; @Component
public class ShiroRealmListener implements ApplicationListener {
@Autowired
private DefaultWebSecurityManager securityManager;
@Autowired
private HyBootRealm realm; @Override
public void onApplicationEvent(ApplicationEvent event) {
securityManager.setRealm(realm);
}
}

II、利用springboot的ApplicationRunner

import com.chyjr.hyboot.security.shiro.realm.HyBootRealm;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; public class ShiroRealmRunner implements ApplicationRunner { @Autowired
private DefaultWebSecurityManager securityManager;
@Autowired
private HyBootRealm realm; @Override
public void run(ApplicationArguments args) throws Exception {
securityManager.setRealm(realm);
}
}

注意:ShiroRealmRunner必须是spring的Bean,所以在配置管理类中要添加:

@Bean
public ShiroRealmRunner shiroRealmRunner(){
return new ShiroRealmRunner();
}

shiro中部分SpringCache失效问题的更多相关文章

  1. 从零到实现Shiro中Authorization和Authentication的缓存

    本文大纲 一.简介 二.缓存的概念 三.自定义实现缓存机制 四.什么是Ehcache 五.Ehcache怎么用 六.Spring对缓存的支持 七.Spring+Ehcache实现 八.Spring+S ...

  2. jquery中Live方法不可用,Jquery中Live方法失效

    jquery中Live方法不可用,Jquery中Live方法失效 >>>>>>>>>>>>>>>>> ...

  3. shiro中 UnknownAccountException

    一 shiro的session.request和response与服务端容器自身的这三个对象的关系 在web.xml中配置了一个Filter,拦截/*,所有的uri.在拦截器中还会调用ShiroFil ...

  4. shiro中CacheManager相关的类结构介绍,提供redis Cache实现

    cacheManager主要用于对shiro中的session.realm中的认证信息.授权信息进行缓存. 1.类结构 2.接口及类介绍 CacheManager 提供根据名字获取cache的作用. ...

  5. j2ee5.0开发中jstl标签失效

    尝试了下,对于Weblogic中的出现的错误,也是有效的!   j2ee5.0开发中jstl标签失效 原因不详, 解决办法, 一:将.web.xml中 <web-app version=&quo ...

  6. Shiro中的授权问题(二)

    上篇博客(Shiro中的授权问题 )我们介绍了Shiro中最最基本的授权问题,以及常见的权限字符的匹配问题.但是这里边还有许多细节需要我们继续介绍,本节我们就来看看Shiro中授权的一些细节问题. 验 ...

  7. Shiro中的授权问题

    在初识Shiro一文中,我们对Shiro的基本使用已经做了简单的介绍,不懂的小伙伴们可以先阅读上文,今天我们就来看看Shiro中的授权问题. Shiro中的授权,大体上可以分为两大类,一类是隐式角色, ...

  8. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  9. Shiro中的Rememberme后出现浏览器500错误

    问题详述:在Shiro中添加Remember me功能后,只要勾选Remember me选项为true的时候,浏览器就会跳转到一个不可达页面,并且在Chrome中显示HTTP 500错误. 问题追踪: ...

随机推荐

  1. python抓取链家房源信息(三)

    之前写过一个链家网北京二手房的数据抓取,然后本来今天想着要把所有的东西弄完,但是临时有事出去了一趟,耽搁了一下,然后现在是想着把北京的二手房的信息都进行抓取,并且存储在mongodb中, 首先是通过' ...

  2. hdu 5875(单调栈)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. java IntelliJ IDEA 13 注册码 IDEA序列号 License Key

    java IntelliJ IDEA 13 注册码 IDEA序列号 License Key Username: JavaDeveloper@sskaje.me License: 282971-M1NW ...

  4. Python 读写xlsx

    # pip install openpyxl # openpyxl只能用于处理xlsx,不能用于处理xlsfrom openpyxl import load_workbook # 打开文件ExcelF ...

  5. python调用api方式

    1.shell版本 #!/bin/bash #根据api提供商,获取指定时间格式 datestr=`xxx` #根据api提供商,获取指定加盐密码格式 pwdstr=`xxx` curl -s -X ...

  6. 【LOJ】#2350. 「JOI 2017/2018 决赛」月票购买

    题解 首先求一个最短路图出来,最短路图就是这条边在最短路上就保留,否则就不保留,注意最短路图是一个有向图,一条边被保留的条件是 dis(S,u) + val(u,v) = dis(v,T)我们需要求两 ...

  7. Deepin 2015 安装后找不到win10 启动选项的解决办法

    #sudo vi /boot/grub/grub.cfg 在export linux_gfx_mode后面加 menuentry "Windows 10 (loader)" --c ...

  8. bzoj 1483 链表 + 启发式合并

    思路:将颜色相同的建成一个链表, 变颜色的时候进行链表的启发式合并.. 因为需要将小的接到大的上边,所以要用个f数组. #include<bits/stdc++.h> #define LL ...

  9. 【转】django 与 vue 的完美结合 实现前后端的分离开发之后在整合

    https://blog.csdn.net/guan__ye/article/details/80451318   最近接到一个任务,就是用django后端,前段用vue,做一个普通的简单系统,我就是 ...

  10. Java常用工具类之时间转换

    import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; ...