Shiro整合Springboot和redis,jwt过程中的错误shiroFilterChainDefinition问题
Shiro整合Springboot和redis,jwt过程错误shiroFilterChainDefinition
在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); // 添加自己的过滤器并且取名为jwt Map<String, Filter> filters = new HashMap<>(); //设置我们自定义的JWT过滤器 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; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringCloud Stream 整合RabbitMQ的基本步骤
这篇文章主要介绍了SpringCloud Stream 整合RabbitMQ的基本步骤,从项目介绍到生产者结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-03-03SpringBoot使用@PathVariable进行数据校验的流程步骤
在SpringBoot项目中,我们经常需要从 URL 中获取参数并进行相关的数据校验,而@PathVariable注解就是一种非常方便的方式,可以让我们在方法参数中直接获取URL中的参数,并进行数据校验,本文将介绍如何使用@PathVariable注解进行数据校验2023-06-06
最新评论