【问题标题】:Implement Caching in Apache Shiro using JBoss Infinspan使用 JBoss Infinspan 在 Apache Shiro 中实现缓存
【发布时间】:2016-11-21 22:27:01
【问题描述】:

我在一个项目中使用 Apache shiro 来实现安全性。我已经使用 CDI 注入了安全领域。我想使用 Jboss Infinispan 在 shiro 中实现身份验证和授权缓存。有人可以分享一些指针吗?

【问题讨论】:

    标签: java shiro infinispan


    【解决方案1】:

    你需要实现 org.apache.shiro.cache.Cache 和 org.apache.shiro.cache.CacheManager 来实现 shiro 中的缓存。如果您想使用 infinispan,请按照以下步骤操作:

    实现 org.apache.shiro.cache.Cache

    import java.util.Collection;
    import java.util.Set;
    
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheException;
    
    
    public class InfinispanCache<K, V> implements Cache<K, V> {
    
    
        private final org.infinispan.Cache<K, V> cacheProxy;
    
        public InfinispanCache(final org.infinispan.Cache<K, V> cacheProxy) {
            this.cacheProxy = cacheProxy;
        }
    
    
        @Override
        public V get(final K key) throws CacheException {
            return cacheProxy.get(key);
        }
    
    
        @Override
        public V put(final K key, final V value) throws CacheException {
            return cacheProxy.put(key, value);
        }
    
    
        @Override
        public V remove(final K key) throws CacheException {
            return cacheProxy.remove(key);
        }
    
    
        @Override
        public void clear() throws CacheException {
            cacheProxy.clear();
        }
    
    
        @Override
        public int size() {
            return cacheProxy.size();
        }
    
    
        @Override
        public Set<K> keys() {
            return cacheProxy.keySet();
        }
    
    
        @Override
        public Collection<V> values() {
            return cacheProxy.values();
        }
    
    }
    

    实现 org.apache.shiro.cache.CacheManager

    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheException;
    import org.apache.shiro.cache.CacheManager;
    import org.infinispan.manager.CacheContainer;
    
    
    public class InfinispanCacheManager implements CacheManager {
    
    
        private final CacheContainer cacheContainer;
    
        public InfinispanCacheManager(final CacheContainer cacheContainer) {
            this.cacheContainer = cacheContainer;
        }
    
        @Override
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public <K, V> Cache<K, V> getCache(final String name) throws CacheException {
            return new InfinispanCache(cacheContainer.getCache(name));
        }
    
    }
    

    注入缓存容器。如果您的 Realm 启用了 CDI,它应该可以工作。

    import javax.annotation.Resource;
    
     /** The security cache manager. */
        @Resource(lookup = "java:jboss/infinispan/container/<YOUR CACHE CONTAINER NAME>")
        private EmbeddedCacheManager securityCacheManager;
    

    在你的领域实现中设置你的缓存管理器:

    setCachingEnabled(true);
    setAuthenticationCachingEnabled(true);
    setAuthorizationCachingEnabled(true);
    setCacheManager(new InfinispanCacheManager(securityCacheManager));
    setAuthenticationCacheName(authenticationCacheName);
    setAuthorizationCacheName(authorizationCacheName);
    

    同样,您可以在 Shiro 中与其他缓存框架一起实现它。

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 2015-07-29
      • 2013-06-11
      • 2013-10-27
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2011-05-24
      • 2014-12-09
      相关资源
      最近更新 更多