【发布时间】:2019-07-19 09:58:55
【问题描述】:
我有我的自定义领域
public class MyCustomRealm extends JdbcRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
// Custom code
}
}
shiro.ini 文件如下:
jdbcRealm= com.company.security.shiro.realm.MyCustomRealm
jdbcRealm.permissionsLookupEnabled = true
ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = jdbc:mysql://datasource
ds.user = user
ds.password = pass
jdbcRealm.dataSource=$ds
securityManager.realms = $jdbcRealm
有人知道为 spring-boot 项目配置/注册 shiro.ini 需要做什么吗?或者 SpringBootApp.java 文件需要什么配置?
@Bean
public Realm realm() {
Realm realm = new IniRealm("classpath:shiro.ini");
DefaultSecurityManager securityManager = new DefaultSecurityManager(realm);
SecurityUtils.setSecurityManager(securityManager);
return realm;
}
使用上面的 bean 得到以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroEventBusAwareBeanPostProcessor' defined in class path resource [org/apache/shiro/spring/boot/autoconfigure/ShiroBeanAutoConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authorizationAttributeSourceAdvisor' defined in class path resource [org/apache/shiro/spring/boot/autoconfigure/ShiroAnnotationProcessorAutoConfiguration.class]: Unsatisfied dependency expressed through method 'authorizationAttributeSourceAdvisor' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityManager' defined in class path resource [org/apache/shiro/spring/config/web/autoconfigure/ShiroWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'securityManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iniClasspathRealm' defined in class path resource [org/apache/shiro/spring/boot/autoconfigure/ShiroAutoConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.FatalBeanException: Error initializing bean [iniClasspathRealm]; nested exception is java.lang.IllegalStateException: Ini instance and/or resourcePath resulted in null or empty Ini configuration. Cannot load account data.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:490) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
Caused by: java.lang.IllegalStateException: Ini instance and/or resourcePath resulted in null or empty Ini configuration. Cannot load account data.
at org.apache.shiro.realm.text.IniRealm.onInit(IniRealm.java:165) ~[shiro-core-1.4.0.jar:1.4.0]
at org.apache.shiro.realm.AuthenticatingRealm.init(AuthenticatingRealm.java:398) ~[shiro-core-1.4.0.jar:1.4.0]
at org.apache.shiro.spring.LifecycleBeanPostProcessor.postProcessBeforeInitialization(LifecycleBeanPostProcessor.java:89) ~[shiro-spring-1.4.0.jar:1.4.0]
... 66 common frames omitted
【问题讨论】:
-
我猜你的 shiro.ini 文件在你的类路径中找不到
-
谢谢布赖恩,我在路径 src/main/resources 中有 shiro.ini 文件
-
它进入你的类路径了吗?你能在
org.apache.shiro.realm.text.IniRealm.onInit(IniRealm.java:165)上设置一个断点,然后在你的cp 周围戳一下吗? -
嗨,布莱恩,很抱歉回复晚了。根据您在 IniRealm 中调试的建议,我在使用 Spring Boot 时完全从类路径中删除了 ini 文件,因此利用 Bean 来配置领域。我观察到如果我们真的不需要ini文件,那么最好从类路径中删除而不是仅仅注释掉ini配置。在这种情况下,在 ini 文件中注释配置会引发异常 Ini 实例和/或 resourcePath 导致空或空 Ini 配置。
标签: java apache spring-boot shiro