【问题标题】:Spring MVC and Shiro Configuration using ini files使用 ini 文件的 Spring MVC 和 Shiro 配置
【发布时间】:2011-11-18 21:25:54
【问题描述】:

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

我在 web.xml 中使用 Spring 的 DelegatingFilterProxy 作为 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 文件定义安全设置?

【问题讨论】:

    标签: spring-mvc shiro


    【解决方案1】:

    您不需要使用 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"/>
    

    【讨论】:

    • 是的,ericacm 是对的;如果没有其他配置机制可用,INI 是 Shiro 的默认配置格式。由于 Spring 已经提供了非常强大的配置机制,所以 Spring 应用程序应该直接通过 Spring 配置 Shiro。这通常也比 INI 更强大,因为您可以使用 Spring 的 PropertyPlaceholderConfigurer 和其他 Spring 配置便利来使 Shiro 配置更好。
    • 埃里克/莱斯,谢谢。我还在和 Shiro 一起努力,这很棒。我仍然没有得到正确的文档来解释 spring-shiro-jdbcRealm 与示例应用程序的集成。你能帮忙吗?
    • @sourcedelica 我需要添加 FacesAjaxAwareUserFilter。我该怎么做。? [main] user = com.example.filter.FacesAjaxAwareUserFilter user.loginUrl = /login.xhtml [users] admin = password [urls] /login.xhtml = user /app/** = user
    【解决方案2】:

    你可以在这里http://shiro.apache.org/reference.html查看shiro文档,它包含了所有东西,在spring中,正如Les所说,通常定义不同的bean而不是使用shiro.ini文件,但你也可以使用这个文件进行身份验证,使用IniRealm之类的:

    <bean id="myRealm" class="org.apache.shiro.realm.text.IniRealm">
      <property name="resourcePath" value="classpath:/shiro.ini" />
    </bean>
    

    更多详情请参考here

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2019-08-16
      • 2015-09-18
      • 2013-06-08
      • 2014-10-04
      • 2012-03-08
      • 2017-02-21
      • 2014-01-04
      • 1970-01-01
      相关资源
      最近更新 更多