Recently in the use of Shiro cache user identity information, reported that Simplebytesource can not be serialized, follow-up source A look, originally this class does not implement the serialization interface, but I in the cache identity information implementation and use this class, workaround: Rewrite a class, Imitate the Simplebytesoure class and write your own class. And the implementation of the serialization interface can be.
1 PackageCom.dzf.shiro;2 3 Importorg.apache.shiro.codec.Base64;4 ImportOrg.apache.shiro.codec.CodecSupport;5 ImportOrg.apache.shiro.codec.Hex;6 ImportOrg.apache.shiro.util.ByteSource;7 8 Importjava.io.Serializable;9 Importjava.util.Arrays;Ten One /** A * <p> - * In order to solve the problem of Redis serialization, Simplebytesource does not implement the serialization interface - * When the Simpleauthenticationinfo () is passed in, the cached user information will appear serialized exception the * </p> - * @authorDINGZF - * @date 2018/3/7 - * @time 21:54 + */ - Public classShirobytesourceImplementsBytesource, Serializable { + Private Static Final LongSerialversionuid = -6814382603612799610l; A Private volatile byte[] bytes; at PrivateString Cachedhex; - PrivateString cachedBase64; - - PublicShirobytesource () { - - } in - PublicShirobytesource (String string) { to This. bytes =codecsupport.tobytes (string); + } - the Public voidSetBytes (byte[] bytes) { * This. bytes =bytes; $ }Panax Notoginseng - @Override the Public byte[] GetBytes () { + return This. bytes; A } the + @Override - PublicString Tohex () { $ if( This. Cachedhex = =NULL ) { $ This. Cachedhex =hex.encodetostring (GetBytes ()); - } - return This. Cachedhex; the } - Wuyi @Override the PublicString toBase64 () { - if( This. cachedBase64 = =NULL ) { Wu This. cachedBase64 =base64.encodetostring (GetBytes ()); - } About return This. cachedBase64; $ } - - @Override - Public BooleanIsEmpty () { A return This. bytes = =NULL|| This. Bytes.length = = 0; + } the - @Override $ PublicString toString () { the returntoBase64 (); the } the the @Override - Public inthashcode () { in if( This. bytes = =NULL|| This. Bytes.length = = 0) { the return0; the } About returnArrays.hashcode ( This. bytes); the } the the @Override + Public Booleanequals (Object o) { - if(O = = This) { the return true;Bayi } the if(OinstanceofBytesource) { theBytesource bs =(Bytesource) o; - returnarrays.equals (GetBytes (), bs.getbytes ()); - } the return false; the } the the Public StaticBytesource of (string string) { - return NewShirobytesource (string); the } the}
Within the realm of your own implementation, call the following:
1 New New Shirobytesource (User.getsalt ()), GetName ());
The previous wording was:
1 New New Simplebytesource (User.getsalt ()), GetName ());
Then we can solve the problem.
When using Shiro to cache user identity information: Java.io.NotSerializableException:org.apache.shiro.util.SimpleByteSource