使用shiro缓存用户身份信息的时候报:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource
最近在使用shiro缓存用户的身份信息的时候,报了simpleByteSource不能序列化,跟进源码一看,原来这个类没有实现序列化的接口,但是我在缓存身份信息的实现又要用到这个类,解决方法:重写一个类,模仿着SimpleByteSoure这个类,写个自己的类。并且实现序列化接口即可·。
package com.dzf.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.Serializable;
import java.util.Arrays; /**
* <p>
* 为了解决redis序列化的问题,SimpleByteSource没有实现序列化接口
* 在传入simpleAuthenticationInfo()的时候,缓存用户信息会出现序列化异常
* </p>
* @author dingzf
* @date 2018/3/7
* @time 21:54
*/
public class ShiroByteSource implements ByteSource, Serializable {
private static final long serialVersionUID = -6814382603612799610L;
private volatile byte[] bytes;
private String cachedHex;
private String cachedBase64; public ShiroByteSource() { } public ShiroByteSource(String string) {
this.bytes = CodecSupport.toBytes(string);
} public void setBytes(byte[] bytes) {
this.bytes = bytes;
} @Override
public byte[] getBytes() {
return this.bytes;
} @Override
public String toHex() {
if ( this.cachedHex == null ) {
this.cachedHex = Hex.encodeToString(getBytes());
}
return this.cachedHex;
} @Override
public String toBase64() {
if ( this.cachedBase64 == null ) {
this.cachedBase64 = Base64.encodeToString(getBytes());
}
return this.cachedBase64;
} @Override
public boolean isEmpty() {
return this.bytes == null || this.bytes.length == 0;
} @Override
public String toString() {
return toBase64();
} @Override
public int hashCode() {
if (this.bytes == null || this.bytes.length == 0) {
return 0;
}
return Arrays.hashCode(this.bytes);
} @Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof ByteSource) {
ByteSource bs = (ByteSource) o;
return Arrays.equals(getBytes(), bs.getBytes());
}
return false;
} public static ByteSource of(String string) {
return new ShiroByteSource(string);
}
}
在自己的实现的realm里面调用如下:
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), new ShiroByteSource(user.getSalt()), getName());
以前的写法是:
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), new SimpleByteSource(user.getSalt()), getName());
然后就可以解决问题了。
使用shiro缓存用户身份信息的时候报:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource的更多相关文章
- Winform框架中窗体基类的用户身份信息的缓存和提取
在Winform开发中,有时候为了方便,需要把窗体的一些常规性的数据和操作函数进行封装,通过自定义基类窗体的方式,可以实现这些封装管理,让我们的框架统一化.简单化的处理一些常规性的操作,如这里介绍的用 ...
- 基于SqlSugar的开发框架循序渐进介绍(6)-- 在基类接口中注入用户身份信息接口
在基于SqlSugar的开发框架中,我们设计了一些系统服务层的基类,在基类中会有很多涉及到相关的数据处理操作的,如果需要跟踪具体是那个用户进行操作的,那么就需要获得当前用户的身份信息,包括在Web A ...
- 【shiro】报错:Caused by: java.lang.ClassNotFoundException: org.apache.shiro.spring.LifecycleBeanPostProcessor
Caused by: java.lang.ClassNotFoundException: org.apache.shiro.spring.LifecycleBeanPostProcessor at o ...
- Shrio | java.io.IOException: Resource [classpath:shiro.ini] could not be found
案例 今天项目启动时一直报异常,看了错误日志发现是shrio文件找不到引起的,异常: java.io.IOException: Resource [classpath:shiro.ini] could ...
- 5G信令(就是用户身份信息)风暴——就是客户端通过公钥加密的消息(携带手机IMSI号)发给服务端,服务器需用私钥解密,这个解密比较消耗资源,如果短时间大量请求到来就会触发信令风暴
信令:手机开机后,先从USIM中读取之前运营商分配的临时身份信息GUTI/TMSI,发送携带该身份信息的信令给基站,请求接入运营商网络. 如果每个设备的每条消息都需要单独认证,则网络侧安全信令的验证需 ...
- 5G信令(就是用户身份信息)——手机开机后,先从USIM中读取之前运营商分配的临时身份信息GUTI/TMSI,发送携带该身份信息的信令给基站,请求接入运营商网络。
5G时代,跟IMSI-CATCHER SAY GOODBYE from:https://unicorn.360.com/blog/2018/04/18/GoodBye_5G_IMSI-Catcher/ ...
- 利用Cookie保存用户身份信息实现免登录
<%@page import="sun.misc.BASE64Encoder"%> <%@page import="java.util.Base64.E ...
- 使用缓存时出现java.io.NotSerializableException:xxx.xxx.xxx.Bean解决办法
解决方案: 开发过程中如果想缓存某个JavaBean,请确保它所引用的对象都implents Serializable,如果某个对象不需要被cache,可以加上transient关键字,否则Ehc ...
- pom.xml报错 : Missing artifact org.apache.shiro:shiro-spring:bundle:1.2.5
添加有<type>bundle</type>标签的依赖时,都会报这个错. 需要在<build/><plugins/>里面追加标签 <plugin& ...
随机推荐
- sqlmap注入教程
1.SQLMAP用于Access数据库注入 (1)猜解是否能注入win: python sqlmap.py -u "http://www.stronkin.com/en/CompHonorB ...
- 认识tornado(四)
接下来我们看一下helloword.py的唯一一个handler. 1 class MainHandler(tornado.web.RequestHandler): 2 def get(self): ...
- python中的self
1.首先明确的是self只有在类的方法中才会有,独立的函数或方法是不必带有self的.self在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数. self名称不是必须的,在python中se ...
- 【BZOJ3876】[Ahoi2014]支线剧情 有上下界费用流
[BZOJ3876][Ahoi2014]支线剧情 Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩 ...
- 【BZOJ3931】[CQOI2015]网络吞吐量 最大流
[BZOJ3931][CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为 ...
- [Algorithms] Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...
- Windows(7)上那些好用的软件及优化技巧(原创)
*本文为原创内容,转载请注明作者和出处:www.cnblogs.com/wang1024 软件篇 注:以下软件在百度直接搜索软件名均可找到官网,直接官网下载即可 大众的软件哪个好: 杀毒软件专题 基于 ...
- 160811、29 个你必须知道的 Linux 命令
虽然Linux发行版支持各种各样的饿GUI(graphical user interfaces),但在某些情况下,Linux的命令行接口(bash)仍然是简单快速的.Bash和 Linux Shell ...
- 巨蟒python全栈开发数据库前端5:JavaScript1
1.js介绍&变量&基础数据类型 2.类型查询&运算符&if判断&for循环 3.while循环&三元运算符 4.函数 5.今日总结 1.js介绍&am ...
- XML 之快速入门
XML 简介 - XML, 即可扩展标记语言(eXtensible Markup Language), 是一种标记语言. - 标记型语言: 使用标签进行操作 - 可扩展: XML 的标签可以自定义 - ...