alexxiyang / shiro-redis

shiro only provide the support of ehcache and concurrentHashMap. Here is an implement of redis cache can be used by shiro. Hope it will help you!
MIT License
1.17k stars 444 forks source link

关于使用ThreadLocal引起的session共享的问题 #34

Closed Pramy closed 6 years ago

Pramy commented 6 years ago

在web环境下面,tomcat使用的nio的reactor的线程模型,而且这些nio线程不会死掉,一直在轮询,意味着线程都不会死掉,为其保存的副本也会一直存在,在一次请求的过程中可能不是同一个线程,所以在调用doReadSession的方法时候使用ThreadLocal会产生session共享问题

alexxiyang commented 6 years ago

你的意思是ThreadLocal对象有可能会给多个用户共享使用。

是这个意思吗?

你看看这个改进方案不知道是否可行:

ThreadLocal中由直接保存session对象改为保存一个Map,map的键使用realmName,值使用session

Pramy commented 6 years ago

就是nio的缘故,在web环境下,假设A第一次请求(该线程为Thread-1),然后保存了session到了Thread-1中,A第二次发起请求就不一定是Thread-1线程了,你试试打印线程名字你就明白了,我参照了你的项目写了一个springboot的starter,我使用的是guava 的 cache来解决redis的多次读写问题,可能写得比较粗糙,你有兴趣可以看看https://github.com/Pramy/shiro-redis-spring-boot-starter

alexxiyang commented 6 years ago

引入ThreadLocal是为了解决单次请求中访问REDIS次数过多而引入的。未引入TheadLocal之前,登陆过程需要访问10次Redis。所以第二次发起请求换了线程是没有关系的。但若是同一个线程会给另外一个session使用,便会出问题了。

Pramy commented 6 years ago

如果用了key ,value的方式就不会出现问题,不过这样的话我个人觉得是用弱引用比较好点

alexxiyang commented 6 years ago

清升级至最新版本,已经加上了key value的设置,不会出现变量混用