---
title: "org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessi"
author: "anduo1989"
date: "2014-01-16T10:16:35Z"
tags: []
categories: ["企业架构"]
slug: "org-apache-shiro-unavailablesecuritymanagerexception-no-securitymanager-accessi"
draft: false
lastmod: "2014-01-16T10:16:35Z"
id: 2004795
url: "https://anduo.iteye.com/blog/2004795"
---

关于shiro错误的分析  
错误提示：

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.

 

错误原因：

在web.xml中配置shiro filter的时候，shiro filter放置位置放到了struts2 filter后面

 

原因分析：

如果使用struts2，那么在struts2加载静态资源的时候，需要将静态资源SecurityUtils也加载进去，如果将shiro filter放置位置放到了struts2 filter后面，那么必将导致无法加载到struts2中去，而后使用SecurityUtils.getSubject();的时候，导致出错，

 

解决办法：

shiro的filter应该放在struts2的 filter的上面

还可以在使用之前使用这种方式：即在SecurityUtils.getSubject();之前加入如下代码

 

**\[java]** [view plain](http://blog.csdn.net/gsying1474/article/details/9266181 "view plain")[copy](http://blog.csdn.net/gsying1474/article/details/9266181 "copy")[print](http://blog.csdn.net/gsying1474/article/details/9266181 "print")[?](http://blog.csdn.net/gsying1474/article/details/9266181 "?")

 

1. Factory&lt;SecurityManager&gt; factory = new IniSecurityManagerFactory("classpath:shiro.ini");
2. // 创建SecurityManager (根据配置创建SecurityManager实例)
3. SecurityManager security = factory.getInstance();
4. SecurityUtils.setSecurityManager(security);

这个想必不用解释了吧