引言:

本人在使用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. Django框架----外键关联

    app/models.py中: 创建班级表 class classes(models.Model): id = models.AutoField(primary_key=True) name = mo ...

  2. 使用Wisdom RESTClient自动化测试REST API,如何取消对返回的body内容的校验?

    使用Wisdom RESTClient V1.1 自动化测试API,默认是对返回HTTP状态码和body内容进行校验的. 如果您的API返回body内容是变化的,可以通过设置来取消对body内容的校验 ...

  3. await

    单个的task await task 多个await asyncio.wait(tasks)

  4. 一、初始PS软件

     PS的介绍 Adope Photoshop,简称“PS”,是由Adope Systems开发和发行的图像处理软件. Photoshop主要处理以像素所构成的数字图像,使用其众多的编修与绘图工具,可以 ...

  5. oracle闪回、闪回数据归档Flashback Data Archive (Oracle Total Recall)的真正强大之处、11gR2增强以及合理使用

    oracle的闪回很早就出来了,准确的说一直以来应该都较少被真正用户广为使用,除了dba和极少部分开发人员偶尔用于逻辑出错.误删恢复之外,较少被用于产生更有价值的用途. 各种闪回表flashback ...

  6. 14: element ui 使用

    1.1 element ui 基本使用 参考网址: http://element.eleme.io/#/zh-CN/component/button 1.初始一个vue项目并安装element ui ...

  7. 08: vue组件

    1.1 初识组件 1.什么是组件 1. Html中有组件,是一段可以被复用的结构代码 2. Css中有组件,是一段可以被复用的样式 3. Js中有组件,是一段可以被复用的功能 4. Vue中也有组件, ...

  8. 导入maven项目各个注解均报错了

    所遇问题: 导入maven项目各个注解均报错了; 思考1: 这个项目使用了springboot;spring是个”大容器”,所有对象的创建和管理都交给了它, (SpringBoot是一个框架,一种全新 ...

  9. Node.js初探

    1, 设计高性能.Web服务器的几个要点:事件驱动.非阻塞I/O 2,常见Web服务器架构: Web服务器的功能: 接受HTTP请求(GET.POST.DELETE.PUT.PATCH) 处理HTTP ...

  10. HDU 1848 Fibonacci again and again【博弈SG】

    Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F( ...