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

时间:2021-04-12 19:12:50 

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的应用程序请求将由对应的过滤器进行验证。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]

URL表达式说明

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

Filter Chain定义说明

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

Shiro内置的FilterChain

Filter NameClass
anonorg.apache.shiro.web.filter.authc.AnonymousFilter
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
portorg.apache.shiro.web.filter.authz.PortFilter
restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter
sslorg.apache.shiro.web.filter.authz.SslFilter
userorg.apache.shiro.web.filter.authc.UserFilter

标签:Apache,Shiro,配置说明
0
投稿

猜你喜欢

  • Windows下Linux安全联机UltraEdit工具

    2010-03-31 18:38:00
  • 没有技术,你也可以很优秀!—我的站长生涯

    2009-01-06 15:12:00
  • DNS 笔记

    2008-01-26 18:39:00
  • 禁止IIS缓存静态文件的方法(png,js,html等)

    2022-07-05 04:16:27
  • Linux中samba服务器的搭建教程

    2023-03-14 01:30:07
  • WordPress 3.2 新亮点

    2011-05-06 12:34:00
  • 避免攻击保证上传FTP服务器的安全

    2009-05-07 09:46:00
  • WordPress设置文章页面的动态关键字和描述

    2011-09-26 20:34:01
  • 隐藏SMTP旗标(Sendmail/Qmail/Postfix/Exim)

    2007-03-27 11:21:00
  • 新云CMS常用模板标签

    2008-09-23 18:19:00
  • 网站推广系列导读:目标群体、载体和策略

    2009-02-14 21:52:00
  • 用Win 2003架设邮件服务器

    2009-01-06 17:24:00
  • 雨林木风115网络U盘 大容量新政策吸引用户

    2009-11-26 13:56:00
  • 用DNS服务器技术检查排除网络故障

    2009-08-27 14:29:00
  • 百度更改网页权重算法 网站如何被顺利收录

    2008-12-04 08:55:00
  • 不登陆QQ 做个html文件查看好友在线状态

    2008-01-19 13:48:00
  • 个人服务器架设全攻略(13)

    2010-04-06 18:43:00
  • Webmail攻防实战(3)

    2007-10-24 13:49:00
  • CentOS VPS安装Kloxo免费虚拟主机管理系统

    2023-11-02 15:15:09
  • 利用virtualbox虚拟机实现端口转发

    2022-06-14 01:20:27
  • asp之家 网站运营 m.aspxhome.com