java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource at java.io.ObjectOutputStr
生活随笔
收集整理的這篇文章主要介紹了
java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource at java.io.ObjectOutputStr
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
異常背景:
做shiro+salt+redis整合時,一直報不能序列化錯誤,不開啟redis時還是正常的,開啟redis后就出錯。
問題描述:
自定義CustomerRealm extends AuthorizingRealm中認證方法中代碼,問題出在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來解決序列化問題,其中所有代碼全是復制自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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用@Autowired注入RedisT
- 下一篇: IDEA配置SVN并实现代码版本控制