日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource at java.io.ObjectOutputStr

發(fā)布時間:2025/1/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource at java.io.ObjectOutputStr 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

異常背景:

做shiro+salt+redis整合時,一直報不能序列化錯誤,不開啟redis時還是正常的,開啟redis后就出錯。


問題描述:

自定義CustomerRealm extends AuthorizingRealm中認(rèn)證方法中代碼,問題出在ByteSource.Util.bytes(user.getSalt().getBytes())加入的隨機鹽部分不能序列化!(這也算是shiro中的一個詬病)!另外如果要做授權(quán),實體類比如User、Role、Permission這些都要實現(xiàn)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來解決序列化問題,其中所有代碼全是復(fù)制自SimpleByteSource.java類(因為該類已經(jīng)實現(xiàn)了ByteSource,雖然大部分方法都沒用,但還是要加上),然后加入無參構(gòu)造,再稍進行修改即可。

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實現(xiàn)自定義序列化接口 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);}} }

總結(jié)

以上是生活随笔為你收集整理的java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource at java.io.ObjectOutputStr的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。