SpringBoot整合Shiro报错:The dependencies of some of the beans in the application context form a cycle
SpringBoot整合Shiro遇到循环依赖问题…
问题代码:ShiroConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| @Configuration public class ShiroConfig {
@Bean public DefaultSecurityManager securityManager(UserRealm userRealm) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(userRealm);
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO(); DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator(); defaultSessionStorageEvaluator.setSessionStorageEnabled(false); subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
securityManager.setSubjectDAO(subjectDAO); return securityManager; }
@Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager, JWTFilter jwtFilter) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
Map<String, Filter> newFilters = new HashMap<>(); newFilters.put("jwt", jwtFilter); shiroFilterFactoryBean.setFilters(newFilters);
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, String> map = new LinkedHashMap<>(); map.put("/login", "anon"); map.put("/register", "anon");
map.put("/swagger/**", "anon"); map.put("/v2/api-docs", "anon"); map.put("/swagger-ui.html", "anon"); map.put("/swagger-resources/**", "anon"); map.put("/webjars/**", "anon"); map.put("/favicon.ico", "anon"); map.put("/captcha.jpg", "anon"); map.put("/csrf", "anon");
map.put("/**", "jwt"); shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
return shiroFilterFactoryBean; }
@Bean public FilterRegistrationBean<Filter> registration(JWTFilter jwtFilter) { FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>(jwtFilter); registration.setEnabled(false); return registration; }
}
|
报错信息:
解决方案
在ShiroConfig.java类中添加如下方法:
1 2 3 4 5 6
| @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) { AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor(); advisor.setSecurityManager(securityManager); return advisor; }
|