散列密码
1.在保存用户时,给用户密码进行加密处理
public void saveEmployee(Employee employee) {
// 把密码进行加密
Md5Hash md5Hash = new Md5Hash(employee.getPassword(),employee.getUsername(),2);
employee.setPassword(md5Hash.toString());
// 保存员工
employeeMapper.insert(employee);
// 保存员工与角色的关系
for (Role role : employee.getRoles()) {
employeeMapper.insertEmployeeAndRoleRel(employee.getId(),role.getRid());
}
}
2.在认证当中添加密码处理
3.在配置文件中添加凭证匹配器
<!-- 凭证匹配器 -->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 散列算法 -->
<property name="hashAlgorithmName" value="md5"/>
<!-- 散列次数 -->
<property name="hashIterations" value="2"></property>
</bean>
权限缓存
进入页面时, 页面当中写了Shiro的标签,每一个标签都要去到授权当中进行一次授权查询
授权查询只使用一次即可, 所以使用缓存,把对应的内容缓存起来,下次再去, 直接从缓存当中进行查询
1.添加缓存pom依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.2</version>
</dependency>
2.添加shiro缓存配置
①添加shiro-ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
②配置约束
3.配置缓存管理器
<!-- 缓存管理器 -->
<bean id="ehCache" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>
4.把缓存管理器添加到安全管理器当中
<!-- 配置shiro安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入realm -->
<property name="realm" ref="employeeRealm"></property>
<!-- 注入缓存器 -->
<property name="cacheManager" ref="ehCache"/>
</bean>