开发者社区> 单红宇> 正文

Shiro报错No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/63257702 问题描述: 项目使用 SpringMVC 并使用 Shiro 来管理Session控制权限。
+关注继续查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/63257702

问题描述:
项目使用 SpringMVC 并使用 Shiro 来管理Session控制权限。

经常会不定期的发现异常:

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.
    at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123)
    at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:627)
    at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)
    ……………………
    ……………………
    ……………………

通俗的对错误原因描述为:
你所请求的URL是不在Shiro所管辖范围的,而你又在你请求的这个URL后试图通过Shiro来获取Session,所以对Shiro来说“你不让我负责的事,为什么要跟我要结果”。

下面是我web.xml 原来的配置:

    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   

我修改后的配置为:

    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>   

配置的区别显而易见。

至此,我再说一下,我后来总结出来我出现这个异常的必现操作:就是我访问的页面是404的时候,异常100%出现。

404很明显是一个ERROR,此前我没有添加 <dispatcher>ERROR</dispatcher>,而我在发生错误的时候又使用 SecurityUtils.getSubject().getSession() 来获取Session(我需要从会话中读取相关数据记录日志),所以被Shiro 所拒绝(抛出异常)。

如果你使用Shiro,并且没有配置 <dispatcher>ERROR</dispatcher> 。那么,你登录后,随便访问一个不存在的页面触发404,或者触发500这样的错误页。再回头看登录后的页面,登录状态就丢失了。因为这个时候sessionid 被重新生成覆盖了登录状态时的sessionid。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Zp
org.apache.ibatis.binding.BindingException: Parameter 'XXXX' not found.的问题解决办法
org.apache.ibatis.binding.BindingException: Parameter 'XXXX' not found.的问题解决办法
141 0
### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating
在写数据库的url地址时用了“&”符号,将“&”换成“&;”
58 0
严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
34 0
springboot整合mybatis中:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
在项目的启动过程中如果出现:以下报错信息,则可以从以下几个方面入手排查问题。 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比,这是第二次出现这种异常。我每次都是按照如下方法去逐一排查的,到最后都能解决问题。
353 0
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found
31 0
Unable to load class 'org.gradle.api.internal.component.Usage
Unable to load class 'org.gradle.api.internal.component.Usage
27 0
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rg.eduservice..
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rg.eduservice..
65 0
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
56 0
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
203 0
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
95 0
+关注
单红宇
软件从业者,爱好技术。
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
Apache Wicket User Guide - Ref
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载