When using Shiro to cache user identity information: Java.io.NotSerializableException:org.apache.shiro.util.SimpleByteSource

Source: Internet
Author: User
Tags base64

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.