使用 ini 文件进行 Spring MVC 和 Shiro 配置

2024-02-13

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

我在 web.xml 中使用 Spring 的 DelegatingFilterProxy 作为 Shiro Filter。

当前的过滤是使用以下命令完成的:

<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 中完成。

例如,将基于 securityManager 和 ehCache 的缓存管理器添加到 shiroFilter 中:

<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"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 ini 文件进行 Spring MVC 和 Shiro 配置 的相关文章

随机推荐