Shiro的緩衝交給redis管理

來源:互聯網
上載者:User
Shiro的緩衝交給redis管理

標籤(空格分隔):Shiro redis

前言:

以下方式只是單機版的redis使用

一、匯入Shiro和redis的jar

    jedis-2.7.3.jar    shiro-core-1.2.3.jar    shiro-ehcache-1.2.3.jar    shiro-spring-1.2.3.jar    shiro-web-1.2.3.jar    slf4j-api-1.7.5.jar    slf4j-log4j12-1.7.5.jar

二、具體實現

JedisUtils工具類

import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisUtils {    private static JedisPool jedisPool;    static {        JedisPoolConfig jedisConfig = new JedisPoolConfig();        jedisConfig.setMaxTotal(100);        jedisConfig.setMaxIdle(10);        jedisConfig.setMaxWaitMillis(100);        //主機名稱和連接埠號碼,開啟redis的伺服器和連接埠號碼        jedisPool = new JedisPool(jedisConfig, "192.168.0.118", 6379);    }    public static Jedis getJedis() {        return jedisPool.getResource();    }    public static void close(Jedis jedis) {        jedis.close();    }}

將Shiro的緩衝交給redis就需要實現Shiro的Cache介面

import java.io.Serializable;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.List;import java.util.Set;import org.apache.commons.lang3.SerializationUtils;import org.apache.shiro.cache.Cache;import org.apache.shiro.cache.CacheException;import redis.clients.jedis.Jedis;import com.baizhi.util.JedisUtils;/** * 自訂redis cache * @author MOTUI * */public class ShiroRedisCache<K,V> implements Cache<K,V>{    // shiro cache  key = value     // redis key = value    public Object get(Object key) throws CacheException {        byte[] bs = SerializationUtils.serialize((Serializable)key);        byte[] value = JedisUtils.getJedis().get(bs);        if(value == null){            return null;        }        return SerializationUtils.deserialize(value);    }    /**     * 將shiro的緩衝儲存到redis中     */    public Object put(Object key, Object value) throws CacheException {        Jedis jedis = JedisUtils.getJedis();        //序列化   和  還原序列化        jedis.set(SerializationUtils.serialize((Serializable)key), SerializationUtils.serialize((Serializable)value));        byte[] bs = jedis.get(SerializationUtils.serialize((Serializable)key));        Object object = SerializationUtils.deserialize(bs);        return object;    }    public Object remove(Object key) throws CacheException {        Jedis jedis = JedisUtils.getJedis();        byte[] bs = jedis.get(SerializationUtils.serialize((Serializable)key));        jedis.del(SerializationUtils.serialize((Serializable)key));        return SerializationUtils.deserialize(bs);    }    /**     * 清空所有緩衝     */    public void clear() throws CacheException {        JedisUtils.getJedis().flushDB();    }    /**     * 緩衝的個數     */    public int size() {        Long size = JedisUtils.getJedis().dbSize();        return size.intValue();    }    /**     * 擷取所有的key     */    public Set keys() {        Set<byte[]> keys = JedisUtils.getJedis().keys(new String("*").getBytes());        Set<Object> set = new HashSet<Object>();        for (byte[] bs : keys) {            set.add(SerializationUtils.deserialize(bs));        }        return set;    }    /**     * 擷取所有的value     */    public Collection values() {        Set keys = this.keys();        List<Object> values = new ArrayList<Object>();        for (Object object : keys) {            byte[] bs = JedisUtils.getJedis().get(SerializationUtils.serialize((Serializable)object));            values.add(SerializationUtils.deserialize(bs));        }        return values;    }}

自訂CustomCacheManager實現CacheManager

import org.apache.shiro.cache.Cache;import org.apache.shiro.cache.CacheException;import org.apache.shiro.cache.CacheManager;public class CustomCacheManager implements CacheManager{    public <K, V> Cache<K, V> getCache(String name) throws CacheException {        return new ShiroRedisCache<K,V>();    }}

在Shiro的配置中配置,shiro.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans                     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd                    http://www.springframework.org/schema/aop                     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd                    http://www.springframework.org/schema/tx                     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd                    http://www.springframework.org/schema/context                    http://www.springframework.org/schema/context/spring-context-3.2.xsd                    http://www.springframework.org/schema/mvc                    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">                     <!-- 建立shiroFilterFactoryBean --><bean id="shiroFilterFactoryBean" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">    <!-- 依賴注入安全管理器 -->    <property name="securityManager" ref="securityManager"/>    <!-- 設定預設的登入頁面 -->    <property name="loginUrl" value="/xxx/login.jsp"/>    <!-- 使用者訪問未對其授權的資源時,所顯示的串連 -->    <property name="unauthorizedUrl" value="/unauthorizedAndError.jsp"/>    <!-- 指定資源存取權限 -->    <property name="filterChainDefinitions">        <value>            /** = authc                 </value>    </property></bean><!-- 建立安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">    <!-- 依賴自訂realms -->    <property name="realm" ref="customerRealm"/>    <!-- 依賴緩衝 -->    <property name="cacheManager" ref="cacheManager"/></bean><!-- 建立自訂的realm --><bean id="customerRealm" class="com.baizhi.shiro.realm.CustomerRealm">    <!-- 依賴憑證匹配器 -->    <property name="credentialsMatcher" ref="credentialsMatcher"/>    <!-- 開啟認證和授權的緩衝 -->    【此處的認證緩衝不能開啟,原因未知。。】    <!-- <property name="authenticationCachingEnabled" value="true"/>    <property name="authenticationCacheName" value="authentication"/> -->    <property name="authorizationCachingEnabled" value="true"/>    <property name="authorizationCacheName" value="authorization"/></bean><!-- 建立憑證匹配器 --><bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">    <property name="hashAlgorithmName" value="MD5"/>    <property name="hashIterations" value="1024"/></bean><!-- 開啟許可權註解功能 --><bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">    <!-- 依賴安全管理器 -->    <property name="securityManager" ref="securityManager"/></bean>     <!-- 建立緩衝管理器 -->【自訂的CustomCacheManager】<!-- redis緩衝 --><bean id="cacheManager" class="com.xxx.cache.shiro.CustomCacheManager"/>

總結

將Shiro的緩衝交給redis管理可以提高效率。提高對資料的讀取的速度
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.