shiro缓存管理及自动登陆的实现
一.shiro缓存管理
涉及到的shiro组建 cacheManager 和 cache
redis实现cacheManager的管理
思路 重写cache 和cachemanager cache中将cache的增删改放在redis中进行操作
将cache注入到cacheManager中 在xml中配置 cacheManager
cache (JedisUtils再上一章中已经有啦)
package com.zhuoshi.cache; import com.zhuoshi.utils.JedisUtil; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.stereotype.Component; import org.springframework.util.SerializationUtils; import java.util.Collection; import java.util.Set; import static org.springframework.util.SerializationUtils.serialize; /** * Created by Alex on 2018/6/8. */ @Component public class RedisCache<K,V> implements Cache<K,V> { @Autowired private JedisUtil jedisUtil; private final String cache_prefix = "shiro_cache"; /** * * @param key * @return hasPrefix的key */ private byte[] getKey(K k) { if(k instanceof String) { return (cache_prefix+(String)k).getBytes(); } return SerializationUtils.serialize(k); } @Override public V get(K k) throws CacheException { System.out.println("从redis获取权限数据"); byte[] value = jedisUtil.get(getKey(k)); if (value != null) { return (V) SerializationUtils.deserialize(value); } return null; } @Override public V put(K k, V v) throws CacheException { byte[] key = getKey(k); byte[] value = SerializationUtils.serialize(v); jedisUtil.set(key, value); jedisUtil.expire(key, 600); return v; } @Override public V remove(K k) throws CacheException { byte[] key = getKey(k); byte[] value = jedisUtil.get(key); jedisUtil.del(key); if (value != null) { return (V) SerializationUtils.deserialize(value); } 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; } }
cacheManager
package com.zhuoshi.cache; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.cache.CacheManager; import org.springframework.beans.factory.annotation.Autowired; /** * Created by Alex on 2018/6/8. */ public class RedisCacheManager implements CacheManager { @Autowired private RedisCache redisCache; //要求返回cache实例 @Override public <K, V> Cache<K, V> getCache(String s) throws CacheException { return redisCache; } }
xml 配置
<!--redisCacheManager 对象--> <bean class="com.zhuoshi.cache.RedisCacheManager" name="cacheManager"/>
<!--创建securitymanager对象--> <bean name="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="customRealm"/> <property name="sessionManager" ref="defaultSessionManager"/> <property name="cacheManager" ref="cacheManager"/> </bean>
二.shiro自动登陆的功能(记住我)
<bean id="cookieRememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager"> <property name="cookie" ref="cookie" /> </bean> <bean id="cookie" class="org.apache.shiro.web.servlet.SimpleCookie"> <constructor-arg value="rememberMe" /> <!--设置cookie最长存活时间--> <property name="maxAge" value="20000000" /> </bean> <!--创建securitymanager对象--> <bean name="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="customRealm"/> <property name="sessionManager" ref="defaultSessionManager"/> <property name="cacheManager" ref="cacheManager"/> <property name="rememberMeManager" ref="cookieRememberMeManager"/> </bean>
token.setRememberMe(user.isRememberMe());
2018-06-08