關(guān)于shiro中部分SpringCache失效問(wèn)題的解決方法
1、問(wèn)題拋出
今天在做Springboot和shiro集成時(shí),發(fā)現(xiàn)一個(gè)嚴(yán)重的問(wèn)題。部分service的緩存和事務(wù)失效,debug代碼時(shí),發(fā)現(xiàn)這些有問(wèn)題的service實(shí)例都不是代理生成的,所以事務(wù)和緩存就失效了(事務(wù)和緩存依賴代理類實(shí)現(xiàn))。繼續(xù)查問(wèn)題,發(fā)現(xiàn)這些有問(wèn)題的service全部被shiro的realm所依賴,所以懷疑是shiro影響了
所以做一下測(cè)試:
shiro中用到的ResourceService
public class LocalRealmService extends RealmService {
@Autowired
private ResourceService resourceService;
...
}
controller也調(diào)用ResourceService
@RestController
@RequestMapping(value = "/test")
public class CacheController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private ResourceService resourceService;
}
結(jié)果發(fā)現(xiàn)resourceService的實(shí)例如圖:

發(fā)現(xiàn)問(wèn)題:resourceService的實(shí)例不是代理即緩存注解和事務(wù)全部生效(緩存和事務(wù)都是代理完成的)
當(dāng)我把resourceService從realm依賴中刪除時(shí),在controller引用時(shí)resourceService的實(shí)例就是“代理”即緩存和事務(wù)生效
結(jié)論:只要被shiro的realm所依賴的service,代理會(huì)全部失效(暫時(shí)沒(méi)擼源碼,還不知原理,知道的童鞋可以說(shuō)下,謝了)
2、解決的方案
常用的解決方式有三種:
第一種:這是網(wǎng)上比較多的
就是realm中不要依賴service,依賴dao
第二種:在依賴的service上添加@Lazy注解
延遲加載,就是在實(shí)例化shiro的realm時(shí),不去實(shí)例化service的bean,等到用的時(shí)候再?gòu)膕pring容器中去取對(duì)應(yīng)的Bean
public class LocalRealmService extends RealmService {
@Lazy
@Autowired
private ResourceService resourceService;
...
}
這種解決方案讓我感覺(jué)到:這里是不是存在多個(gè)上下文,或者不是spring?這里有待后續(xù)考證。。。
第三種:shiro在實(shí)例化securityManager時(shí),先不設(shè)置realm,等到容器加載完再設(shè)置
這種方式與第二種類似,只不過(guò)無(wú)需在每個(gè)service屬性上增加@Lazy注解
SecurityMangaerd的實(shí)例化
/**
* 注釋掉realm
*/
@Bean("securityManager")
public DefaultWebSecurityManager securityManager(/*@Qualifier("realm") BootRealm BootRealm,*/
SessionManager sessionManager,
CacheManager shiroCacheManager) {
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
//注釋掉
//manager.setRealm(hyBootRealm);
}
容器加載完設(shè)置realm:這里有多重方案,主要列舉兩種
I、利用spring監(jiān)聽(tīng)
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();
}
總結(jié):
上述就是常用的解決方案,至于原理后續(xù)會(huì)抽時(shí)間研究....
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
SpringBoot中的靜態(tài)資源訪問(wèn)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot中的靜態(tài)資源訪問(wèn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Spring入門(mén)實(shí)戰(zhàn)之Profile詳解
什么是spring profile?簡(jiǎn)單講profile就是一組配置,不同profile提供不同組合的配置,程序運(yùn)行時(shí)可以選擇使用哪些profile來(lái)適應(yīng)環(huán)境。下面這篇文章主要介紹了Spring中Profile實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考借鑒。2017-02-02
SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處
這篇文章主要介紹了SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式詳析
這篇文章主要給大家介紹了關(guān)于Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式,在編程中我們經(jīng)常需要進(jìn)行各種數(shù)據(jù)類型之間的轉(zhuǎn)換操作,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
利用Java+MySQL實(shí)現(xiàn)附近功能實(shí)例
現(xiàn)在很多手機(jī)軟件都用附近搜索功能,但具體是怎么實(shí)現(xiàn)的呢?下面這篇文章就來(lái)給大家介紹關(guān)于利用Java+MySQL實(shí)現(xiàn)附近功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-12-12

