shiro使用redis作为缓存

应用场景:Shiro的为每个用户的角色和权限信息提供缓存支持,通过Shiro自己定义的CacheManager实现,默认实现有Ehcache和内存(就是一个Map结构),在应用中通常使用redis作为缓存服务器,因此使用redis来作为shiro的缓存。
优缺点:一般来说缓存放在本地,通过本地内存进行缓存速度更快,但是在分布式环境下,修改了用户权限需要经行协同。将缓存和Session放在统一的数据库里面可以避免,但是通过网络请求延迟较高,且容易造成瞬间高并发。
代码分析:
  1. 实现AbstractCacheManager:
package com.example.demo.shiro;

import org.apache.shiro.cache.AbstractCacheManager;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: demo
 * @description:
 * @author: C_xf
 * @create: 2018-11-25 18:38
 **/
@Component
public class MyCacheManager extends AbstractCacheManager {
    @Autowired
    private MCahce mCahce;
    @Override
    protected Cache createCache(String name) throws CacheException {
        return mCahce;
    }
}

  1. 实现Shiro定义的Cache接口
package com.example.demo.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;

/**
 * @program: demo
 * @description: 通过RedisTemplate的hash操作实现缓存接口
 * @author: C_xf
 * @create: 2018-11-25 18:39
 **/
@Slf4j
@Component
public class MCahce implements Cache {
    //redis key
    private final K prefix = (K)"shiro_cache";

    @Autowired
    private RedisTemplate redisTemplate;

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    //------------------------
    @Override
    public V get(K key) throws CacheException {
        Object o = redisTemplate.opsForHash().get(prefix,key);
        return o == null ? null : (V) o;
    }

    @Override
    public V put(K key, V value) throws CacheException {
        V old = this.get(key);
        redisTemplate.opsForHash().put(prefix,key,value);
        return old;
    }

    @Override
    public V remove(K key) throws CacheException {
        V old = this.get(key);
        redisTemplate.opsForHash().delete(prefix,key);
        return old;
    }

    @Override
    public void clear() throws CacheException {
        redisTemplate.delete(prefix);
    }

    @Override
    public int size() {
        return  redisTemplate.opsForHash().size(prefix).intValue();
    }

    @Override
    public Set keys() {
        return redisTemplate.opsForHash().keys(prefix);
    }

    @Override
    public Collection values() {
        return redisTemplate.opsForHash().values(prefix);
    }
}

  1. 配置shiro注入缓存管理器:
@bean
public AuthorizingRealm myrealm(CacheManager cacheManager)
    {
        log.info("myrealm()");
        AuthorizingRealm realm = new MyRealm();
        realm.setCachingEnabled(true);  //开启缓存
        realm.setCacheManager(cacheManager); //注入缓存管理器
        return realm;
    }
  1. Redis序列化

    1. jdk序列化
    new  JdkSerializationRedisSerializer()
    
    1. json序列化
    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
    

你可能感兴趣的:(shiro)