java—如何自定义configurableobjectinputstream并使用扩展的lookaheadobjectinputstream

vohkndzv  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(502)

我们的技术栈是spring,jpa使用hibernate,sqlserver。我们使用jackson对rest实现进行json序列化/反序列化。几个问题:

In our model class - we have implements Serializable
Jackson objectmapper which spring uses also implements Serializable.

如我们所见,hibernate具有二进制(java)序列化,jackson是基于文本(json)的序列化。
问题1:当spring和jpa/hibernate实现时,hibernate是否使用configurableobjectinputstream
问题2:jackson是否使用configurableobjectinputstream
问题3:如何通知spring使用lookaheadobjectinputstream而不是configurableobjectinputstream(参见下面的详细信息)。
我们希望加强java的objectinputstream的所有使用,并确保在我们的应用程序中没有反序列化问题
灵感来自https://cheatsheetseries.owasp.org/cheatsheets/deserialization_cheat_sheet.html#harden-您自己的javaioobjectinputstream,我们想定义一个lookaheadobjectinputstream,它将扩展configurableobjectinputstream,在这里我们可以定义自己的可以解析的应用程序类。
如何通知spring使用lookaheadobjectinputstream而不是configurableobjectinputstream

public class LookAheadObjectInputStream extends ConfigurableObjectInputStream {{

    public LookAheadObjectInputStream(InputStream inputStream) throws IOException {
        super(inputStream);
    }

    /**
    * Only deserialize instances of our expected Bicycle class
    */
    @Override
    protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
        if (!desc.getName().equals(Bicycle.class.getName())) {
            throw new InvalidClassException("Unauthorized deserialization attempt", desc.getName());
        }
        return super.resolveClass(desc);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题