no valid constructor,这个错误是反序列化导致的,第一次正常登录的时候是shiro缓存序列化后存入到redis里,第二次登录账号从redis里取shiro缓存信息时不能反序列化取出缓存,就出这个错误:no valid constructor要解决取缓存反序列化的问题,还是要实现ByteSource接口。
把SimpleByteSource里面的全部方法和属性 复制到继承ByteSource接口的类上
1.自定义的随机盐转化 实现ByteSource接口
package com.hzw.shiro;
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;
//自定义随机盐转化
public class MyBateSource implements ByteSource,Serializable{
private byte[] bytes;
private String cachedHex;
private String cachedBase64;
public MyBateSource(){
}
public MyBateSource(byte[] bytes) {
this.bytes = bytes;
}
public MyBateSource(char[] chars) {
this.bytes = CodecSupport.toBytes(chars);
}
public MyBateSource(String string) {
this.bytes = CodecSupport.toBytes(string);
}
public MyBateSource(ByteSource source) {
this.bytes = source.getBytes();
}
public MyBateSource(File file) {
this.bytes = (new MyBateSource.BytesHelper()).getBytes(file);
}
public MyBateSource(InputStream stream) {
this.bytes = (new MyBateSource.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;
}
public byte[] getBytes() {
return this.bytes;
}
public boolean isEmpty() {
return this.bytes == null || this.bytes.length == 0;
}
public String toHex() {
if (this.cachedHex == null) {
this.cachedHex = Hex.encodeToString(this.getBytes());
}
return this.cachedHex;
}
public String toBase64() {
if (this.cachedBase64 == null) {
this.cachedBase64 = Base64.encodeToString(this.getBytes());
}
return this.cachedBase64;
}
public String toString() {
return this.toBase64();
}
public int hashCode() {
return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
}
public 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);
}
}
}
2.替换随机盐转化 new MyBateSource(user.getSalt())
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("================");
//获取身份信息
String principal = (String)token.getPrincipal();
UserService userService =(UserService)AppliactionContextUtils.getBean("userService");
User user = userService.findByUserName(principal);
System.out.println(principal);
if (!ObjectUtils.isEmpty(user)){
return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),new MyBateSource(user.getSalt()),this.getName());
}
return null;
}
测试 可以看出登录账号从redis里取shiro缓存信息