【问题标题】:How to Configure shiro.ini for custom realm in spring-boot app?如何在 spring-boot 应用程序中为自定义领域配置 shiro.ini?
【发布时间】: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


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2019-08-25
    • 2018-01-18
    • 2016-05-09
    • 1970-01-01
    • 2017-07-04
    • 2016-08-18
    相关资源
    最近更新 更多