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

Apache Shiro的配置主要分为四部分:
[list]
[*]对象和属性的定义与配置
[*]URL的过滤器配置
[*]静态用户配置
[*]静态角色配置
[/list]其中,由于用户、角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包含前两项的配置。

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

[b][color=darkblue]Shiro对象的配置:[/color][/b]
主要是对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>


[b][color=darkblue]Shiro过滤器的配置[/color][/b]
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>

[color=darkblue][b]URL过滤器配置说明:[/b][/color]
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"]

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

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

[color=darkblue][b]Shiro内置的FilterChain[/b][/color]
[table]
|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|
[/table]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值