SecurityManager
对应 shiro来说 SecurityManager 非常重要,这里配置了
Realm
CacheManager
RememberMeManager
sessionManager
可以说是shiro的核心
我们今天就是要 配置 sessionManager 和 CacheManager
让 ecache和redis来缓存 session和 AuthorizationInfo的数据信息
使得应用能集群部署,多个机器之间共享session和 认证授权数据信息。
SecurityManager的配置
//配置核心安全事务管理器
@Bean
public SecurityManager securityManager(@Qualifier("authRealm")AuthRealm authRealm,@Qualifier("redisCacheManager")CacheManager
cacheManager) {
logger.info("--------------shiro已经加载----------------");
DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
// 设置realm.
manager.setRealm(authRealm);
//注入缓存管理器;
//注意:开发时请先关闭,如不关闭热启动会报错
manager.setCacheManager(cacheManager);//这个如果执行多次,也是同样的一个对象;
//注入记住我管理器;
manager.setRememberMeManager(rememberMeManager());
return manager;
}
这里配置了
manager.setCacheManager(cacheManager)
CacheConfig 配置 ehCacheManager 和redisCacheManager
@Configuration
@EnableCaching
public class CacheConfig {
private Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
@Bean(name = "ehCacheManager")
public EhCacheManager ehCacheManager() {
logger.info("--------------ehCacheManager init---------------");
EhCacheManager cacheManager = new EhCacheManager();
cacheManager.setCacheManagerConfigFile("classpath:cache/ehcache-shiro.xml");
logger.info("--------------ehCacheManager init---------------"+cacheManager);
return cacheManager;
}
@Autowired
private RedisTemplate redisTemplate;
@Bean(name = "redisCacheManager")
@Primary
public RedisCacheManager redisCacheManager() {
logger.info("--------------redis cache init---------------");
RedisCacheManager cacheManager = new RedisCacheManager();
cacheManager.setRedisTemplate(redisTemplate);
logger.info("--------------redis cache ---------------"+cacheManager);
return cacheManager;
}
}
使用 ecache做缓存
1 、那么 加入依赖
org.ehcache
ehcache
${ehcache.version}
provided
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache-core
${ehcache.core.version}
org.apache.shiro
shiro-ehcache
${shiro.version}
2、配置 cacheManager
@Bean(name = "ehCacheManager")
public EhCacheManager ehCacheManager() {
logger.info("--------------ehCacheManager init---------------");
EhCacheManager cacheManager = new EhCacheManager();
cacheManager.setCacheManagerConfigFile("classpath:cache/ehcache-shiro.xml");
logger.info("--------------ehCacheManager init---------------"+cacheManager);
return cacheManager;
}
3、配置 ehcache-shiro.xml
4、SecurityManager 注入 ehCacheManager
@Qualifier("ehCacheManager")CacheManager
使用redis做缓存 auth数据
使用redis比较麻烦一些,没有用
org.crazycake
shiro-redis
2.4.6
这样的 jar 只能自己来弄一个 cache 和 cacheManager
当然 也可以使用 shiro-redis 这样就不需要自己实现 cache 和 cacheManager
我们先看自己实现的方式
1、实现Cache
public class RedisCache implements Cache {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static final String REDIS_SHIRO_CACHE = "slife-shiro-cache:";
private String cacheKey;
private RedisTemplate redisTemplate;
private long globExpire = 30;
@SuppressWarnings("rawtypes")
public RedisCache(String name, RedisTemplate client) {
this.cacheKey = REDIS_SHIRO_CACHE + name + ":";
this.redisTemplate = client;
}
@Override
public V get(K key) throws CacheException {
redisTemplate.boundValueOps(getCacheKey(key)).expire(globExpire, TimeUnit.MINUTES);
return redisTemplate.boundValueOps(getCacheKey(key)).get();
}
@Override
public V put(K key, V value) throws CacheException {
V old = get(key);
redisTemplate.boundValueOps(getCacheKey(key)).set(value);
return old;
}
@Override
public V remove(K key) throws CacheException {
V old = get(key);
redisTemplate.delete(getCacheKey(key));
return old;
}
@Override
public void clear() throws CacheException {
redisTemplate.delete(keys());
}
@Override
public int size() {
return keys().size();
}
@Override
public Set keys() {
return redisTemplate.keys(getCacheKey("*"));
}
@Override
public Collection values() {
Set set = keys();
List list = new ArrayList<>();
for (K s : set) {
list.add(get(s));
}
return list;
}
private K getCacheKey(Object k) {
return (K) (this.cacheKey + k);
}
}
2、实现 CacheManager
public class RedisCacheManager implements CacheManager {
private static final Logger logger = LoggerFactory.getLogger(RedisCacheManager.class);
private RedisTemplate redisTemplate;
@Override
public Cache getCache(String name) throws CacheException {
return new RedisCache(name, redisTemplate);
}
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
3、注入RedisCacheManager 到容器
@Autowired
private RedisTemplate redisTemplate;
@Bean(name = "redisCacheManager")
@Primary
public RedisCacheManager redisCacheManager() {
logger.info("--------------redis cache init---------------");
RedisCacheManager cacheManager = new RedisCacheManager();
cacheManager.setRedisTemplate(redisTemplate);
logger.info("--------------redis cache ---------------"+cacheManager);
return cacheManager;
}
4、SecurityManager 注入 redisCacheManager
@Qualifier("redisCacheManager")CacheManager
ok 这样 就搞定了 redis和ecache 缓存shrio的认证授权数据。
接下来看看 怎么用使用redis进行基于shiro的session集群共享
redis进行基于shiro的session集群共享
1、加入依赖
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.session
spring-session-data-redis
2、 配置session
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30) //maxInactiveIntervalInSeconds: 设置Session失效时间,使用Redis Session之后,原Boot的server.session.timeout属性不再生效
public class SessionConfig {
}
这样 就可以了
shiro-redis jar 缓存 shiro数据
1、加入依赖
org.crazycake
shiro-redis
2.4.6
2、 配置 redisCacheManager1
@Bean(name = "redisCacheManager1")
@Primary
public RedisCacheManager redisCacheManager1() {
logger.info("--------------redis cache init---------------");
RedisCacheManager redisCacheManager= new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager());
logger.info("--------------redis cache ---------------"+redisCacheManager);
return redisCacheManager;
}
public RedisManager redisManager() {
RedisManager redisManager= new RedisManager();
redisManager.setHost("119.29.53.182");
redisManager.setPassword("123456");
redisManager.setPort(6379);
return redisManager;
}
3、SecurityManager 注入 redisCacheManager1
@Qualifier("redisCacheManager1")CacheManager
ok 这样就搞定了
自己实现一些 操作 ecahe和redis的工具类
我的官网
我的官网http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的地址http://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/
阿里云优惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld