美文网首页alreadyredisshiro
Shiro结合Redis序列化之踩坑

Shiro结合Redis序列化之踩坑

作者: CodeYang | 来源:发表于2022-01-26 15:21 被阅读0次

注意:
保存用户信息的类记得实现序列化 implements Serializable

踩坑1

关于登录的


image.png

错误原因:
SimpleByteSource 类没有实现序列化

解决方式:
1.新增实现类

里面的方法都是从SimpleByteSource拷贝过来的,多的就是一个 implements Serializable


public class MySimpleByteSource implements ByteSource, Serializable {
    
    private final byte[] bytes;
    private String cachedHex;
    private String cachedBase64;
    
    public MySimpleByteSource(byte[] bytes) {
        this.bytes = bytes;
    }

    public MySimpleByteSource(char[] chars) {
        this.bytes = CodecSupport.toBytes(chars);
    }

    public MySimpleByteSource(String string) {
        this.bytes = CodecSupport.toBytes(string);
    }

    public MySimpleByteSource(ByteSource source) {
        this.bytes = source.getBytes();
    }

    public MySimpleByteSource(File file) {
        this.bytes = (new MySimpleByteSource.BytesHelper()).getBytes(file);
    }

    public MySimpleByteSource(InputStream stream) {
        this.bytes = (new MySimpleByteSource.BytesHelper()).getBytes(stream);
    }

    public static boolean isCompatible(Object o) {
        return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
    }

    public byte[] getBytes() {
        return this.bytes;
    }

    public boolean isEmpty() {
        return this.bytes == null || this.bytes.length == 0;
    }

    public String toHex() {
        if (this.cachedHex == null) {
            this.cachedHex = Hex.encodeToString(this.getBytes());
        }

        return this.cachedHex;
    }

    public String toBase64() {
        if (this.cachedBase64 == null) {
            this.cachedBase64 = Base64.encodeToString(this.getBytes());
        }

        return this.cachedBase64;
    }

    public String toString() {
        return this.toBase64();
    }

    public int hashCode() {
        return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (o instanceof ByteSource) {
            ByteSource bs = (ByteSource)o;
            return Arrays.equals(this.getBytes(), bs.getBytes());
        } else {
            return false;
        }
    }

    private static final class BytesHelper extends CodecSupport {
        private BytesHelper() {
        }

        public byte[] getBytes(File file) {
            return this.toBytes(file);
        }

        public byte[] getBytes(InputStream stream) {
            return this.toBytes(stream);
        }
    }
}

2.修改UserRealm


image.png

踩坑2

关于登录的


image.png
解决:

序列化方式修改成 StringRedisSerializer

Redis配置

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factoryBean) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(factoryBean);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //单独设置键和值的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);

        //设置配置,可以不加
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

踩坑3

关于授权的

image.png image.png

原因:

热部署的问题

解决:
把热部署依赖去掉即可

相关文章

网友评论

    本文标题:Shiro结合Redis序列化之踩坑

    本文链接:https://www.haomeiwen.com/subject/kvenhrtx.html