我正在尝试X509CertificateObject
使用org.apache.commons.lang3.SerializationUtils
. 显然,反序列化对象的类型 ( sun.security.x509.X509CertImpl
) 与原始序列化对象 ( org.bouncycastle.jce.provider.X509CertificateObject
) 不同。因此铸造失败。我究竟做错了什么?
public static void test(X509CertificateObject certObj) {
byte[] serializedObj;
Object deSerializedObj;
X509CertificateObject deSerializedCertObj;
X509Certificate deSerializedCert;
System.out.println("certObj type: " + certObj.getClass().getName());
serializedObj = SerializationUtils.serialize(certObj);
deSerializedObj = SerializationUtils.deserialize(serializedObj);
System.out.println("deSerializedObj type: " + deSerializedObj.getClass().getName());
deSerializedCert = (X509Certificate) deSerializedObj;
System.out.println("deSerializedCert type: " + deSerializedCert.getClass().getName());
deSerializedCertObj = (X509CertificateObject) deSerializedObj;
System.out.println("deSerializedCertObj type: " + deSerializedCertObj.getClass().getName());
}
结果是:
certObj type: org.bouncycastle.jce.provider.X509CertificateObject
deSerializedObj type: sun.security.x509.X509CertImpl
deSerializedCert type: sun.security.x509.X509CertImpl
最后在
java.lang.ClassCastException: sun.security.x509.X509CertImpl cannot be cast to org.bouncycastle.jce.provider.X509CertificateObject
at Test.test(Test.java:1010)
at Test.main(Test.java:153)
这是因为 的上层类X509CertificateObject
是X509Certificate
抽象的和/或因为X509CertificateObject
没有定义自己的serialVersionUID
?