shiro 使用缓存时出现:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource

在学习Shiro使用缓存时,出现:
java.io.NotSerializableException:org.apache.shiro.util.SimpleByteSource异常,开启debug会提示:
ERROR [authentication.data] - Disk Write of test failed: 错误。

出现这种情况是因为:SimpleByteSource没有是实现Serializable接口

解决办法:自定义一个类继承SimpleByteSource实现Serializable接口

当然也可以实现ByteSource接口和Serializable接口,但是实现ByteSource接口需要实现其方法,不方便。

自定义一个SimpleByteSource 类继承继承SimpleByteSource实现Serializable接口。

import java.io.Serializable;
 
public class SimpleByteSource extends org.apache.shiro.util.SimpleByteSource
implements Serializable{
 
	private static final long serialVersionUID = 5528101080905698238L;
 
	public SimpleByteSource(byte[] bytes) {
		super(bytes);
		// TODO 自动生成的构造函数存根
	}
 
}

然后创建工具类ByteSourceUtils

import org.apache.shiro.util.ByteSource;
 
public class ByteSourceUtils{
	public static ByteSource bytes(byte[] bytes){
		return new SimpleByteSource(bytes);
	}
	public static ByteSource bytes(String arg0){
		return new SimpleByteSource(arg0.getBytes());
	}
}

在自定义realm的认证方法中:

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		 
        String username = (String)token.getPrincipal();  
        User user = userService.getByUsername(username); 
        if(user == null) {  
            throw new UnknownAccountException();//没找到帐号  
        }  
        if(Boolean.TRUE.equals(user.isLocked())) {
        	throw new LockedAccountException(); //帐号锁定
        }
        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(  
                user.getUsername(), //用户名  
                user.getPassword(), //密码  
                ByteSourceUtils.bytes(user.getSalt()),//salt
                getName()  //realm name  
        );  
        return authenticationInfo;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值