ITeye博客

Apache Shiro 使用手册(五)Shiro 配置说明

kdboy · 2011-09-09 22:23 · 开发 · 开源软件 · Markdown

Apache Shiro的配置主要分为四部分:

对象和属性的定义与配置 URL的过滤器配置 静态用户配置 静态角色配置 其中,由于用户、角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包含前两项的配置。

Apache Shiro的大多数组件是基于POJO的,因此我们可以使用POJO兼容的任何配置机制进行配置,例如:Java代码、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式为例,并且对其中的一些配置参数进行一些简单说明。

Shiro对象的配置: 主要是对Shiro各个组件的实现进行定义配置,主要组件在前文已做过简单介绍,这里不再一一说明。

<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="sessionMode" value="native"/>
        <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
        <property name="realm" ref="myRealm"/>
        <property name="sessionManager" ref="sessionManager"/> 
</bean>

Shiro过滤器的配置 Shiro主要是通过URL过滤来进行安全管理,这里的配置便是指定具体授权规则定义。

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/login.jsp"/>
    <property name="successUrl" value="/home.jsp"/>
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
    <property name="filterChainDefinitions">
        <value>
            # some example chain definitions:
            /admin/** = authc, roles[admin]
            /docs/** = authc, perms[document:read]
            /** = authc
            # more URL-to-FilterChain definitions here
        </value>
    </property>
</bean>

URL过滤器配置说明: Shiro可以通过配置文件实现基于URL的授权验证。FilterChain定义格式: URL_Ant_Path_Expression = Path_Specific_Filter_Chain 每个URL配置,表示匹配该URL的应用程序请求将由对应的过滤器进行验证。 例如:

/index.html = anon /user/create = anon /user/** = authc /admin/** = authc, roles /rest/** = authc, rest /remoting/rpc/** = authc, perms

URL表达式说明 1、URL目录是基于HttpServletRequest.getContextPath()此目录设置 2、URL可使用通配符,**代表任意子目录 3、Shiro验证URL时,URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序,尤其在使用通配符时。

Filter Chain定义说明 1、一个URL可以配置多个Filter,使用逗号分隔 2、当设置多个过滤器时,全部验证通过,才视为通过 3、部分过滤器可指定参数,如perms,roles

Shiro内置的FilterChain

|Filter Name|Class| |anon|org.apache.shiro.web.filter.authc.AnonymousFilter| |authc|org.apache.shiro.web.filter.authc.FormAuthenticationFilter| |authcBasic|org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter| |perms|org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter| |port|org.apache.shiro.web.filter.authz.PortFilter| |rest|org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter| |roles|org.apache.shiro.web.filter.authz.RolesAuthorizationFilter| |ssl|org.apache.shiro.web.filter.authz.SslFilter| |user|org.apache.shiro.web.filter.authc.UserFilter|

评论(15)

  1. #15 楼 · sp42 · 2016-01-23 19:54

    非常不错,谢谢 :idea:

  2. #14 楼 · kongbinheng · 2015-06-23 15:52

    非常好,请问能给转发一份整合好的ssi和shiro吗?最好里面还有shiro的Demo(QQ:355815741),谢谢!

  3. #13 楼 · coffeemvpcom · 2013-10-23 13:17

    it158 writes:

    shrio 的ssl 你配置过没?为什么我声明了ssl。代码是 /user/** =ssl, authc 但是跳转到/user/** 的时候直接504了

    请问能给转发一份整合好的SSI和shiro吗?最好里面还有shiro的Demo(QQ:308056797)

  4. #12 楼 · zhangzi · 2013-08-23 15:56

    有具体demo实例吗?

  5. #11 楼 · brightfox · 2013-07-18 14:01

    不知楼主有没有尝试过在开源的jeecms中应用shiro

  6. #10 楼 · gbanana · 2013-07-03 21:45

    讲的非常详细,受益匪浅,非常感谢。

  7. #9 楼 · jayyunfei · 2013-07-03 11:05

    其实我想问楼主=号后面的参数都是什么意思,能解答一下吗?

  8. #8 楼 · elan1986 · 2013-07-03 09:56

    mark!

  9. #7 楼 · it158 · 2013-05-02 16:42

    shrio 的ssl 你配置过没?为什么我声明了ssl。代码是 /user/** =ssl, authc 但是跳转到/user/** 的时候直接504了

  10. #6 楼 · kdboy · 2012-08-19 07:41

    可以参考springside中的mini-web项目: https://github.com/springside/springside4

  11. #5 楼 · itsnothing · 2012-08-16 15:16

    同求demo啊. 楼主,能发一下到我邮箱么. 谢谢了 . 136356123@qq.com

  12. #4 楼 · llfc · 2012-05-07 14:59

    楼主,能把demo给我(315108928@qq.com)发下吗?谢谢了!

  13. #3 楼 · alan2012 · 2012-04-16 10:38

    楼主,如果能把demo发给我0858.178@163.com,太是感谢了!

  14. #2 楼 · nothing0318 · 2012-02-23 01:32

    同求demo nothing890318@gmail.com 谢谢!

  15. #1 楼 · binghejinjun · 2011-09-14 16:55

    楼主,你能贴出登录界面的代码吗,或者给提供个demo(359387306@qq.com),登录页面提交给谁?请求怎样到达自定义realm的认证方法的?困扰了很久了,不胜感激!

评论功能维护中,当前仅展示历史评论。