Shiro - 散列密码与权限缓存

散列密码

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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值