先说出现错误的可能原因吧!
1:web.xml配置中的filter-name的shiroFilter 与 spring-shiro.xml 中的 beanid命名的shiroFilter不一致。
2:保证在启动WEB容器的时候能加载到 spring-shiro.xml 文件,检查文件路径是否正确,参考:classpath*:spring-shiro.xml。
3:很有可能是在web.xml中没有配置监听器类ContextLoaderListener,导致即使加载了spring-shiro.xml,但是未保存到上下文环境中,从而导致No bean named 'shiroFilter' is defined 。
4:其它原因
下面是web.xml部分配置和spring-shiro.xml的配置:
web.xml中的配置:
<!-- ****************************shiro 安全框架 *****************************begin -->
<!-- 如果同一个bean被定义两次,后面一个优先 -->
<!-- classpath*与classpath的区别:前者遍历所有的classpath加载所有符合条件的资源(效率较差),后者只会去找第一个符合条件的资源(避免重名) -->
<context-param>
<description></description>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-shiro.xml
</param-value>
</context-param>
<!-- 千万记得用这个监听器类,否则可能出现加载xml文件中的bean找不到错误 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- shiro过滤器,
DelegatingFilterProxy代理会自动的到IOC容器找名字为shiroFilter的bean;
如果要修改bean的名字,可以设置targetBeanName的值,要注意的是必须与ShiroFilterFactoryBean的id值保持一致
-->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ****************************shiro 安全框架 *****************************end -->
spring-shiro.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
<!-- 配置安全管理器securityManager, 缓存技术: 缓存管理 realm:负责获取处理数据 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myShiroRealm" />
<property name="cacheManager" ref="cacheManager" />
</bean>
<!-- 項目自定义的Realm,从数据库中获取用户的安全数据 -->
<bean id="myShiroRealm" class="com.project.base.shiro.UserRealm">
<!-- 配置缓存管理器-->
<property name="cacheManager" ref="cacheManager" />
<!-- 配置加密器 -->
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"></property> <!-- 加密算法的名称 -->
<property name="hashIterations" value="1024"></property> <!-- 配置加密的次数 -->
</bean>
</property>
</bean>
<!-- 用户授权信息Cache -->
<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
<!-- 必须配置lifecycleBeanPostProcessor:管理shiro中常见的对象 -->
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- shiro的核心配置: 配置shiroFileter id名必须与web.xml中的filtername保持一致 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login.jsp" />
<property name="successUrl" value="/index.jsp" />
<property name="unauthorizedUrl" value="/unauthorized.jsp" />
<!-- shiro过滤器的具体配置 -->
<!-- anon-匿名访问(不需要验证),authc-需要认证 -->
<property name="filterChainDefinitions">
<value>
/login.jsp = anon
/user/logon = anon
/index.jsp = authc
/admin/** = roles[admin]
/user/** = roles[user]
/logout=logout
/** = authc
</value>
</property>
</bean>
</beans>