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

 2023-09-19 阅读 27 评论 0

摘要:Apache Shiro的配置主要分为四部分:对象和属性的定义与配置URL的过滤器配置静态用户配置静态角色配置其中,由于用户、角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包含前两项的配置。Apache Shiro的大多数组件是基于POJO的,因此我们可以
Apache Shiro的配置主要分为四部分: 
  • 对象和属性的定义与配置
  • URL的过滤器配置
  • 静态用户配置
  • 静态角色配置
其中,由于用户、角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包含前两项的配置。 

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

Shiro对象的配置: 
主要是对Shiro各个组件的实现进行定义配置,主要组件在前文已做过简单介绍,这里不再一一说明。 
Xml代码  收藏代码
  1. <bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">  
  2.         <property name="cacheManager" ref="cacheManager"/>  
  3.         <property name="sessionMode" value="native"/>  
  4.         <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->  
  5.         <property name="realm" ref="myRealm"/>  
  6.         <property name="sessionManager" ref="sessionManager"/>   
  7. </bean>  


Shiro过滤器的配置 
Shiro主要是通过URL过滤来进行安全管理,这里的配置便是指定具体授权规则定义。 
Xml代码  收藏代码
  1. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  2.     <property name="securityManager" ref="securityManager"/>  
  3.     <property name="loginUrl" value="/login.jsp"/>  
  4.     <property name="successUrl" value="/home.jsp"/>  
  5.     <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->  
  6.     <property name="filterChainDefinitions">  
  7.         <value>  
  8.             # some example chain definitions:  
  9.             /admin/** = authc, roles[admin]  
  10.             /docs/** = authc, perms[document:read]  
  11.             /** = authc  
  12.             # more URL-to-FilterChain definitions here  
  13.         </value>  
  14.     </property>  
  15. </bean>  

URL过滤器配置说明: 
Shiro可以通过配置文件实现基于URL的授权验证。centos7 hbase安装,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

来源: http://kdboy.iteye.com/blog/1169637


来自为知笔记(Wiz)


转载于:https://www.cnblogs.com/jeffen/p/6185607.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/79564.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息