在Shiro整合Springboot和redis,jwt过程中,编写完成ShiroConfig之后启动项目报错
Description:
Field shiroFilterChainDefinition in org.apache.shiro.spring.web.config.AbstractShiroWebFilterConfiguration required a bean of type 'org.apache.shiro.spring.web.config.ShiroFilterChainDefinition' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.apache.shiro.spring.web.config.ShiroFilterChainDefinition' in your configuration.
原因是:使用了如下依赖之后,自定义的Realm竟然和authorizer冲突了
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.4.0-RC2</version>
</dependency>
或者
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
解决方法:在ShiroConfig文件中的getShiroFilterFactoryBean方法上加注解@Bean(“shiroFilterFactoryBean”)
@Bean("shiroFilterFactoryBean")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
Map<String,String> map = new HashMap<>();
map.put("/user/login","anon");
map.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
return shiroFilterFactoryBean;
}
如果无法解决,则在ShiroConfig中单独写一个ShiroFilterChainDefinition的bean
@Bean("shiroFilterFactoryBean")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager,ShiroFilterChainDefinition shiroFilterChainDefinition){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
Map<String, Filter> filters = new HashMap<>();
filters .put("jwt", new JwtFilter());
shiroFilterFactoryBean.setFilters(filters );
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
Map<String, String> filterMap = shiroFilterChainDefinition.getFilterChainMap();
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactoryBean;
}
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/**", "jwt");
chainDefinition.addPathDefinitions(filterMap);
return chainDefinition;
}
详细分析见:https://www.cnblogs.com/insaneXs/p/11028286.html
|