一.本文主要是本人对使用shiro方面的总结。重点在介绍shiro使用。
二.1.导入所需要的架包
maven导入
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-aspectj</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.shiro的xml配置
spring-context-shiro.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> <!-- Shiro生命周期处理器 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"> </bean> <!--配置权限管理器(核心)--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!--自定义登录权限模块--> <property name="realm" ref="jdbcRealm"> </property> <!-- 使用下面配置的缓存管理器 --> <property name="cacheManager" ref="cacheManager"/> </bean> <!-- 进行缓存的操作配置 --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> </bean> <!-- 安全管理器 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> <!--<!– 缓存管理器 使用Ehcache实现 –>--> <!--<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">--> <!--<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />--> <!--</bean>--> <!-- 凭证匹配器 --> <bean id="credentialsMatcher" class="shiro.RetryLimitHashedCredentialsMatcher"> <constructor-arg ref="cacheManager" /> <property name="hashAlgorithmName" value="md5" /> <!-- md5盐值加密3次 --> <property name="hashIterations" value="3" /> <property name="storedCredentialsHexEncoded" value="true" /> </bean> <!--<!– 自定义的jdbcrealm –>--> <!--<bean id="jdbcRealm" class="shiro.JdbcRealmCustom">--> <!--<!– 凭证匹配器 –>--> <!--<!–<property name="credentialsMatcher" ref="credentialsMatcher" />–>--> <!--<!–<property name="cachingEnabled" value="true" />–>--> <!--<!– 如需要自定义缓存时间放开以下.修改 ehcache.xml –>--> <!--<property name="authenticationCachingEnabled" value="true" />--> <!--<property name="authenticationCacheName" value="authenticationCache" />--> <!--<property name="authorizationCachingEnabled" value="true" />--> <!--<property name="authorizationCacheName" value="authorizationCache" />--> <!--</bean>--> <!-- 使用Shiro自带的JdbcRealm类 指定密码匹配所需要用到的加密对象 指定存储用户、角色、权限许可的数据源及相关查询语句 --> <!--<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">--> <!--<property name="credentialsMatcher">--> <!--<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">--> <!--<!– 加密算法的名称 –>--> <!--<property name="hashAlgorithmName" value="MD5"> </property>--> <!--<!– 配置加密的次数 –>-->