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

黄舟
发布: 2017-01-18 09:34:15
原创
2003人浏览过

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

博客分类: 

开发

安全框架shiro 

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 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    

智写助手
智写助手

智写助手 写得更快,更聪明

智写助手 12
查看详情 智写助手

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    

以上就是Apache Shiro 使用手册(五)Shiro 配置说明的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号