SpringBoot结合Shiro实现redis缓存序列化问题

no valid constructor,这个错误是反序列化导致的,第一次正常登录的时候是shiro缓存序列化后存入到redis里,第二次登录账号从redis里取shiro缓存信息时不能反序列化取出缓存,就出这个错误:no valid constructor要解决取缓存反序列化的问题,还是要实现ByteSource接口。

把SimpleByteSource里面的全部方法和属性  复制到继承ByteSource接口的类上

  1.自定义的随机盐转化  实现ByteSource接口

package com.hzw.shiro;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.util.ByteSource;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
//自定义随机盐转化
public class MyBateSource implements ByteSource,Serializable{
    private byte[] bytes;
    private String cachedHex;
    private String cachedBase64;
    public MyBateSource(){

    }
    public MyBateSource(byte[] bytes) {
        this.bytes = bytes;
    }

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

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

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

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

    public MyBateSource(InputStream stream) {
        this.bytes = (new MyBateSource.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.替换随机盐转化  new MyBateSource(user.getSalt())

 @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("================");
        //获取身份信息
        String principal = (String)token.getPrincipal();
        UserService userService =(UserService)AppliactionContextUtils.getBean("userService");
        User user = userService.findByUserName(principal);
        System.out.println(principal);
        if (!ObjectUtils.isEmpty(user)){
            return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),new MyBateSource(user.getSalt()),this.getName());
        }
        return null;
    }

测试 可以看出登录账号从redis里取shiro缓存信息

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值