Skip to content

Commit

Permalink
Merge pull request #85 from gjhuai/master
Browse files Browse the repository at this point in the history
修复issues#84
  • Loading branch information
alexxiyang authored Feb 14, 2019
2 parents e2f4cdd + fa6eda5 commit d57b697
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.crazycake.shiro.serializer;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MultiClassLoaderObjectInputStream extends ObjectInputStream {
private static Logger log = LoggerFactory.getLogger(MultiClassLoaderObjectInputStream.class);

MultiClassLoaderObjectInputStream(InputStream str) throws IOException {
super(str);
}

@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
String name = desc.getName();
//log.debug("resolveClass:"+name);

try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return Class.forName(name, false, cl);
} catch (Throwable ex) {
log.debug(ex.getMessage());
// Cannot access thread context ClassLoader - falling back...
}

try {
// No thread context class loader -> use class loader of this class.
ClassLoader cl = MultiClassLoaderObjectInputStream.class.getClassLoader();
return Class.forName(name, false, cl);
} catch (Throwable ex) {
log.debug(ex.getMessage());
// Cannot access thread context ClassLoader - falling back...
}

// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
ClassLoader cl = ClassLoader.getSystemClassLoader();
return Class.forName(name, false, cl);
} catch (Throwable ex) {
log.debug(ex.getMessage());
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}

return super.resolveClass(desc);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.crazycake.shiro.serializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.crazycake.shiro.exception.SerializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class ObjectSerializer implements RedisSerializer<Object> {
private static Logger logger = LoggerFactory.getLogger(ObjectSerializer.class);
private static Logger log = LoggerFactory.getLogger(ObjectSerializer.class);

public static final int BYTE_ARRAY_OUTPUT_STREAM_SIZE = 128;

Expand Down Expand Up @@ -45,7 +50,7 @@ public Object deserialize(byte[] bytes) throws SerializationException {

try {
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
ObjectInputStream objectInputStream = new MultiClassLoaderObjectInputStream(byteStream);
result = objectInputStream.readObject();
} catch (IOException e) {
throw new SerializationException("deserialize error", e);
Expand Down

0 comments on commit d57b697

Please sign in to comment.