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>
<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>
<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 |
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中文版使用手册!
Apache Shiro使用手册 共22页.pdf
Apache_Shiro_使用手册Apache_Shiro_使用手册Apache_Shiro_使用手册
Apache Shiro使用手册.pdf 学习shiro 入门之用
Apache_Shiro_使用手册(一)Shiro架构介绍
Apache_Shiro参考手册中文版 Introduction to Apache Shiro What is Apache Shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密。 Apache Shiro 的首要目标...
Apache Shiro 中文手册,想了解权限方面知识的小伙伴们下载吧。
Shiro核心组件Aplication,Subject,SecurityManager, Realm, Authenticator, Authorizer. 介绍了Shiro认证,授权的实现及其机制原理。给了具体操作步骤及代码。
Apache Shiro的大多数组件是基于POJO的,因此我们可以使用POJO兼容的任何配置机制进行配置,例如:Java代码、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式为例,并且对其中的一些配置参数进行...
Apache Shiro 开发使用手册,介绍了shiro的架构,认证,授权,实现,配置等,希望能够为大家使用shiro的时候带来帮助。
Apache_Shiro参考手册中文版,官方原版,pdf格式,方便查看,可复制!
ApacheShiro使用手册和Apache Shiro Reference Documentation 该文件下载了别人上传的两个文件,一个10分,一个30分,为了自己以后下载不花那么多分,也不让一些没有多少分的朋友能够下载,所以将别人资源下载后,将...
apache-shiro中文开发技术手册,很实用
shiro文档 java相关项目开发短信功能使用的jar包
取代spring-security的东西,
apache-shiro-reference《Apache Shiro 参考手册》 Chinese translation of and the other article collection. The laset version of Apache Shiro is 1.5.x. You can also see the demos of the reference at ....
Apache-Shiro-使用手册 Apache Shiro 是一个框架,可用于身份验证和授权。本文提供了几个示例用来展示如何在 Java™ 应用程序中使用 Shiro 并给出了如何在一个 Grails web 应用程序中使用它的概述。
Apache_Shiro参考手册中文版-converted
Apache_Shiro_使用手册(四)Realm_实现