Как настроить shiro.ini для пользовательского царства в приложении весенней загрузки? - PullRequest
0 голосов
/ 26 февраля 2019

У меня есть пользовательская область

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;
}

При использовании вышеуказанного компонента получается следующее исключение:

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

1 Ответ

0 голосов
/ 05 марта 2019
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
...