springboot整合shiro引⽤配置⽂件配置是出现的问题
Spring boot 整合shiro 使⽤yml配置⽂件
最近⾃⼰玩⼀下springBoot配置然后整合⼀下常⽤的框架,遇到⼀个问题。配置LifecycleBeanPostProcessor Bean 的时候总是先于spring 读取yml⽂件。导致配置失败
@Bean(name = "shiroEhcacheManager")
public EhCacheManager getEhCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile(cacheManagerConfigFilePath);
return em;
}
Error initializing bean [shiroEhcacheManager]; nested exception is org.apache.shiro.cache.CacheException:
java.lang.NullPointerException
很显然报出了⼀个空指针的错误,也就是 cacheManagerConfigFilePath 的值没有初始化成功,我原先以为我的yml⽂件配置错误,
回去检查了没问题,
于是我开始⼀个⼀个Bean 进⾏初始化,下⾯是我整个config的配置
@Configuration
@ConfigurationProperties(prefix = "shiro")
public class ShiroConfiguration {
private String cacheManagerConfigFilePath;
private String loginUrl;
private String successUrl;
private Map
filterChainDefinitionMap = new LinkedHashMap
();
@Bean(name = "shiroRealm")
public ShiroRealm getShiroRealm() {
return new ShiroRealm();
}
@Bean(name = "shiroEhcacheManager")
public EhCacheManager getEhCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile(cacheManagerConfigFilePath);
return em;
springboot是啥
}
@Bean(name = "lifecycleBeanPostProcessor")
public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
@Bean
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
daap.setProxyTargetClass(true);
return daap;
}
@Bean(name = "securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(AuthorizingRealm shiroRealm,EhCacheManager ehCacheManager) {        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
dwsm.setRealm(shiroRealm);
dwsm.setCacheManager(ehCacheManager);
return dwsm;
}
@Bean
public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
aasa.setSecurityManager(securityManager);
return new AuthorizationAttributeSourceAdvisor();
}
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean
.setSecurityManager(securityManager);
shiroFilterFactoryBean.setLoginUrl(loginUrl);
shiroFilterFactoryBean.setSuccessUrl(successUrl);
//filterChainDefinitionMap.put("/sa/**", "authc");
/
/filterChainDefinitionMap.put("/**", "anon");
System.out.println(filterChainDefinitionMap);
shiroFilterFactoryBean
.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
public String getCacheManagerConfigFilePath() {
return cacheManagerConfigFilePath;
}
public void setCacheManagerConfigFilePath(String cacheManagerConfigFilePath) {
this.cacheManagerConfigFilePath = cacheManagerConfigFilePath;
}
public String getLoginUrl() {
return loginUrl;
}
public void setLoginUrl(String loginUrl) {
this.loginUrl = loginUrl;
}
public String getSuccessUrl() {
return successUrl;
}
public void setSuccessUrl(String successUrl) {
this.successUrl = successUrl;
}
public  Map
getFilterChainDefinitionMap() {
return filterChainDefinitionMap;
}
}
public  void setFilterChainDefinitionMap(Map
filterChainDefinitionMap) {
this.filterChainDefinitionMap = filterChainDefinitionMap;
}
}
如果我注释掉shiroEhcacheManager 以下所有bean的创建,项⽬能启动,当我@Bean(name = "lifecycleBeanPostProcessor")创建这个bean的时候就抛出了上⾯的异常。这么分析过来问题应该出在这个bean上。
百度了⼀下,
LifecycleBeanPostProcessor⽤于在实现了Initializable接⼝的Shiro bean初始化时调⽤Initializable接⼝回调,在实现了Destroyable接⼝的
Shiro bean销毁时调⽤ Destroyable接⼝回调。
⽽我在创建的EhCacheManager 正是实现了Initializable接⼝,下⾯我截图了⼀张官⽅的在配置上⾯的说明
⼤概意思就是使⽤@Configuration配置,会会在上下⽂初始化的时候强制的注⼊⼀些依赖。导致⼀下不可知的初始化。尤其是创建BeanPostProcessor 和
BeanFactoryPostProcessor的时候(LifecycleBeanPostProcessor正是BeanPostProcessor 的⼦类)。应该讲这些创建Bean的⽅法前⾯加上static。让使⽤configuration的类在没有实例化的时候不会去过早的要求@Autowired和@Value
进⾏注⼊。
最后解决问题很简单,只要在创建LifecycleBeanPostProcessor的⽅法变为静态static⽅法。
其实我也没有彻底弄清楚出现这个问题的根本原因,如果有其他⼈知道的话,请指出来共同学习。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。