springboot集成shiro(Ehcache和Redis缓存)

 引入shiro整合Ehcache依赖



 <!-- shiro整合ehcache -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.3.2</version>
       </dependency>

shiro中结合Ehcache缓存

Ehcache已经帮我们自动实现了shiro的CacheManager接口

//权限缓存到内存
    @Bean(name = "shiroRealm")
    public Realm getRealm() {
        MyShiroRealm myShiroRealm = new MyShiroRealm();

//修改凭证匹配器
        HashedCredentialsMatcher hashedCredentialsMatcher =new HashedCredentialsMatcher();  
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");//散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashIterations(1024);//散列的次数                     
        myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher);

//开启缓存管理
        myShiroRealm.setCacheManager(new EhCacheManager());
        myShiroRealm.setCachingEnabled(true);//开启全局缓存
        myShiroRealm.setAuthenticationCachingEnabled(true);//开启认证缓存
        myShiroRealm.setAuthenticationCacheName("authenticationCache");//设置认证缓存的名字
        myShiroRealm.setAuthorizationCachingEnabled(true);//开启授权缓存
        myShiroRealm.setAuthorizationCacheName("authorizationCache");//设置授权缓存的名字
       
        return myShiroRealm;
    }

shiro中结合Ehcache缓存 

//权限缓存到内存
    @Bean(name = "shiroRealm")
    public Realm getRealm() {
        MyShiroRealm myShiroRealm = new MyShiroRealm();

//修改凭证匹配器
        HashedCredentialsMatcher hashedCredentialsMatcher =new HashedCredentialsMatcher();  
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");//散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashIterations(1024);//散列的次数                     
        myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher);

//开启缓存管理
        myShiroRealm.setCacheManager(new RedisCacheManager());
        myShiroRealm.setCachingEnabled(true);//开启全局缓存
        myShiroRealm.setAuthenticationCachingEnabled(true);//开启认证缓存
        myShiroRealm.setAuthenticationCacheName("authenticationCache");//设置认证缓存的名字
        myShiroRealm.setAuthorizationCachingEnabled(true);//开启授权缓存
        myShiroRealm.setAuthorizationCacheName("authorizationCache");//设置授权缓存的名字
       
        return myShiroRealm;
    }

 

实现方式一:shiro中结合redis缓存:用普通redisTemplate.opsForValue().set()或.get()方法实现

1.新建一个自定义的RedisCacheManager

package cn.shiro.web;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
//自定义shiro缓存管理器
public class RedisCacheManager implements CacheManager {
    //参数:为认证缓存或授权缓存的名字
    @Override
    public <K, V> Cache<K, V> getCache(String cacheName) throws CacheException {
        System.out.println(cacheName);
        return new RedisCache<K, V>();
    }
}

2. 自定义一个类叫RedisCache

在.properties文件中配置了redis的基本属性,就会在容器中自动生成一个redisTemplate 的bean

【下面这个类的方法具体实现是用的redisTemplate.opsForValue().set()或.get()方法】

package cn.shiro.web;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import java.util.Collection;
import java.util.Set;
//自定义redis缓存的实现
public class RedisCache<k,v> implements Cache<k,v> {
    @Override
    public v get(k key) throws CacheException {
        System.out.println("get key:"+key);
        RedisTemplate redisTemplate=ApplicationContextUtils.getBean("redisTemplate");
        return (v)redisTemplate.opsForValue().get(key.toString());
    }

    @Override
    public v put(k key, v value) throws CacheException {
        System.out.println("put key:"+key);
        System.out.println("put value:"+value);
        //ApplicationContextUtils工具是自己用applicationContextAwere实现的,来获取容器中某一个bean
        RedisTemplate redisTemplate=ApplicationContextUtils.getBean("redisTemplate");
        //key是字符串,value才是对象,所有需要进行对象的序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set(key.toString(),value);
        return null;
    }

    @Override
    public v remove(k key) throws CacheException {
        return null;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<k> keys() {
        return null;
    }

    @Override
    public Collection<v> values() {
        return null;
    }
}

工具类:ApplicationContextUtils 获取spring容器中的bean

package cn.shiro.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtils  implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(ApplicationContextUtils.applicationContext == null) {
            ApplicationContextUtils.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

实现方式二:shiro中结合redis缓存:用普通redisTemplate.opsForValue().set()或.get()方法实现

package cn.shiro.web;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
//自定义shiro缓存管理器
public class RedisCacheManager implements CacheManager {
    //参数:为认证缓存或授权缓存的名字
    @Override
    public <K, V> Cache<K, V> getCache(String cacheName) throws CacheException {
        System.out.println(cacheName);
        //给自定义RedisCache传入一个cacheName,方便RedisCache的方法用redisTemplate.opsForHash.put()或.opsForHash.get()方法来向缓存存值.
        return new RedisCache<K, V>(cacheName);
    }
}

 

package cn.shiro.web;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import java.util.Collection;
import java.util.Set;
//自定义redis缓存的实现
public class RedisCache<k,v> implements Cache<k,v> {
    
    private String cacheName;
    
    public RedisCache(){
    }
    public RedisCache(String cacheName){
        this.cacheName=cacheName;
    }
    @Override
    public v get(k key) throws CacheException {
        System.out.println("get key:"+key);
        return (v)getRedisTemplate().opsForHash().get(this.cacheName,key.toString());
    }

    @Override
    public v put(k key, v value) throws CacheException {
        System.out.println("put key:"+key);
        System.out.println("put value:"+value);
        getRedisTemplate().opsForHash().put(this.cacheName,key.toString(),value);
        return null;
    }

    @Override
    public v remove(k key) throws CacheException {
        return null;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<k> keys() {
        return null;
    }

    @Override
    public Collection<v> values() {
        return null;
    }
    //可以把获取redisTemplate的方法进行封装
    private RedisTemplate getRedisTemplate(){
        //ApplicationContextUtils是自己用applicationContextAwere实现的,来获取容器中某一个bean
        RedisTemplate redisTemplate=ApplicationContextUtils.getBean("redisTemplate");

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值