Apache Shiro的配置主要分为四部分:
html
对象和属性的定义与配置web
URL的过滤器配置spring
静态用户配置apache
静态角色配置数组
其中,因为用户、角色通常由后台进行操做的动态数据,所以Shiro配置通常仅包含前两项的配置。安全
Apache Shiro的大多数组件是基于POJO的,所以咱们可使用POJO兼容的任何配置机制进行配置,例如:Java代码、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式为例,而且对其中的一些配置参数进行一些简单说明。 session
Shiro对象的配置:
主要是对Shiro各个组件的实现进行定义配置,主要组件在前文已作过简单介绍,这里再也不一一说明。 app
<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过滤来进行安全管理,这里的配置即是指定具体受权规则定义。jsp
<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的应用程序请求将由对应的过滤器进行验证。
例如: url
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表达式说明
一、URL目录是基于HttpServletRequest.getContextPath()此目录设置
二、URL可以使用通配符,**表明任意子目录
三、Shiro验证URL时,URL匹配成功便再也不继续匹配查找。因此要注意配置文件中的URL顺序,尤为在使用通配符时。
Filter Chain定义说明
一、一个URL能够配置多个Filter,使用逗号分隔
二、当设置多个过滤器时,所有验证经过,才视为经过
三、部分过滤器可指定参数,如perms,roles
Shiro内置的FilterChain