From 5c3b13834c2fb2b37d80fe98ec67fd21c3386cfc Mon Sep 17 00:00:00 2001 From: xiao Date: Tue, 3 Sep 2024 17:01:30 +0800 Subject: [PATCH] Optimize the usage of JacksonMongoSessionConverter to prevent duplicate MongoSession Document saves when a custom ObjectMapper is provided.https://github.com/spring-projects/spring-session/issues/3185 --- .../mongo/JacksonMongoSessionConverter.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java index 986634200..cf7e615a6 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java @@ -16,11 +16,6 @@ package org.springframework.session.data.mongo; -import java.io.IOException; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; - import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; @@ -37,7 +32,6 @@ import org.bson.Document; import org.bson.json.JsonMode; import org.bson.json.JsonWriterSettings; - import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.lang.Nullable; @@ -45,16 +39,21 @@ import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.util.Assert; +import java.io.IOException; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; + /** * {@code AbstractMongoSessionConverter} implementation using Jackson. * * @author Jakub Kubrynski * @author Greg Turnquist * @author Michael Ruf + * @author TiQuan Hu * @since 1.2 */ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter { - private static final Log LOG = LogFactory.getLog(JacksonMongoSessionConverter.class); private static final String ATTRS_FIELD_NAME = "attrs."; @@ -76,11 +75,22 @@ public JacksonMongoSessionConverter(Iterable modules) { } public JacksonMongoSessionConverter(ObjectMapper objectMapper) { - - Assert.notNull(objectMapper, "ObjectMapper can NOT be null!"); + Assert.notNull(objectMapper, "ObjectMapper can not be null!"); this.objectMapper = objectMapper; } + public JacksonMongoSessionConverter(ObjectMapper objectMapper, boolean copyToUse) { + Assert.notNull(objectMapper, "ObjectMapper can not be null!"); + if (!copyToUse) { + configureObjectMapper(objectMapper); + this.objectMapper = objectMapper; + return; + } + var objectMapperCopy = objectMapper.copy(); + configureObjectMapper(objectMapperCopy); + this.objectMapper = objectMapperCopy; + } + @Nullable protected Query getQueryForIndex(String indexName, Object indexValue) { @@ -93,9 +103,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) { } private ObjectMapper buildObjectMapper() { - ObjectMapper objectMapper = new ObjectMapper(); + this.configureObjectMapper(objectMapper); + return objectMapper; + } + private void configureObjectMapper(ObjectMapper objectMapper) { // serialize fields instead of properties objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); @@ -108,8 +121,6 @@ private ObjectMapper buildObjectMapper() { objectMapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader())); objectMapper.addMixIn(MongoSession.class, MongoSessionMixin.class); objectMapper.addMixIn(HashMap.class, HashMapMixin.class); - - return objectMapper; } @Override @@ -154,7 +165,7 @@ private static class MongoSessionMixin { @JsonCreator MongoSessionMixin(@JsonProperty("_id") String id, - @JsonProperty("intervalSeconds") long maxInactiveIntervalInSeconds) { + @JsonProperty("intervalSeconds") long maxInactiveIntervalInSeconds) { } }