我有一个使用PasswordEncryptor作为其字段之一的类org.jasypt.util.password.StrongPasswordEncryptor,因为我试图使该应用程序“可群集”,因此所有类都需要可序列化以进行会话复制,但是每当访问PasswordEncryptor时,我都会遇到以下例外:

Caused by: java.io.NotSerializableException: org.jasypt.util.password.StrongPasswordEncryptor
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:891)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1063)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:885)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1063)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:885)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:680)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:62)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:119)
    at org.jboss.as.clustering.SimpleMarshalledValue.getBytes(SimpleMarshalledValue.java:74)
    at org.jboss.as.clustering.SimpleMarshalledValue.writeObject(SimpleMarshalledValue.java:172)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_34]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.6.0_34]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.6.0_34]
    at java.lang.reflect.Method.invoke(Method.java:622) [rt.jar:1.6.0_34]
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:175)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1007)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:885)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:62)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:119)
    at org.infinispan.marshall.MarshallUtil.marshallMap(MarshallUtil.java:60)
    at org.infinispan.marshall.exts.MapExternalizer.writeObject(MapExternalizer.java:63)
    at org.infinispan.marshall.exts.MapExternalizer.writeObject(MapExternalizer.java:47)
    at org.infinispan.marshall.jboss.ExternalizerTable$ExternalizerAdapter.writeObject(ExternalizerTable.java:406)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:145)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:62)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:119)
    at org.infinispan.atomic.AtomicHashMap$Externalizer.writeObject(AtomicHashMap.java:229)
    at org.infinispan.atomic.AtomicHashMap$Externalizer.writeObject(AtomicHashMap.java:226)
    at org.infinispan.marshall.jboss.ExternalizerTable$ExternalizerAdapter.writeObject(ExternalizerTable.java:406)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:145)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:62)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:119)
    at org.infinispan.marshall.jboss.AbstractJBossMarshaller.objectToObjectStream(AbstractJBossMarshaller.java:86)
    at org.infinispan.marshall.VersionAwareMarshaller.objectToObjectStream(VersionAwareMarshaller.java:151)
    at org.infinispan.marshall.AbstractDelegatingMarshaller.objectToObjectStream(AbstractDelegatingMarshaller.java:44)
    at org.infinispan.marshall.MarshalledValue.serialize0(MarshalledValue.java:119)
    ... 117 more
Caused by: an exception which occurred:
    in field spe
    in field bean
    in object java.util.HashMap@b629b463
    in object org.jboss.as.clustering.SimpleMarshalledValue@b629b463
    in object org.infinispan.util.FastCopyHashMap@43ad73a2
    in object org.infinispan.atomic.AtomicHashMap@4fd181fe
    in object org.infinispan.marshall.MarshalledValue@4fd181fe
    in object org.infinispan.commands.write.PutKeyValueCommand@ce32d716
    in object org.infinispan.commands.tx.PrepareCommand@293098b


我已经尝试过将字段标记为transient,如下所示:

import java.io.Serializable;

import org.jasypt.util.password.StrongPasswordEncryptor;

public class PasswordEncryptor implements Serializable {

    private static final long serialVersionUID = 1L;

    //Need to mark transient as its not serializable
    private transient StrongPasswordEncryptor spe = new StrongPasswordEncryptor();

    public String encrypt(String password){
        return spe.encryptPassword(password);
    }

    public boolean isPasswordCorrect(String enteredPassword, String passwordHash){
        return spe.checkPassword(enteredPassword, passwordHash);
    }

}


而且我不能使用包装器将StrongPasswordEncryptor子类化,因为它是最终的

还有其他方法吗? (最好不引入任何其他库)

最佳答案

从Infinispan的角度来看,控制类如何进行序列化(尤其是在类是最终类或无法修改类时)的最佳方法是为要序列化的类提供自己的Infinispan外在器。 Infinispan的用户指南包含有关how to Plug Infinispan with User-Defined Externalizers的整章内容,解释了其优点,其中包括减少有效负载,加快序列化并解决无法修改要序列化的类时的问题。除了文档中的示例,Infinispan source code (ASL2)还包含大量的Externalizer示例,可以满足您的需要。

更具体地说,用于PasswordEncryptor的Externalizer无需在writeObject中编写任何内容,而只需在readObject中实例化PasswordEncryptor。

10-08 14:11