shiro 使用缓存时出现:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource
2021/9/9 22:35:14
本文主要是介绍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; }
这篇关于shiro 使用缓存时出现:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求