Shiro with kryo
I use the kryo as the Serializer in the project with spring session and redis.
when I wirte like below:
public static void main(String[] args) {
MyShiroRealm myShiroRealm = new MyShiroRealm();
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
"username", //用户名
"passwd", //密码
myShiroRealm.getName()
);
PrincipalCollection principals = simpleAuthenticationInfo.getPrincipals();
KryoRedisSerializer<Object> serializer = new KryoRedisSerializer<>();
byte[] serialize = serializer.serialize(principals);
Object deserialize = serializer.deserialize(serialize); //1.throw a NullPointException
}
Object deserialize = serializer.deserialize(serialize) it will throw a NullPointException
i debug it found the shiro Object Class named:org.apache.shiro.subject.SimplePrincipalCollection can be
serialize but can't deserialize!
why?
I exchange the serializer to JDK..it can be OK! it confused me all day! the shiro version: 1.3.2 the kyro version: 4.0.0
192.168.5.152:7001> hgetall spring:session:CRM:sessions:18bd3eec-8dd3-4484-a892-31fe9a32b5a6
-> Redirected to slot [14637] located at 192.168.5.152:7002
1) "lastAccessedTime"
2) "\t\xee\xdb\xe9\xa2\xf3W"
3) "creationTime"
4) "\t\x9e\xa4\xe5\xa2\xf3W"
5) "sessionAttr:shiroSavedRequest"
6) "\x01\x00org.apache.shiro.web.util.SavedReques\xf4\x01\x01GE\xd4\x00\x01/favicon.ic\xef"
7) "sessionAttr:org.apache.shiro.subject.support.DelegatingSubject.RUN_AS_PRINCIPALS_SESSION_KEY"
8) "\x00"
9) "sessionAttr:org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY"
10) "\x01\x00org.apache.shiro.subject.SimplePrincipalCollectio\xee\x01\x01\x01java.util.LinkedHashMa\xf0\x01\x01\x03\x01com.youboy.user.shiro.realm.MyShiroRealm_\xb0\x01\x02java.util.LinkedHashSe\xf4\x01\x01\x01\x03com.xxxx.user.dto.user.LoginUse\xf2\x01\x00\x00\x01kf_tes\xf4\x01E10ADC3949BA59ABBE56E057F20F883\xc5\x00\x00\x01\xc0\x19"
11) "sessionAttr:org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY"
12) "\x05\x01"
13) "sessionAttr:org.apache.shiro.web.session.HttpServletSession.HOST_SESSION_KEY"
14) "\x03\x01127.0.0.\xb1"
15) "maxInactiveInterval"
16) "\x02\x90\x1c"
the Exception it throw with the key in redis:
sessionAttr:org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KE
the KryoRedisSerializer important code:
private static final byte[] EMPTY_ARRAY = new byte[]{0};
@Override
public byte[] serialize(Object t) throws SerializationException {
if (t == null) {
return EMPTY_ARRAY;
}
Kryo kryo = getKryo();
Output output = new Output(DEFAULT_BUFFER);
kryo.writeClassAndObject(output, t);
return output.toBytes();
}
@SuppressWarnings("unchecked")
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null) {
bytes = EMPTY_ARRAY;
}
Kryo kryo = getKryo();
Input input = new Input(bytes);
T t = (T) kryo.readClassAndObject(input);
return t;
}
my fault:
I set the strategy as the kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
I change to user the kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
solve the question!