上一节我们介绍了shiro的相关概念,本节结合ssm架构的系统进行搭建。配置项详解参考:配置项详解
目录
4、applicationContext-shiro.xml
1、Pom.xml
<!--Apache Shiro所需的jar包 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
2、Web.xml
<!-- shiro配置 开始 -->
<!-- 配置Shiro过滤器,先让Shiro过滤系统接收到的请求 -->
<!-- 这里filter-name必须对应applicationContext.xml中定义的<bean id="shiroFilter"/> -->
<!-- 使用[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 -->
<!-- 通常会将此filter-mapping放置到最前面(即其他filter-mapping前面),以保证它是过滤器链中第一个起作用的 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- shiro配置 结束 -->
需要放在所有filter中靠前的位置,比如需要放在siteMesh的过滤器之前。
org.springframework.web.filter.DelegatingFilterProxy表示这是一个代理filter,它会将实际的工作,交给spring配置文件中 id="shiroFilter" 的bean来处理。
3、ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="shirocache">
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
maxElementsOnDisk="0"
eternal="false"
overflowToDisk="true"
diskPersistent="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
diskSpoolBufferSizeMB="50"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LFU"
/>
<!-- 登录记录缓存 锁定10分钟 -->
<cache name="passwordRetryCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
</ehcache>
配置项解读
默认的策略:
<!-- 硬盘缓存的临时路径 -->
<diskStore path="java.io.tmpdir"/>
<!-- 默认的缓存区域的缓存策略
maxElementsInMemory:内存中最大容纳的元素数量
eternal:对象是否永生,默认是false
overflowToDisk:缓存存储的数据达到maxInMemory限制时是否overflow到磁盘上,默认为false
diskPersistent:设定在虚拟机重启时是否进行磁盘存储,默认为false
timeToLiveSeconds:活多久就死掉。
timeToIdleSeconds:发呆不用超过多长时间,over死掉
maxElementsOnDisk:硬盘上能存放多少元素
diskExpiryThreadIntervalSeconds:轮询的时间,检查的时间。
memoryStoreEvictionPolicy:如果缓存满了怎么办?LRU,LFU,FIFO
persistence strategy:如果内存满了,溢出到硬盘
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
加深记忆
timeToLiveSeconds : 缓存自创建之时起至失效时的间隔时间单位为秒,默认为0,代表无限长,即缓存永不过期;
timeToIdleSeconds : 缓存创建以后,最后一次访问缓存之时至失效之时的时间间隔,单位为秒,默认为0,永不过期;
eternal : 缓存是否永久有效(true/false)
当你配置了eternal属性为true时,如果同时配置timeToLiveSeconds/timeToIdleSeconds不为0,则程序就会报以上警告
下面说说他们之间的关系:
eternal不多说,true表示缓存永久有效,false表示不为永久有效
主要是timeToLiveSeconds 和timeToIdleSeconds 之间的使用(单独配置时,以上已说明)
举例说明:timeToLiveSeconds =3600 timeToIdleSeconds =300
以上配置代表缓存有效时间为3600秒(自缓存建立起一个小时有效 ),在有效的一个小时内,如果连续五分钟未访问缓存,
则缓存失效,特别说明的是,就算缓存访问从未间断,到一个小时后,缓存也会失效
4、applicationContext-shiro.xml
4.1 配置流程图
自己结合cas系统画的配置流程图,如有错误请留言。
4.2 详细配置内容
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<description>Shiro安全配置</description>
<!-- 缓存管理 <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean> -->
<!-- 缓存管理器 使用Ehcache实现 -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml" />
</bean>
<!-- 凭证匹配器 -->
<!-- 功能就是用来匹配用户登录使用的令牌和数据库中保存的用户信息是否匹配 -->
<bean id="credentialsMatcher"
class="com.comqing.common.shiro.RetryLimitHashedCredentialsMatcher">
<!--constructor-arg :表示通过构造函数引入 -->
<constructor-arg ref="shiroCacheManager" />
<property name="hashAlgorithmName" value="md5" />
<property name="hashIterations" value="2" />
<property name="storedCredentialsHexEncoded" value="true" />
</bean>
<!-- Realm实现 -->
<bean id="userRealm" class="com.comqing.common.shiro.UserRealm">
<property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>
<!-- 会话管理器 -->
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- session的失效时长,单位毫秒 -->
<property name="globalSessionTimeout" value="10000" />
<!-- 删除失效的session -->
<property name="deleteInvalidSessions" value="true" />
</bean>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm" />
<property name="sessionManager" ref="sessionManager" />
<!--将缓存管理器,交给安全管理器 -->
<property name="cacheManager" ref="shiroCacheManager" />
<!-- 记住我 -->
<!--<property name="rememberMeManager" ref="rememberMeManager"/> -->
</bean>
<!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 默认的登陆访问url -->
<property name="loginUrl" value="/" />
<!-- 登陆成功后跳转的url -->
<property name="successUrl" value="/index" />
<!-- 没有权限跳转的url -->
<property name="unauthorizedUrl" value="/" />
<!-- 具体配置需要拦截哪些 URL, 以及访问对应的 URL 时使用 Shiro 的什么 Filter 进行拦截 -->
<property name="filterChainDefinitions">
<value>
/authc/admin = roles[admin]
/user/** = authc
/** = anon
</value>
</property>
</bean>
<!-- 配置 Bean 后置处理器: 会自动的调用和 Spring 整合后各个组件的生命周期方法 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>
4.2.1 EhCacheManager
在spring-context配置文件中,还有一个是需要配置-cacheManager,因为shiro的session是自己实现的,所以我们还需要一个缓存框架,所以在spring的配置文件一定要注意配置哦,用的是ehcache。
<!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml" />
</bean>
4.2.2 SecurityManager
我们知道securityManager是shiro的顶层对象,它管理和调用其它所有子系统,负责系统的安全。
我们知道shiro有两个类型的securityManager:一个是JavaSE环境,默认是DefaultSecurityManager;一个是web环境,默认是DefaultWebSecurityManager。所以我们web环境肯定应该使用后者。我们从顶层对象一层一层向下配置。先看securityManager如何配置:
组件SecurityManager直接继承了SessionManager,且提供了SessionsSecurityManager实现直接把会话管理委托给相应的SessionManager,DefaultSecurityManager及DefaultWebSecurityManager默认SecurityManager都继承了SessionsSecurityManager。
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm"/>
<property name="cacheManager" ref="cacheManager"/>
<property name="rememberMeManager" ref="rememberMeManager"/>
</bean>
它默认使用的session管理器是 ServletContainerSessionManager,所以上面没有配置,所以就使用默认值。
4.2.3 userRealm
显然 securityManager 最重要的工作就是用户登录认证和获得用户的权限等相关信息,所以 realm 是其最重要的依赖:
<!-- Realm实现 -->
<bean id="userRealm" class="com.comqing.common.utils.UserRealm">
<property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>
UserRealm 继承 AuthorizingRealm 显然是为了获取权限信息,对用户进行访问控制;继承AuthenticatingRealm显然是为了获得用户的认证信息,对用户进行认证。
4.2.4 credentialsMatcher
而 credentialsMatcher 就是 AuthenticatingRealm 使用来进行密码验证的依赖的组件:
<!-- 凭证匹配器 -->
<!-- 功能就是用来匹配用户登录使用的令牌和数据库中保存的用户信息是否匹配 -->
<bean id="credentialsMatcher"
class="com.comqing.common.utils.RetryLimitHashedCredentialsMatcher">
<!--constructor-arg :表示通过构造函数引入 -->
<constructor-arg ref="cacheManager" />
<property name="hashAlgorithmName" value="md5" />
<property name="hashIterations" value="2" />
<property name="storedCredentialsHexEncoded" value="true" />
</bean>
hashAlgorithmName必须的,没有默认值。可以有MD5或者SHA-1,如果对密码安全有更高要求可以用SHA-256或者更高。这里使用MD5
storedCredentialsHexEncoded默认是true,此时用的是密码加密用的是Hex编码;false时用Base64编码
hashIterations迭代次数,默认值是1。
4.2.5 shiroFilter
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 装配 (安全管理器)securityManager -->
<property name="securityManager" ref="securityManager" />
<!-- 配置登陆页面 -->
<property name="loginUrl" value="/" />
<property name="unauthorizedUrl" value="/" />
<!-- 具体配置需要拦截哪些 URL, 以及访问对应的 URL 时使用 Shiro 的什么 Filter 进行拦截 -->
<property name="filterChainDefinitions">
<value>
/authc/admin = roles[admin]
/authc/** = authc
/** = anon
</value>
</property>
</bean>
- <property name="loginUrl" value="/" />
loginUrl:没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。
- <property name="successUrl" value="/index" />
successUrl:登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。
- <property name="unauthorizedUrl" value="/" />
unauthorizedUrl:没有权限默认跳转的页面。
- <property name="filterChainDefinitions">
shiro验证URL时,URL匹配成功便不再继续匹配查找(所以要注意配置文件中的URL顺序,尤其在使用通配符时)
故filterChainDefinitions的配置顺序为自上而下,以最上面的为准。
anon: 例子 /admins/**=anon 没有参数,表示可以匿名使用。
authc: 例子/admins/user/**=authc表示需要认证(登录)才能使用,没有参数
user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查
roles: 例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。
perms:例子/admins/user/**=perms[user:add:*],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。
注:
anon,authcBasic,auchc,user是认证过滤器,
perms,roles,ssl,rest,port是授权过滤器
具体配置:
http://blog.csdn.net/clj198606061111/article/details/24185023
http://blog.csdn.net/userrefister/article/details/47807075