0

我在 Spring 应用程序中使用 hazelcast 进行会话复制,这是一个 mvc 应用程序。我有一个单独的应用程序,它是一个 api 网关(弹簧云网关),它具有 hazelcast 客户端,能够从 mvc 应用程序读取 hazelcast 会话详细信息。

Spring session 以以下格式将 session 详细信息存储在 hazelcast 中:

session Id => MapSession
                          -> id = "xyz"
                          -> sessionAttrs 
                                          -> session attributes set if any
                                          -> SPRING_SECURITY_CONTEXT = SecurityContextImpl

我的 hazelcast 客户端尝试使用会话 ID 读取会话映射时

hazelcastInstance.getMap("spring:session:sessions").get(sessionId)

失败并出现错误HazelcastSerializationException,导致SecurityContextImpl.class not found。这个类不会出现在 Spring Cloud Gateway 应用程序中,因为它是响应式的。我不关心网关端的 SPRING_SECURITY_CONTEXT 属性。我只需要我设置的其他会话属性。那么是否可以忽略 SecurityContextImpl 并仍然反序列化 MapSession?请帮忙。

4

1 回答 1

0

Hazelcast 支持为任何类(包括类)定义自定义序列化程序java.io.Serializable。您可以为MapSession类和跳过阅读SPRING_SECURITY_CONTEXT属性注册自定义序列化程序。

static class MapSessionSerializer implements StreamSerializer<MapSession> {

    @Override
    public void write(ObjectDataOutput out, MapSession object) throws IOException {
        // write attributes
    }

    @Override
    public MapSession read(ObjectDataInput in) throws IOException {
        MapSession mapSession = new MapSession();
        // read attributes
        return mapSession;
    }

    @Override
    public int getTypeId() {
        return typeId;
    }

    @Override
    public void destroy() {
    }
}

[...]

SerializerConfig serializerConfig = new SerializerConfig()
    .setTypeClass(MapSession.class)
    .setImplementation(new MapSessionSerializer());

config.getSerializationConfig().addSerializerConfig(serializerConfig);

有关更多信息,请参阅Hazelcast 自定义序列化部分。

于 2018-10-09T06:19:43.787 回答