我有自己的自定义领域
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
有人知道需要为春季启动项目配置/注册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
答案 0 :(得分:0)
This is how I made it work, Please suggest if any other better way of doing it.
-Removed existing Bean realm().
-Added authorizer bean to SprinBootApp main class
@Bean
public Authorizer authorizer() {
MyCusotmRealm realm = new MyCusotmRealm();
return realm;
}
- Added shiro filter chain definition(seems crucial).
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chainDefinition = new
DefaultShiroFilterChainDefinition();
chainDefinition.addPathDefinition("/**", "anon");
return chainDefinition;
}
- Removed datasource ds from shiro.ini, as I am leveraging database connection through spring boot application.properties file.
shiro.ini will look like:
jdbcRealm= com.company.security.shiro.realm.MyCustomRealm
securityManager.realms = $jdbcRealm