异常背景:

做shiro+salt+redis整合时,一直报不能序列化错误,不开启redis时还是正常的,开启redis后就出错。


问题描述:

自定义CustomerRealm extends AuthorizingRealm中认证方法中代码,问题出在ByteSource.Util.bytes(user.getSalt().getBytes())加入的随机盐部分不能序列化!(这也算是shiro中的一个诟病)!另外如果要做授权,实体类比如User、Role、Permission这些都要实现Serializable接口以确保可以序列化。

@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {String principal = (String) authenticationToken.getPrincipal();User user = userService.findByUserName(principal);if (!ObjectUtils.isEmpty(user)) {return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),new MyByteSource(user.getSalt()),//ByteSource.Util.bytes(user.getSalt().getBytes()),this.getName());}return null;}

解决方案:

通过自定义MyByteSource.java来解决序列化问题,其中所有代码全是复制自SimpleByteSource.java类(因为该类已经实现了ByteSource,虽然大部分方法都没用,但还是要加上),然后加入无参构造,再稍进行修改即可。

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;//salt实现自定义序列化接口
public class MyByteSource  implements ByteSource, Serializable {private  byte[] bytes;private String cachedHex;private String cachedBase64;public MyByteSource() {}public MyByteSource(byte[] bytes) {this.bytes = bytes;}public MyByteSource(char[] chars) {this.bytes = CodecSupport.toBytes(chars);}public MyByteSource(String string) {this.bytes = CodecSupport.toBytes(string);}public MyByteSource(ByteSource source) {this.bytes = source.getBytes();}public MyByteSource(File file) {this.bytes = (new MyByteSource.BytesHelper()).getBytes(file);}public MyByteSource(InputStream stream) {this.bytes = (new MyByteSource.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;}@Overridepublic byte[] getBytes() {return this.bytes;}@Overridepublic boolean isEmpty() {return this.bytes == null || this.bytes.length == 0;}@Overridepublic String toHex() {if (this.cachedHex == null) {this.cachedHex = Hex.encodeToString(this.getBytes());}return this.cachedHex;}@Overridepublic String toBase64() {if (this.cachedBase64 == null) {this.cachedBase64 = Base64.encodeToString(this.getBytes());}return this.cachedBase64;}@Overridepublic String toString() {return this.toBase64();}@Overridepublic int hashCode() {return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;}@Overridepublic 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);}}
}

java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource at java.io.ObjectOutputStr相关推荐

  1. java.io.NotSerializableException: org.apache.kafka.clients.consumer.ConsumerRecord

    java消费kafka数据时报错 ERROR [Executor task launch worker for task 90] - Exception in task 0.0 in stage 54 ...

  2. java.lang.NoClassDefFoundError: org/apache/tomcat/util/res/StringManager

    java.lang.NoClassDefFoundError: org/apache/tomcat/util/res/StringManager 一个比较老的web项目,  IDEA 导入后不能用,  ...

  3. java.lang.NoClassDefFoundError: org/apache/shiro/authc/AuthenticationToken

    使用ieda构建web工程项目,启动tomcat报错: java.lang.NoClassDefFoundError: org/apache/shiro/authc/AuthenticationTok ...

  4. java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray

    "java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray" 意味着在程序运行时,找不到类 &quo ...

  5. java.lang.NullPointerException org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)

    java.lang.NullPointerException org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) org.apache.jsp.i ...

  6. java date只保留年月日_java.util.Date、java.sql.Date、java.sql.Timestamp区别和总结

    在web开发中,避免不了对日期的操作,就几种常见的日期操作做个总结(部分参考网络,在此表示感谢): java.util.Date.java.sql.Datej.java.sql.Timestamp j ...

  7. 记录报错:java.lang.NullPointerException org.apache.jsp.test_jsp._jspInit(test_jsp.java:23)

    一个刚写的jsp 直接访问的时候报错了,原因是 项目lib下面的jsp-api.jar和tomcat 下的jsp-api.jar有冲突导致的, 我吧lib下面的这两个包删掉即可 type Except ...

  8. java.lang.UnsatisfiedLinkError: org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray

    hive中的报错信息,完全看不懂, 最后我参考了 http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed19fa950d100b92235c4380146 ...

  9. apache shiro怎么升级_Springboot整合Shiro之授权

    第二条为推广文章,阅读一次0.3kuai, 收入用于网站服务器及资源索取. Shiro是我们常用的一个权限管理框架,本文的重点是来介绍下在SpringBoot环境下我们怎么来使用Shiro. 一.添加 ...

最新文章

  1. 中文微博客的热门锐推用户榜
  2. python3 xrange *的作用
  3. Win 8 app 获取窗口的宽度和高度, 本地化, 及文本读取
  4. python函数每日一讲 - cmp(x,y)
  5. SpringMVC—对Ajax的处理(含 JSON 类型)(2)
  6. tostring 16进制_ToString:身份哈希码的十六进制表示形式
  7. spring security 注解_Spring框架使用@Autowired自动装配引发的讨论
  8. BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
  9. 计算机一直在启动修复怎么关机,电脑开机总是要启动修复修复后重启还要修复怎么处理方法...
  10. arduino cc3000 php,【arduino】新手求助, 想问一下关於CC3000设置的问题
  11. android字体设置
  12. SAP HANA - 企业管理软件数据库表结构完整文档
  13. 系统运维工程师面试的11大热门问题
  14. IDEAR 自动生成serialVersionUID
  15. 如何快速开发一个响应式移动端页面
  16. 小程序实现image标签的图片铺满整个屏幕,高度自适应
  17. 2022 ICLR | Geodiff:分子构象几何扩散生成模型
  18. 分享一个前两天写的自动监控blocking 的脚本(基于12C)
  19. 【Java-Calendar】打印日历
  20. OidProducer保姆级铺码教程(教你如何自制点读书)

热门文章

  1. 第一份工作是选择安逸,还是勇敢尝试?
  2. 【网易云信 x 齿轮易创】互联网时代,如何在教育行业抢占C位
  3. 网易云信携手小天才电话手表 打造视频通话体验的行业标杆
  4. Spring Boot使用Spring Data Redis操作Redis(单机/集群)
  5. dwarf调试信息格式入门
  6. Xcode 6.3 Ineligible Devices 临时解决方法
  7. 在VS2010中配制Elmah邮件发送到Gmail
  8. SpringBatch 多线程(TaskExecutor)启动Job详解 (七)
  9. Linux 下的截屏并编辑的工具介绍
  10. 解决Exception in thread “main“ org.apache.spark.SparkException: When running with master ‘yarn‘ either