java序列化辨析
java对象序列化是一个神奇的功能,它通过让对象实现Serializable接口,并将其传递给ObjectOutputStream的writeObject方法,就能得到该对像。writeObject是怎么样实现这个功能的呢?下面分析一下这个方法的实现 writeObject首先是获取当前序列化对象的的类信息,调用的是ObjectStreamClass的lookup方法 [code="java"] static ObjectStreamClass lookup(Class cl, boolean all) { .... if (entry == null) { try { entry = new ObjectStreamClass(cl);//创建ObjectStreamClass } catch (Throwable th) { entry = th; } if (future.set(entry)) { Caches.localDescs.put(key, new SoftReference(entry)); } else { // nested lookup call already set future entry = future.get(); } } if (entry instanceof ObjectStreamClass) { return (ObjectStreamClass) entry; } else if (entry instanceof RuntimeException) { throw (RuntimeException) entry; } else if (entry instanceof Error) { throw (Error) entry; } else { throw new InternalError("unexpected entry: " + entry); } } [/code] 创建ObjectStreamClass会读取序列化所需要的一些类信息,代码如下 [code="java"] private ObjectStreamClass(final Class cl) { this.cl = cl; name = cl.getName(); isProxy = Proxy.isProxyClass(cl);//是否proxy类 isEnum = Enum.class.isAssignableFrom(cl);//是否Enum serializable = Serializable.class.isAssignableFrom(cl);//否Serializable externalizable = Externalizable.class.isAssignableFrom(cl);//是否Externalizable Class superCl = cl.getSuperclass(); superDesc = (superCl != null) ? lookup(superCl, false) : null; //获取父类及父类信息 localDesc = this; if (serializable) {//如果serializable AccessController.doPrivileged(new PrivilegedAction() { public Object run() { if (isEnum) { //Enum生成suid为0且将fields设置为空ObjectStreamField的数组 suid = Long.valueOf(0); fields = NO_FIELDS; return null; } if (cl.isArray()) { //Array fields设置为空ObjectStreamField的数组 fields = NO_FIELDS; return null; } suid = getDeclaredSUID(cl);//获取类中定义的suid try { fields = [color=red]getSerialFields[/color](cl); //如果是Serializable且!Externalizable.class.isAssignableFrom(cl) && // !Proxy.isProxyClass(cl) && // !cl.isInterface() //则获取类中要定义的序列化的属性域 //可以通过serialPersistentFields来定义要序列化的属性域,或者非static 非TRANSIENT computeFieldOffsets();//计算field占用空间的大小,属性个数 } catch (InvalidClassException e) { serializeEx = deserializeEx = e; fields = NO_FIELDS; } if (externalizable) { //实现了externalizable 获取构造器 cons = getExternalizableConstructor(cl); } else { //未实现externalizable 获取构造器 私有的writeObject readbject readObjectNoData cons = getSerializableConstructor(cl); writeObjectMethod = getPrivateMethod(cl, "writeObject", new Class[] { ObjectOutputStream.class }, Void.TYPE); readObjectMethod = getPrivateMethod(cl, "readObject", new Class[] { ObjectInputStream.class }, Void.TYPE); readObjectNoDataMethod = getPrivateMethod( cl, "readObjectNoData", null, Void.TYPE); hasWriteObjectData = (writeObjectMethod != null); } //获取 writeReplace readResolve writeReplaceMethod = getInheritableMethod( cl, "writeReplace", null, Object.class); readResolveMethod = getInheritableMethod( cl, "readResolve", null, Object.class); return null; } }); } else {//未实现serializable suid为0且将fields设置为空ObjectStreamField的数组 suid = Long.valueOf(0); fields = NO_FIELDS; } try { fieldRefl = getReflector(fields, this); } catch (InvalidClassException ex) { // field mismatches impossible when matching local fields vs. self throw new InternalError(); } if (deserializeEx == null) { if (isEnum) { deserializeEx = new InvalidClassException(name, "enum type"); } else if (cons == null) { deserializeEx = new InvalidClassException( name, "no valid constructor"); } } for (int i = 0; i < fields.length; i++) { if (fields[i].getField() == null) { defaultSerializeEx = new InvalidClassException( name, "unmatched serializable field(s) declared"); } } } [/code] 最后则是根据要序列化的Object类型调用相应的方法进行序列化 看看对于普通对象的实现代码 private void writeOrdinaryObject(Object obj, ObjectStreamClass desc, boolean unshared) throws IOException { if (extendedDebugInfo) { debugInfoStack.push( (depth == 1 ? "root " : "") + "object (class \"" + obj.getClass().getName() + "\", " + obj.toString() + ")"); } try { desc.checkSerialize(); //检查是否可序列化 bout.writeByte(TC_OBJECT); writeClassDesc(desc, false); handles.assign(unshared ? null : obj); if (desc.isExternalizable() && !desc.isProxy()) { //Externalizable writeExternalData中会调用对象实现的writeExternal方法来完成写入 writeExternalData((Externalizable) obj); } else { //writeSerialData方法中会判断序列化对象有无writeObject方法 writeSerialData(obj, desc); } } finally { if (extendedDebugInfo) { debugInfoStack.pop(); } } } 通过以上源码分析可以知道,类必须实现Serializable或者Externalizable接口,通过实现Externalizable接口 编写私有的writeObject或者writeReplace方法,给属性添加transient或者serialPersistentFileds来控制序列化的行为 如下这个列子中我们编写私用的writeObject和readObject以帮助我们序列化中我们需要混淆totalValue的值还在反序列化中才还原 [code="java"] class Person implements Serializable{ public String name; public Person spouse; @SuppressWarnings("rawtypes") public ArrayList children=new ArrayList(); public double totalValue; private void writeObject(ObjectOutputStream oos) throws IOException{ totalValue=totalValue*2-1; oos.defaultWriteObject(); } private void readObject(ObjectInputStream ois) throws Exception{ totalValue=(totalValue-1)/2; ois.defaultReadObject(); } } } [/code] 下面的代码给出了 serialPersistentFields的声明示例,即只有name这个域是要被序列化的。而当一个属性同时被serialPersistentFields和transient声明 则以serialPersistentFields为主 [code="java"] class Person implements Serializable{ public String name; public Person spouse; @SuppressWarnings("rawtypes") public ArrayList children=new ArrayList(); public double totalValue; private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("name", String.class) }; private void writeObject(ObjectOutputStream oos) throws IOException{ totalValue=totalValue*2-1; oos.defaultWriteObject(); } private void readObject(ObjectInputStream ois) throws Exception{ totalValue=(totalValue-1)/2; ois.defaultReadObject(); } } [/code] [b] 序列化的安全性[/b] Java对象序列化之后的内容格式是公开的。所以可以很容易的从中提取出各种信息。从实现的角度来说,可以从不同的层次来加强序列化的安全性。 [list] [*]对序列化之后的流进行加密。这可以通过CipherOutputStream来实现。 [*] 实现自己的writeObject和readObject方法,在调用defaultWriteObject之前,先对要序列化的域的值进行加密处理。 [*] 使用一个SignedObject或SealedObject来封装当前对象,用SignedObject或SealedObject进行序列化。 [*] 在从流中进行反序列化的时候,可以通过ObjectInputStream的registerValidation方法添加ObjectInputValidation接口的实现,用来验证反序列化之后得到的对象是否合法。 [/list]