shiro 使用缓存时出现:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource
2017-02-09 14:53
686 查看
在学习Shiro使用缓存时,出现:
java.io.NotSerializableException:org.apache.shiro.util.SimpleByteSource异常,开启debug会提示:
ERROR [authentication.data] - Disk Write of test failed: 错误。
出现这种情况是因为:SimpleByteSource没有是实现Serializable接口
解决办法:自定义一个类继承SimpleByteSource实现Serializable接口
当然也可以实现ByteSource接口和Serializable接口,但是实现ByteSource接口需要实现其方法,不方便。
自定义一个SimpleByteSource 类继承继承SimpleByteSource实现Serializable接口。
然后创建工具类ByteSourceUtils
在自定义realm的认证方法中:
java.io.NotSerializableException:org.apache.shiro.util.SimpleByteSource异常,开启debug会提示:
ERROR [authentication.data] - Disk Write of test failed: 错误。
出现这种情况是因为:SimpleByteSource没有是实现Serializable接口
解决办法:自定义一个类继承SimpleByteSource实现Serializable接口
当然也可以实现ByteSource接口和Serializable接口,但是实现ByteSource接口需要实现其方法,不方便。
自定义一个SimpleByteSource 类继承继承SimpleByteSource实现Serializable接口。
import java.io.Serializable; public class SimpleByteSource extends org.apache.shiro.util.SimpleByteSource implements Serializable{ private static final long serialVersionUID = 5528101080905698238L; public SimpleByteSource(byte[] bytes) { super(bytes); // TODO 自动生成的构造函数存根 } }
然后创建工具类ByteSourceUtils
import org.apache.shiro.util.ByteSource; public class ByteSourceUtils{ public static ByteSource bytes(byte[] bytes){ return new SimpleByteSource(bytes); } public static ByteSource bytes(String arg0){ return new SimpleByteSource(arg0.getBytes()); } }
在自定义realm的认证方法中:
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String)token.getPrincipal(); User user = userService.getByUsername(username); if(user == null) { throw new UnknownAccountException();//没找到帐号 } if(Boolean.TRUE.equals(user.isLocked())) { throw new LockedAccountException(); //帐号锁定 } //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配 SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( user.getUsername(), //用户名 user.getPassword(), //密码 ByteSourceUtils.bytes(user.getSalt()),//salt getName() //realm name ); return authenticationInfo; }
相关文章推荐
- shiro 使用缓存时出现:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource
- shiro 集成缓存中遇到 java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource
- storm中使用log出现java.lang.RuntimeException: java.io.NotSerializableException: java.util.logging.Logger
- 使用缓存时出现java.io.NotSerializableException:xxx.xxx.xxx.Bean解决办法
- Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
- Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils
- Caused by: java.io.NotSerializableException: org.apache.spark.SparkContext
- java.io.NotSerializableException: org.apache.log4j.Logger
- spark1.4 读取hbase 0.96 报错 java.io.NotSerializableException: org.apache.hadoop.hbase.io.ImmutableBytes
- java.io.NotSerializableException: org.hibernate.util.MarkerObject解决办法
- Maven使用tomcat7-maven-plugin插件run时出现错误: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
- java.lang.ClassNotFoundException: org.apache.http.util.Args
- org.apache.tomcat.util.bcel.classfile.ClassFormatException: It is not a Java .class file
- java.io.NotSerializableException: java.util.AbstractList$SubAbstractListRandomAccess
- velocity使用中出现org.apache.velocity.exception.ResourceNotFoundException的解决办法
- 使用JSONObject报错:Java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeEx
- 【shiro】报错:Caused by: java.lang.ClassNotFoundException: org.apache.shiro.spring.LifecycleBeanPostProcessor
- Oozie时bin/oozied.sh start或bin/oozied.sh run出现Bootstrap进程无法启动,http://bigdatamaster:11000/oozie界面也无法打开?E0103: Could not load service classes, java.lang.ClassNotFoundException: Class org.apache.oozie.ser
- 如何解决jdbc中出现的java.io.NotSerializableException
- org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.util.HashMap