解决shiro-redis集成序列化的问题

1.spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
						
	<!-- redis连接池 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxIdle}" />
		<property name="maxIdle" value="${redis.maxTotal}" />
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
 	</bean>

    <!-- redis连接工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostName}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="database" value="${redis.database}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="jedisConnectionFactory">
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="keySerializer">
            <bean class="com.ytsd.common.common.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="com.ytsd.common.common.StringRedisSerializer"/>
        </property>
    </bean>

    <!--spring cache-->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
          c:redisOperations-ref="redisTemplate">
        <!-- 默认缓存10分钟 -->
        <property name="defaultExpiration" value="${redis.expiration}"/>
        <property name="usePrefix" value="true"/>
        <!-- cacheName 缓存超时配置,半小时,一小时,一天 -->
        <property name="expires">
            <map key-type="java.lang.String" value-type="java.lang.Long">
                <entry key="halfHour" value="1800"/>
                <entry key="hour" value="3600"/>
                <entry key="oneDay" value="86400"/>
                <!-- shiro cache keys -->
                <entry key="authorizationCacheAdmin" value="1800"/>
                <entry key="authenticationCacheAdmin" value="1800"/>
                <entry key="activeSessionCacheAdmin" value="1800"/>
            </map>
        </property>
    </bean>

    <!-- 注入redis的操作工具类 -->
    <bean id="redisService" class="com.ytsd.service.service.impl.RedisServiceImpl">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>

    <!-- cache注解,和spring-ehcache.xml中的只能使用一个 -->
    <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>

</beans>

2.StringRedisSerializer.java

package com.ytsd.common.common;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.util.Assert;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.nio.charset.Charset;

/**
 * Created by zhanghao
 * 2018-05-26 10:32
 */
public class StringRedisSerializer implements RedisSerializer<Object> {

    private final Charset charset;

    private final String target = "\"";

    private final String replacement = "";

    public StringRedisSerializer() {
        this(Charset.forName("UTF8"));
    }

    public StringRedisSerializer(Charset charset) {
        Assert.notNull(charset, "Charset must not be null!");
        this.charset = charset;
    }

    @Override
    public String deserialize(byte[] bytes) {
        return (bytes == null ? null : new String(bytes, charset));
    }

    @Override
    public byte[] serialize(Object object) {
        String string = JSON.toJSONString(object);
        if (string == null) {
            return null;
        }
        string = string.replace(target, replacement);
        return string.getBytes(charset);
    }
}

ps:其中redis中的key使用string类型的,value使用JdkSerializationRedisSerializer序列化,value会出现乱码,不影响使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值