定時任務:No SecurityManager accessible to the calling code

原文鏈接:https://blog.csdn.net/f1370335844/article/details/96845039

Cause: org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.

問題現象:

安全框架用的shiro,然後寫了一個定時任務,報錯,看報錯內容不讓執行定時任務。未綁定SecurityManager啥的。

解決方法:

看報錯是ThreadContext未綁定SecurityManager,那就幫頂下,在定時任務最開始的代碼前加上:

DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
ThreadContext.bind(manager);
重新啓動項目,定時任務正常運行。

帶來的問題與優化:

每次定時任務啓動就要new一個DefaultWebSecurityManager,這是個問題;優化下,可以將DefaultWebSecurityManager 作爲靜態變量,或者放到spring容器中
————————————————
版權聲明:本文爲CSDN博主「汽水貓」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/f1370335844/article/details/96845039

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章