解决spring boot shiro的 Consider defining a bean named ‘shiroFilterFactoryBean‘ in your configuration问题
文章目录
- 1. 复现问题
- 2. 分析问题
- 3. 解决问题
1. 复现问题
今天在启动spring boot项目时。出现了如下错误:
具体错误信息如下:
Description:Method filterShiroFilterRegistrationBean in org.apache.shiro.spring.config.web.autoconfigure.ShiroWebFilterConfiguration required a bean named 'shiroFilterFactoryBean' that could not be found.Action:Consider defining a bean named 'shiroFilterFactoryBean' in your configuration.
上述错误就是我们没有定义一个名为shiroFilterFactoryBean
的bean
,那么,这个bean有什么作用呢?
2. 分析问题
这是配置shiro
出现的问题,具体代码如下:
/*** @author zby* @datetime 2022/8/19 09:39* @desc shiro配置类*/
@Configuration
public class ShiroConfig {@Autowiredprivate JwtTokenProvider jwtTokenProvider;/*** 设置过滤器** @author zby* @datetime 2022/8/19:13:06* @return shiroFilter过滤器*/@Bean("shiroFilter")public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();// 配置安全管理shiroFilterFactoryBean.setSecurityManager(securityManager);Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();filters.put(AUTHC_TOKEN, new JwtAuthFilter(jwtTokenProvider));// filters.put("anyRole", createRolesFilter());shiroFilterFactoryBean.setFilters(filters);shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition().getFilterChainMap());return shiroFilterFactoryBean;}/*** 禁用session, 不保存用户登录状态。保证每次请求都重新认证。 需要注意的是,如果用户代码里调用Subject.getSession()还是可以用session。* 如果要完全禁用,要配合下面的noSessionCreation的Filter来实现** @author zby* @datetime 2022/8/19:14:20* @return*/@Beanprotected SessionStorageEvaluator sessionStorageEvaluator() {DefaultWebSessionStorageEvaluator sessionStorageEvaluator =new DefaultWebSessionStorageEvaluator();sessionStorageEvaluator.setSessionStorageEnabled(false);return sessionStorageEvaluator;}/*** DbRealm,默认的密码校验算法为BCrypt** @author zby* @datetime 2022/8/19:14:22* @return*/@Bean("dbRealm")public Realm dbShiroRealm() {DbShiroRealm myShiroRealm = new DbShiroRealm();// 将Realm的默认密码校验设置为BCrypt算法myShiroRealm.setCredentialsMatcher((authenticationToken, authenticationInfo) -> {String password =new String(((UsernamePasswordToken) authenticationToken).getPassword());String hashed = (String) authenticationInfo.getCredentials();return BCrypt.checkpw(password, hashed);});return myShiroRealm;}
}
在ShiroConfig
类中虽然实例化ShiroFilterFactoryBean
对象,但是 @Bean("shiroFilter")
里写的是shiroFilter
,而不是shiroFilterFactoryBean
,故而报错。
3. 解决问题
既然知道了是什么原因造成的,我们便可以将上述代码进行如下修改:
/*** 设置过滤器** @author zby* @datetime 2022/8/19:13:06* @return shiroFilter过滤器*/
@Bean("shiroFilterFactoryBean")
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();// 配置安全管理shiroFilterFactoryBean.setSecurityManager(securityManager);Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();filters.put(AUTHC_TOKEN, new JwtAuthFilter(jwtTokenProvider));// filters.put("anyRole", createRolesFilter());shiroFilterFactoryBean.setFilters(filters);shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition().getFilterChainMap());return shiroFilterFactoryBean;
}
如此便能成功启动spring boot,如下图所示:
解决spring boot shiro的 Consider defining a bean named ‘shiroFilterFactoryBean‘ in your configuration问题相关推荐
- Consider defining a bean named 'entityManagerFactory' in your configuration解决办法
错误信息: *************************** APPLICATION FAILED TO START ***************************Description ...
- Consider defining a bean named ‘entityManagerFactory‘ in your configuration
先不要急着删除maven仓库中下载下来的依赖,先看看是否有如下配置: 上面这配置等于去除了自动注入数据源的操作,那entityManagerFactory自然就找不到了. 删除以上配置,允许自动注入数 ...
- 解决Spring boot中读取属性配置文件出现中文乱码的问题
解决Spring boot中读取属性配置文件出现中文乱码的问题 参考文章: (1)解决Spring boot中读取属性配置文件出现中文乱码的问题 (2)https://www.cnblogs.com/ ...
- (39.3) Spring Boot Shiro权限管理【从零开始学Spring Boot】
在学习此小节之前您可能还需要学习: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...
- Spring Boot Shiro 权限管理
Spring Boot Shiro 权限管理 标签: springshiro 2016-01-14 23:44 94587人阅读 评论(60) 收藏 举报 本来是打算接着写关于数据库方面,集成MyBa ...
- 解决Spring Boot报错Mapped Statements collection already contains value for...Error while adding the mapp
解决Spring Boot报错Mapped Statements collection already contains value for...Error while adding the mapp ...
- Spring Boot + Shiro 集成
2019独角兽企业重金招聘Python工程师标准>>> Spring Boot + Shiro 集成 Shiro 是一个流行的 Java 安全框架. 其实 Spring 有一个 Sp ...
- spring boot shiro redis整合基于角色和权限的安全管理-Java编程
一.概述 本博客主要讲解spring boot整合Apache的shiro框架,实现基于角色的安全访问控制或者基于权限的访问安全控制,其中还使用到分布式缓存redis进行用户认证信息的缓存,减少数据库 ...
- springboot导入项目依赖报错_如何解决spring boot 项目导入依赖后代码报错问题
如何解决spring boot 项目导入依赖后代码报错问题 2020-08-15 14:17:18 代码截图如图所示(由于本人问题已经解决,没来得及截图,所以在网上找了一张图片) 针对图中所示的 ...
最新文章
- CF1407D Discrete Centrifugal Jumps(单调队列+DP)
- 关于varchar(max), nvarchar(max)和varbinary(max)
- MySQL的常用语法
- 从零开始刷Leetcode——数组(31.33)
- 基于点云的视觉引导系统
- 图解设计模式:行为型模式之观察者模式
- JAVA后端开发常用的Linux命令总结
- Ubuntu20.04使用FreeRADIUS搭建EAP认证环境
- jenkins测试人员的使用
- mac多屏幕切换快捷键
- centos php安装 pecl,pecl是什么?如何在centos下安装pecl?
- 网贷平台倒闭了钱要还吗?网贷平台倒闭了怎么还钱
- 微信小程序制作天气查询系统
- Android仿网易游戏的精美开场动画+动画基础详解
- uni-app中设置不同平台显示不同的样式
- winwebmail轻松搭建邮件服务器
- 技巧2 router-link鼠标切换样式 vue3中.router-link-exact-active失效
- Oracle报错及解决办法
- VC++笔记(未整理版本)
- 楷书书法规则_《楷书之美》3分钟教会你如何欣赏楷书,明白楷书的基本规律!...