本人在开发现在的web项目中的时候,碰到了一个问题,大概就是说调用不到SecurityManager。报错的是其中一个Controller,上网查了一下,看了下代码,定位到了类的开头定义的一个通过Shiro获取用户的登录名的变量。一开始以为我是因为final关键字不能修饰session这种动态的变量。后面删除final也没有变化还是报错。之后我把从Shiro获取用户名删掉,只用字符串的时候就可以运行了。
报错代码:
private static final String REDIS_KEY = ShiroUtils.getLoginName() + "12345";
推测是因为当在类的开头获取登录名的时候,Controller只有在方法内,即处理转发用户请求时才会有Shiro.session的作用域,不在方法内获取的Shiro.session并不知道你是哪个用户,方法外并没有他的作用域,也就没有session了,因此报了这个错误。
完整报错如下:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxController' defined in file [xxxController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxx.xxxController]: Constructor threw exception; nested exception is 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.
参考博客:https://blog.youkuaiyun.com/catoop/article/details/63257702

本文探讨了在Spring MVC项目中使用Shiro时遇到的SecurityManager未初始化问题,详细分析了在Controller类中提前调用Shiro获取登录名导致的错误,并提供了可行的解决方案。
3万+

被折叠的 条评论
为什么被折叠?



