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

1.spring-redis.xml



						
	
	
		
		
		
		
        
        
        
        
 	

    
    
        
        
        
        
        
        
    

    
        
            
        
        
            
        
        
            
        
        
            
        
    

    
    
        
        
        
        
        
            
                
                
                
                
                
                
                
            
        
    

    
    
        
    

    
    



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 {

    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会出现乱码,不影响使用。

你可能感兴趣的:(shiro)