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<>();
//設(shè)置我們自定義的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"); // 主要通過注解方式校驗權(quán)限
chainDefinition.addPathDefinitions(filterMap);
return chainDefinition;
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 如何使用jwt+redis實現(xiàn)單點登錄
- 使用Redis實現(xiàn)JWT令牌主動失效機制
- SpringSecurity+Redis+Jwt實現(xiàn)用戶認證授權(quán)
- jwt+redis實現(xiàn)登錄認證的示例代碼
- 基于 Redis 的 JWT令牌失效處理方案(實現(xiàn)步驟)
- springboot+springsecurity+mybatis+JWT+Redis?實現(xiàn)前后端離實戰(zhàn)教程
- SpringBoot整合SpringSecurity和JWT和Redis實現(xiàn)統(tǒng)一鑒權(quán)認證
- SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認證的實現(xiàn)
- java實現(xiàn)認證與授權(quán)的jwt與token+redis,哪種方案更好用?
相關(guān)文章
Java Web使用POI導(dǎo)出Excel的方法詳解
這篇文章主要介紹了Java Web使用POI導(dǎo)出Excel的方法,結(jié)合實例形式詳細分析了Java Web使用POI導(dǎo)出Excel的具體操作步驟、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2017-06-06
java?CompletableFuture異步任務(wù)編排示例詳解
這篇文章主要為大家介紹了java?CompletableFuture異步任務(wù)編排示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
springboot如何通過@PropertySource加載自定義yml文件
這篇文章主要介紹了springboot如何通過@PropertySource加載自定義yml文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

