上篇【Apache-Shiro+Zookeeper系统集群安全解决方案之会话管理】,解决了Shiro在系统集群开发时安全的会话共享问题,系统在使用过程中会有大量的权限检查和用户身份检验动作,为了不频繁访问储存容器,Shiro提供了三个缓存机制:

  1. 用户登录Session缓存,默认是不开启的,在Realm配置中设置authenticationCachingEnabled开启,
    使用原则是用户在登录成功后缓存登录校验信息,如 admin 登录成功后将用户名密码等缓存,在超时退出或直接关闭浏览器需要重新登录时不访问数据库。
    在用户退出或超时自动清理缓存数据。
    缓存数据通过配置的cacheManager存储。

  2. 用户权限缓存,用户访问到在配置URL规则范围内的链接时触发读取当前用户的权限并缓存
    同样缓存数据是通过配置的cacheManager存储。

  3. 还有一个是SessionDao中实现的用户登录后的身份信息缓存到本地比如内存,在一定时间内不会到Session存储容器中取用户信息,可以减少Shiro对存储容器的大量读取,在上文有提到并有实现方法。

所以本文的说到解决方案是Shiro + Zookeeper,Shiro权限框架,利用Zookeeper做存储容器。

用到的框架技术:

Spring + Shiro + Zookeeper

SHIRO整合SPRING配置

applicationContext-shiro.xml 伪代码:

<!-- 自定义shiro的realm -->
<bean id="jdbcAuthenticationRealm" class="..jdbcAuthenticationRealm" depends-on="...">
...
<!-- 自定义缓存名 -->
<property name="authorizationCacheName" value="shiroAuthorizationCache"/>
<property name="authenticationCacheName" value="shiroAuthenticationCache"/>
<!--
缓存用户登录信息,默认不缓存
缓存后用户在不点退出的情况下再次登录直接使用缓存中数据
注意修改密码后要自动退出用户, 否则用户直接关闭浏览器下次使用缓存数据将无法登录!
-->
<property name="authenticationCachingEnabled" value="true"/>
<!--缓存用户权限信息-->
<property name="authorizationCachingEnabled" value="true"/>
</bean> <!-- 缓存配置 -->
<bean id="zkShiroSessionCache" class="..ZKShiroCacheManager">
<property name="zookeeperTemplate" ref="zookeeperTemplate"/>
<!-- 结尾不加/ -->
<property name="shiroSessionCacheZKPath" value="/SHIROSESSIONCACHE"/>
<property name="sessionCachePrefix" value="cache-"/>
</bean> <!-- SHIRO安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="jdbcAuthenticationRealm"/>
<property name="cacheManager" ref="zkShiroSessionCache"/>
<property name="sessionManager" ref="sessionManager"/>
</bean>

缓存管理器

Shiro会通过缓存管理器和缓存名获取缓存DAO,比如上面配置的shiroAuthorizationCacheshiroAuthenticationCache会产生两个缓存DAO用来存取不同类型的缓存数据。

ZKShiroCacheManager.java

import bgonline.foundation.hadoop.zk.IZookeeperTemplate;
import org.apache.shiro.ShiroException;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.Destroyable;
import org.apache.shiro.util.Initializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert; /**
* SHIRO缓存Manager
*
*/
public class ZKShiroCacheManager implements CacheManager, Initializable, Destroyable { Logger logger = LoggerFactory.getLogger(this.getClass()); /**
* zk操作工具类
*/
private IZookeeperTemplate zookeeperTemplate; /**
* 缓存根路径
*/
private String shiroSessionCacheZKPath = "/SHIROSESSIONS2"; /**
* 缓存项前缀
*/
private String sessionCachePrefix = "cache-"; @Override
public void destroy() throws Exception {
} /**
* 根据名字返回CACHE
* @param s
* @param <K>
* @param <V>
* @return
* @throws CacheException
*/
@Override
public <K, V> Cache<K, V> getCache(String s) throws CacheException {
logger.info("shiro get cache, return : ZKShiroCache, cache name: {}", s);
Assert.notNull(zookeeperTemplate, "zookeeperTemplate must be set !");
return new ZKShiroCache<K, V>(zookeeperTemplate, shiroSessionCacheZKPath, sessionCachePrefix, s);
} @Override
public void init() throws ShiroException {
} public void setZookeeperTemplate(IZookeeperTemplate zookeeperTemplate) {
this.zookeeperTemplate = zookeeperTemplate;
} public void setShiroSessionCacheZKPath(String shiroSessionCacheZKPath) {
this.shiroSessionCacheZKPath = shiroSessionCacheZKPath;
} public void setSessionCachePrefix(String sessionCachePrefix) {
this.sessionCachePrefix = sessionCachePrefix;
}
}

缓存DAO

真正的缓存CRUD等操作类。

ZKShiroCache.java

import bgonline.foundation.hadoop.zk.IZookeeperTemplate;
import bgonline.foundation.hadoop.zk.ZNode;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.SerializationUtils; import java.util.*; /**
* SHIRO集群缓存储存类
*
*/
public class ZKShiroCache<K, V> implements Cache<K, V> { Logger logger = LoggerFactory.getLogger(this.getClass()); private IZookeeperTemplate zookeeperTemplate;
private String shiroSessionCacheZKPath;
private String sessionCachePrefix;
private String cacheName; /**
* 根据cacheName创建ZK CACHE 实例
*
* @param zookeeperTemplate zk操作工具类
* @param shiroSessionCacheZKPath 缓存根路径, 结尾不加/
* @param sessionCachePrefix 缓存项前缀
* @param cacheName 缓存名字
*/
public ZKShiroCache(IZookeeperTemplate zookeeperTemplate, String shiroSessionCacheZKPath, String sessionCachePrefix, String cacheName) {
this.zookeeperTemplate = zookeeperTemplate;
this.shiroSessionCacheZKPath = shiroSessionCacheZKPath;
this.sessionCachePrefix = sessionCachePrefix;
this.cacheName = cacheName;
} @Override
public V get(K k) throws CacheException {
String path = getPath(k);
logger.debug("get cache for key: {}", path); byte[] byteValue = zookeeperTemplate.getData(path).getByteData();
if (byteValue.length > 0)
return (V) SerializationUtils.deserialize(byteValue);
else
return null;
} @Override
public V put(K k, V v) throws CacheException {
V previous = get(k);
String path = getPath(k);
ZNode node = new ZNode();
node.setPath(path);
node.setByteData(SerializationUtils.serialize(v));
String nodepath = zookeeperTemplate.createNode(node);
logger.debug("put cache for key: {}, return path: {}", path, nodepath);
return previous;
} @Override
public V remove(K k) throws CacheException {
String path = getPath(k);
logger.debug("remove cache for key: {}", path);
V previous = get(k);
zookeeperTemplate.deleteNode(path);
return previous;
} @Override
public void clear() throws CacheException {
logger.debug("clear cache");
zookeeperTemplate.deleteNode(shiroSessionCacheZKPath);
} @Override
public int size() {
ZNode node = new ZNode();
node.setPath(shiroSessionCacheZKPath);
int size = zookeeperTemplate.getChildren(node).size();
logger.debug("get cache size: {}", size);
return size;
} @Override
public Set<K> keys() {
List<String> cl = cacheList();
logger.debug("get cache keys, size : {}", cl.size());
return (Set<K>) cl;
} @Override
public Collection<V> values() {
Collection<V> cs = new ArrayList<V>();
List<String> cl = cacheList();
for (String k : cl) {
//读取的key形如: cache-shiroAuthenticationCacheadmin
if (k.startsWith(sessionCachePrefix)) {
String noPrefixId = k.replace(sessionCachePrefix, "");
String path = getPath(noPrefixId);
cs.add((V) zookeeperTemplate.getData(path).getByteData());
}
}
logger.debug("get cache values, size : {}", cs.size());
return cs;
} /**
* 获取所有缓存列表
*
* @return list
*/
private List<String> cacheList() {
ZNode node = new ZNode();
node.setPath(shiroSessionCacheZKPath);
List<String> children = zookeeperTemplate.getChildren(node);
logger.debug("get cache list, size : {}", children.size());
return children;
} /**
* 生成缓存项存储全路径KEY
* 根路径/缓存前缀+缓存名字+缓存KEY
*
* @param key
* @return
*/
private String getPath(Object key) {
return shiroSessionCacheZKPath + '/' + sessionCachePrefix + cacheName + key;
}
}

小结

Shiro提供了很全面的接口,方便我们根据自己的需要任意扩展,关于自定义缓存还可以参考Shiro自带的MemoryConstrainedCacheManagerMapCache 这也是一个非常好的例子。

文中提到几个自己封装的有关Zookeeper的类可以替换成自己的实现类,或者也可能直接使用其它的工具做存储容器比如数据库等,本文主要是起到抛砖引玉的作用,说明Shiro在缓存共享自定义实现的过程和配置。

Apache-Shiro+Zookeeper系统集群安全解决方案之缓存管理的更多相关文章

  1. Apache-Shiro+Zookeeper系统集群安全解决方案之会话管理

    如今的系统多不是孤军奋战,在多结点会话共享管理方面有着各自的解决办法,比如Session粘连,基于Web容器的各种处理等或者类似本文说的完全接管Web容器的Session管理,只是做法不尽相同. 而本 ...

  2. 一键运行CIS安全扫描,集群安全无忧!

    CIS安全扫描是Rancher 2.4推出的其中一个重磅功能,旨在帮助用户快速.有效地加强集群的安全性.本文将详细介绍CIS安全扫描这一功能,包含详细的操作demo. 本文来自Rancher Labs ...

  3. JAVA系统架构高并发解决方案 分布式缓存 分布式事务解决方案

    JAVA系统架构高并发解决方案 分布式缓存 分布式事务解决方案

  4. mongodb副本集加分片集群安全认证使用账号密码登录

    mongodb副本集加分片集群搭建网上资料有很多.粘贴一个写的比较好的.副本集加分片搭建 对于搭建好的mongodb副本集加分片集群,为了安全,启动安全认证,使用账号密码登录. 默认的mongodb是 ...

  5. kafka集群安全化之启用kerberos与acl

    一.背景 在我们部署完kafka之后,虽然我们已经可以“肆意”的用kafka了,但是在一个大公司的实际生产环境中,kafka集群往往十分庞大,每个使用者都应该只关心自己所负责的Topic,并且对其他人 ...

  6. kubernetes实战(八):k8s集群安全机制RBAC

    1.基本概念 RBAC(Role-Based Access Control,基于角色的访问控制)在k8s v1.5中引入,在v1.6版本时升级为Beta版本,并成为kubeadm安装方式下的默认选项, ...

  7. Kubernetes集群安全概述

    API的访问安全性 API Server的端口和地址 在默认情况下,API Server通过本地端口和安全端口两个不同的HTTP端口,对外提供API服务,其中本地端口是基于HTTP协议的,用于在本机( ...

  8. kubernetes(k8s)集群安全机制RBAC

    1.基本概念 RBAC(Role-Based Access Control,基于角色的访问控制)在k8s v1.5中引入,在v1.6版本时升级为Beta版本,并成为kubeadm安装方式下的默认选项, ...

  9. elasticsearch集群安全重启节点

    es2.x 关闭集群的动态分片:(动态分片开启状态如果节点宕机了,会导致集群重新分配数据进行数据转移,会导致节点直接大量传输数据)curl -XPUT 'http://192.168.248.193: ...

随机推荐

  1. php 缓存加速器软件

    Xcache 和 memcached 是两个不同层面的缓存,不存在可比性.Xcache 是 php 底层的缓存,它将PHP程式编译成字节码(byte code),再透过服务器上安装对应的程式来执行PH ...

  2. 【python cookbook】【字符串与文本】16.以固定的列数重新格式化文本

    问题:重新格式化一些很长的字符串,以指定的列数来显示 解决方案:textwrap模块的fill()方法来实现 # A long string s = "Look into my eyes, ...

  3. scala一些高级类型

    package com.ming.test import scala.collection.mutable.ArrayBuffer import scala.io.Source import java ...

  4. xib中设置控件的圆角

    1.http://my.oschina.net/ioslighter/blog/387991?p=1 利用layer.cornerRadius实现一个圆形的view,将layer.cornerRadi ...

  5. JSP直接连接sql2008数据库并显示

    <%@ page contentType="text/html; charset=utf-8" language="java" errorPage=&qu ...

  6. activeMQ下载,安装,启动,关闭

    1.新建一个文件夹activeMQ   mkdir /server 2.授权    chmod 777 /server 3.下载activeMQ安装包,拷贝到/activeMQ目录下 apache-a ...

  7. Caused by: javax.xml.bind.JAXBException: standardPremiumUpdateMessageDTO is not a valid property on

    Caused by: javax.xml.bind.JAXBException:  standardPremiumUpdateMessageDTO is not a valid property on ...

  8. PostgreSQL中如何查看一个表所对应的文件

    通过pg_relation_filepath可以直接表(索引)对象对应的物理文件在哪里? 上面截图是“德哥”做的ppt:上面有详细解释! 当然也可以通过 系统表 pg_class 可以直接查出对应的物 ...

  9. ACM题目————还是畅通工程

    Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...

  10. c#记事本

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...