使用ini文件的Spring MVC和Shiro配置


问题内容

我正在尝试使用Spring MVC和Apache Shiro建立环境。我正在关注shiro.apache.org中提到的文章。

我正在使用Spring的DelegatingFilterProxy作为web.xml中的Shiro过滤器。

当前过滤使用:

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login"/>
        <property name="successUrl" value="/dashboard"/>
        <property name="unauthorizedUrl" value="/unauthorized"/>
        <property name="filterChainDefinitions">
            <value>
                /** = authc, user, admin
                /admin/** = authc, admin
                /login = anon
            </value>
        </property>
    </bean>

问题是,如何使用shiro.ini文件定义安全设置?


问题答案:

您不需要使用shiro.ini。其余所有配置都可以(而且应该使用,因为您正在使用ShiroFilterFactoryBean)在Spring中完成。

例如,向您的shiroFilter添加一个securityManager和基于ehCache的缓存管理器:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myRealm"/>
    <property name="sessionMode" value="native"/>
    <property name="sessionManager" ref="sessionManager"/>
    <property name="cacheManager" ref="cacheManager"/>
</bean>

<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManager" ref="ehCacheManager"/>
</bean>

<bean id="ehCacheManager" 
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

<bean id="sessionDAO" 
    class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"/>

<bean id="sessionManager"
    class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    <property name="sessionDAO" ref="sessionDAO"/>
</bean>

<bean id="myRealm" class="com.foo.MyRealm"/>