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 {
@Autowired
private 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
*/
@Bean
protected 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,如下图所示: