@Override public Set<K> keys() { return _cache.keys(); }
@Override public Collection<V> values() { ImmutableList.Builder<V> values = ImmutableList.builder(); for (K key : _cache.keys()) { ValidatingEntry entry = _cache.get(key); V value; if (entry != null && (value = entry.getValue(key)) != null) { values.add(value); } } return values.build(); }
/** * 清除所有用户授权信息缓存. */ public void clearAllCachedAuthorizationInfo() { Cache<Object, AuthorizationInfo> cache = getAuthorizationCache(); if (cache != null) { for (Object key : cache.keys()) { cache.remove(key); } } }
/** * 清除用户的权限 * <p> * 这里需要注意的是, * 网上很多实现都是这里只传递一个String类型的username过来,根据这个String当key去清除Cache * 但是Shiro在缓存用户权限的时候使用的key并不是String类型,所以调用remove的时候并不能清除缓存的权限 * <p> * shiro缓存时使用的key,是登录时使用的SimplePrincipalCollection对象,所以remove的时候需要的不是一个String值, * 具体可以参考下面方法中打印cache的key的过程, 可以看到打印出key的类是 `class org.apache.shiro.subject.SimplePrincipalCollection` * 所以你cache.remove(String username)肯定清除不了 * * @param principal */ public void clearAuthorizationInfo(AuthCachePrincipal principal) { logger.info("clear the user: " + principal.toString() + "'s authorizationInfo"); Cache<Object, Object> cache = cacheManager.getCache(EcAuthorityRealm.class.getName() + ".authorizationCache"); SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, principal.getAuthCacheKey()); for (Object k : cache.keys()) { logger.info(k.getClass().getName()); } cache.remove(principals); }
public void clearCache(@Observes ClearAuthorizationCacheEvent event) { checkCache(); if (cache != null) { UserPrincipal userPrincipal = event.getUserPrincipal(); if (userPrincipal == null) { // no principal specified in event, clear all cache.clear(); } else { // remove only for one principal // cache.remove(userPrincipal); -> doesn't when we have a CDI proxy for userPrincipal Object key = null; for (Object k : cache.keys()) { if (k.equals(userPrincipal)) { key = k; } } if (key != null) { cache.remove(key); } } } }
/** 获取所有在线用户 **/ public List<IUser> getAllActiveUsers() { String principalsKey = DefaultSubjectContext.PRINCIPALS_SESSION_KEY; Cache<Serializable, Session> sessionCache = cachingSessionDao.getActiveSessionsCache(); List<IUser> list = new ArrayList<>(); Set<Serializable> keys = sessionCache.keys(); for (Serializable key : keys) { Session session = sessionCache.get(key); if (session == null) { continue; } PrincipalCollection principals = (PrincipalCollection) session.getAttribute(principalsKey); if (principals == null) { continue; } Object principal = getAvailablePrincipal(principals); if (principal instanceof IUser) { list.add((IUser) principal); } } return list; }
Set<Serializable> keys = sessionCache.keys(); for (Serializable key : keys) { Session session = sessionCache.get(key);
Set<Serializable> keys = sessionCache.keys(); for (Serializable key : keys) { Session session = sessionCache.get(key);
Cache<Serializable, Session> sessionCache = cachingSessionDao.getActiveSessionsCache(); Set<Serializable> keys = sessionCache.keys(); for (Serializable key : keys) { Session session = sessionCache.get(key);