简体   繁体   中英

Partially deserialize the spring session object stored in hazelcast

I am using hazelcast for session replication in spring application which is an mvc app. I have a separate app which is an api gateway(spring cloud gateway) which has hazelcast client to be able to read hazelcast session details from mvc app.

Spring session stores session details in hazelcast in following format:

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

My hazelcast client when tries to read the session map using session id

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

fails with error HazelcastSerializationException , cause SecurityContextImpl.class not found . This class will not be present in spring cloud gateway app as it is reactive one. I don't care about about SPRING_SECURITY_CONTEXT attribute at gateway side. I just need other session attributes that I set. So is it possible to ignore SecurityContextImpl and still deserialize MapSession? please help.

Hazelcast supports defining a custom serializer for any class, including java.io.Serializable classes. You can register a custom serializer for MapSession class and skip reading SPRING_SECURITY_CONTEXT attribute.

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);

See Hazelcast custom serialization section for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM