引言:

本人在使用spring集成shiro是总是报“no bean named 'shiroFilter' is defined”,网上的所有方式挨个试了一遍,又检查了一遍,

还是没有解决,最后,抱着试试看的心态,采用单元调试的方法终于解决了。

1.原因记录

以下是我报错的原因:

报错部分标记为红色,也被我注释掉。

第一个报错的地方是缓存管理器配置的有问题,第二个报错的地方是shiroFilter缺少值。

<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.xsd"> <!--1.配置SecurityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--<property name="cacheManager" ref="cacheManager" />-->
<property name="realm" ref="shiroRealm" />
<!--<property name="realms">
<list>
<ref bean="shiroRealm"/>
</list>
</property>-->
</bean> <!-- 2. 配置缓存管理器 cacheManager -->
<!-- <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
&lt;!&ndash;<property name="cacheManager" ref="ehCacheManager"/>&ndash;&gt;
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
</bean>--> <!-- 3. 项目自定义的Realm -->
<bean id="shiroRealm" class="com.interceptor.ShiroRealm" />
<!-- 4. Shiro生命周期处理器,可以自动的来调用配置在spring IOC 容器中shiro bean的生命周期方法-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!--5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用.-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> <!--6. 配置 ShiroFilter.
6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/> <!--安全管理-->
<property name="loginUrl" value="/"/> <!--登录页面-->
<property name="successUrl" value="/index.jsp"/> <!--登录成功后页面-->
<property name="unauthorizedUrl" value="/login/login"/> <!--
配置哪些页面需要受保护.以及访问这些页面需要的权限.
1). anon 可以被匿名访问
2). authc 必须认证(即登录)后才可能访问的页面.
3). logout 登出.
4). roles 角色过滤器
-->
<property name="filterChainDefinitions">
<value>
<!--请求登录-->
/login/login = anon
<!-- /login.jsp = anon
/shiro/login = anon
/shiro/logout = logout /user.jsp = roles[user]
/admin.jsp = roles[admin]--> <!--静态资料-->
/static/login/** = anon (由于粗心,当时写少了)
/static/js/** = anon # everything else requires authentication:
/** = authc
</value>
</property>
</bean>
</beans>

2. 分析解决

以下是解决这个错误的方法:

网上已有的方法,不在赘述。

采用@Test单元测试去进行精确定位错误

public class MybatisTest {
/**
* ClassPathXmlApplicationContext:是ApplicationContext的一个实现类
*/
private ApplicationContext ac = null;
// ClassPathXmlApplicationContext ac = null; /**
* 在所有测试方法之前执行这个标签
*/
@Before
public void init() {
ac = new ClassPathXmlApplicationContext("spring/spring-shiro.xml");
} /**
* shiro异常调试
*/
@Test
public void testShiro(){
// DefaultWebSecurityManager securityManager = ac.getBean("securityManager", DefaultWebSecurityManager.class);
// System.out.println(securityManager);
// LifecycleBeanPostProcessor lifecycleBeanPostProcessor = ac.getBean("lifecycleBeanPostProcessor", LifecycleBeanPostProcessor.class);
// System.out.println("lifecycleBeanPostProcessor = " + lifecycleBeanPostProcessor);
/* ShiroRealm shiroRealm = ac.getBean("shiroRealm", ShiroRealm.class);
System.out.println("shiroRealm = " + shiroRealm);*/
/* ShiroFilterFactoryBean shiroFilter = ac.getBean("shiroFilter", ShiroFilterFactoryBean.class);
System.out.println("shiroFilter = " + shiroFilter);*/
System.out.println("ac = " + ac);
}
}

3.架包导致的

架包也有可能导致“no bean named 'shiroFilter' is defined”,我的项目中还有一个原因导致了“no bean named 'shiroFilter' is defined”的问题。

在我自己搭建的项目中缺少了一个spring-web的架包(参照:spring各架包的作用),这个架包会导致报“no bean named 'shiroFilter' is defined”,

但是当把web.xml中的shiro过滤器注释掉以后,不在使用shiro,那就对项目其他功能以及使用不产生影响,这是我困惑的地方?在此记录一下,

对于其中的原因,如果有大神看到此博客,希望能解答一下!

  <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

另外,在记录一个场景,留给有缘人,希望不要像我一样花了两天时间才搞定:

我们公司有两个项目,两个项目都使用了shiro,而且都是maven项目,有一个项目在pom.xml中有写spring-web的索引,有一个项目中没有写spring-web的

索引,关键是两个项目都还能够正常使用shiro,我就是参照了没有使用spring-web索引的那个项目搭建的系统(运气太背),所以导致了spring没有创建shiroFilter这个

bean的异常。其实,当我再回过去看没有写spring-web索引的那个项目时,在依赖的架包里面是已经存在spring-web的这个架包的,这很有可能是使用maven自动

导包时,有的架包底层依赖了spring-web这个架包,所以在pom.xml没有写spring-web索引时,自动导入了,刚好解决了shiroFilter底层依赖的问题。

如果觉得有用,记得点赞哦!

spring集成shiro报错解决(no bean named 'shiroFilter' is defined)的更多相关文章

  1. springboot工程启动时,报错:No bean named 'shiroFilter' available

    在启动Springboot项目时,报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ' ...

  2. 使用SpringMVC报错 Error creating bean with name 'conversionService' defined in class path resource [springmvc.xml]

    使用SpringMVC报错 Error creating bean with name 'conversionService' defined in class path resource [spri ...

  3. NoSuchBeanDefinitionException: No bean named 'shiroFilter' is defined

    以前运行正常的项目,过了一段时间再运行时出问题,打开链接无反应,无法访问Tomcat,空白页面. 经检查发现,在Tomcat log中有报错: NoSuchBeanDefinitionExceptio ...

  4. spring Boot启动报错Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes

    spring boot 启动报错如下 org.springframework.context.ApplicationContextException: Unable to start web serv ...

  5. java数据库执行迁移报错Error creating bean with name 'flywayInitializer' defined in class path resource

    报错原因 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayI ...

  6. idea报错 Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource

    核对一下控制器是不是写了相同的路径...org.springframework.beans.factory.BeanCreationException: Error creating bean wit ...

  7. SpringBoot启动zipkin-server报错Error creating bean with name ‘armeriaServer’ defined in class path resource

    目前,GitHub 上最新 release 版本是 Zipkin 2.12.9,从 2.12.6 版本开始有个较大的更新,迁移使用 Armeria HTTP 引擎. 从此版本开始,若直接添加依赖的 S ...

  8. CXF和spring整合遇到的问题:No bean named 'cxf' is defined

    今天在做ws和spring整合的时候,很不幸的遇到了这个问题,百度了好久,竟然没人遇到这个问题,后来谷歌了一下,都是遇到这个问题的了...在看到一篇文章中提到了cxf.xml,所以我果断的打开这个配置 ...

  9. Spring:No bean named 'beanScope' is defined

    初学Spring,“No bean named 'beanScope' is defined”这个问题困扰了我好几个小时,查资料无果后,重写好几遍代码后发现问题居然是配置文件不能放在包里...要放在s ...

随机推荐

  1. 开启redis-server提示 # Creating Server TCP listening socket *:6379: bind: Address already in use--解决方法

    在bin目录中开启Redis服务器,完整提示如下: 3496:C 25 Apr 00:56:48.717 # Warning: no config file specified, using the  ...

  2. phpStudy集成环境下 安装composer

    报错提示: The "https://getcomposer.org/versions" file could not be downloaded: failed to open  ...

  3. bzoj2595 / P4294 [WC2008]游览计划

    P4294 [WC2008]游览计划 斯坦纳树 斯坦纳树,是一种神奇的树.它支持在一个连通图上求包含若干个选定点的最小生成树. 前置算法:spfa+状压dp+dfs(大雾) 我们设$f[o][P]$为 ...

  4. Linux一些基本配置

    Linux发行版:centos 6.5 配置yum源 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -P /etc/yum.repos ...

  5. k8s device plugin

    基本概念入门: Device Manager Proposal Device plugin offical Doc(中文) device-plugins offical Doc(En) Go thro ...

  6. tensorflow intel platform 优化

    intel平台优化 TensorFlow *是深度学习领域中主要使用的机器学习框架,要求高效利用计算资源. 为了充分利用英特尔架构和提高性能,TensorFlow *库已经使用英特尔MKL-DNN原语 ...

  7. 在eclipse中, 如何快速输入(快捷键)System.out.println();

    1.快速输入(快捷键)System.out.println(); 首先输入sysout或syso,然后ALT+/ System.out.println(); 2.快速输入(快捷键)System.err ...

  8. [c/c++] programming之路(4)、常量和变量

    一.打开多个程序(温习) #include<stdio.h> #include<stdlib.h> void main(){ ; ]; scanf("%d" ...

  9. 神秘的.user.ini文件

    原文链接:https://segmentfault.com/a/1190000011552335

  10. 20165211 2017-2018-2 《Java程序设计》课程总结

    20165211 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告博客链接汇总 预备作业1:我期望的师生关系 预备作业2:学习基础和C语言调查 预备作业3:Li ...