zoukankan      html  css  js  c++  java
  • 使用shiro缓存用户身份信息的时候报:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource

    最近在使用shiro缓存用户的身份信息的时候,报了simpleByteSource不能序列化,跟进源码一看,原来这个类没有实现序列化的接口,但是我在缓存身份信息的实现又要用到这个类,解决方法:重写一个类,模仿着SimpleByteSoure这个类,写个自己的类。并且实现序列化接口即可·。

     1 package com.dzf.shiro;
     2 
     3 import org.apache.shiro.codec.Base64;
     4 import org.apache.shiro.codec.CodecSupport;
     5 import org.apache.shiro.codec.Hex;
     6 import org.apache.shiro.util.ByteSource;
     7 
     8 import java.io.Serializable;
     9 import java.util.Arrays;
    10 
    11 /**
    12  * <p>
    13  *     为了解决redis序列化的问题,SimpleByteSource没有实现序列化接口
    14  *     在传入simpleAuthenticationInfo()的时候,缓存用户信息会出现序列化异常
    15  * </p>
    16  * @author dingzf
    17  * @date 2018/3/7
    18  * @time 21:54
    19  */
    20 public class ShiroByteSource implements ByteSource, Serializable {
    21     private static final long serialVersionUID = -6814382603612799610L;
    22     private volatile byte[] bytes;
    23     private String cachedHex;
    24     private String cachedBase64;
    25 
    26     public ShiroByteSource() {
    27 
    28     }
    29 
    30     public ShiroByteSource(String string) {
    31         this.bytes = CodecSupport.toBytes(string);
    32     }
    33 
    34     public void setBytes(byte[] bytes) {
    35         this.bytes = bytes;
    36     }
    37 
    38     @Override
    39     public byte[] getBytes() {
    40         return this.bytes;
    41     }
    42 
    43     @Override
    44     public String toHex() {
    45         if ( this.cachedHex == null ) {
    46             this.cachedHex = Hex.encodeToString(getBytes());
    47         }
    48         return this.cachedHex;
    49     }
    50 
    51     @Override
    52     public String toBase64() {
    53         if ( this.cachedBase64 == null ) {
    54             this.cachedBase64 = Base64.encodeToString(getBytes());
    55         }
    56         return this.cachedBase64;
    57     }
    58 
    59     @Override
    60     public boolean isEmpty() {
    61         return this.bytes == null || this.bytes.length == 0;
    62     }
    63 
    64     @Override
    65     public String toString() {
    66         return toBase64();
    67     }
    68 
    69     @Override
    70     public int hashCode() {
    71         if (this.bytes == null || this.bytes.length == 0) {
    72             return 0;
    73         }
    74         return Arrays.hashCode(this.bytes);
    75     }
    76 
    77     @Override
    78     public boolean equals(Object o) {
    79         if (o == this) {
    80             return true;
    81         }
    82         if (o instanceof ByteSource) {
    83             ByteSource bs = (ByteSource) o;
    84             return Arrays.equals(getBytes(), bs.getBytes());
    85         }
    86         return false;
    87     }
    88 
    89     public static ByteSource of(String string) {
    90         return new ShiroByteSource(string);
    91     }
    92 }

    在自己的实现的realm里面调用如下:

    1 SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), new ShiroByteSource(user.getSalt()), getName());

    以前的写法是:

    1 SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), new SimpleByteSource(user.getSalt()), getName());

    然后就可以解决问题了。



  • 相关阅读:
    使用WCF 测试客户端测试你的WCF服务
    使用Fiddler解析WCF RIA Service传输的数据
    SqlServer中BULK INSERT用法简介
    Silverlight的安全性初探
    使用Apache cxf 和Spring在Tomcat下发布Webservice指南
    Spring 3 MVC And XML Example
    设计与开发 JAXWS 2.0 Web 服务
    如何高效、准确、自动识别网页编码
    Spring 3 MVC And RSS Feed Example
    Spring 3 MVC And JSON Example
  • 原文地址:https://www.cnblogs.com/zfding/p/8525657.html
Copyright © 2011-2022 走看看