1616
1717package org .springframework .session .data .mongo ;
1818
19- import java .io .IOException ;
20- import java .util .Collections ;
21- import java .util .Date ;
22- import java .util .HashMap ;
23-
2419import com .fasterxml .jackson .annotation .JsonAutoDetect ;
2520import com .fasterxml .jackson .annotation .JsonCreator ;
2621import com .fasterxml .jackson .annotation .JsonProperty ;
3732import org .bson .Document ;
3833import org .bson .json .JsonMode ;
3934import org .bson .json .JsonWriterSettings ;
40-
4135import org .springframework .data .mongodb .core .query .Criteria ;
4236import org .springframework .data .mongodb .core .query .Query ;
4337import org .springframework .lang .Nullable ;
4438import org .springframework .security .jackson2 .SecurityJackson2Modules ;
4539import org .springframework .session .FindByIndexNameSessionRepository ;
4640import org .springframework .util .Assert ;
4741
42+ import java .io .IOException ;
43+ import java .util .Collections ;
44+ import java .util .Date ;
45+ import java .util .HashMap ;
46+
4847/**
4948 * {@code AbstractMongoSessionConverter} implementation using Jackson.
5049 *
5150 * @author Jakub Kubrynski
5251 * @author Greg Turnquist
5352 * @author Michael Ruf
53+ * @author TiQuan Hu
5454 * @since 1.2
5555 */
5656public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
@@ -70,17 +70,27 @@ public JacksonMongoSessionConverter() {
7070 }
7171
7272 public JacksonMongoSessionConverter (Iterable <Module > modules ) {
73-
7473 this .objectMapper = buildObjectMapper ();
7574 this .objectMapper .registerModules (modules );
7675 }
7776
7877 public JacksonMongoSessionConverter (ObjectMapper objectMapper ) {
79-
80- Assert .notNull (objectMapper , "ObjectMapper can NOT be null!" );
78+ Assert .notNull (objectMapper , "ObjectMapper can not be null!" );
8179 this .objectMapper = objectMapper ;
8280 }
8381
82+ public JacksonMongoSessionConverter (ObjectMapper objectMapper , boolean copyToUse ) {
83+ Assert .notNull (objectMapper , "ObjectMapper can not be null!" );
84+ if (!copyToUse ) {
85+ configureObjectMapper (objectMapper );
86+ this .objectMapper = objectMapper ;
87+ return ;
88+ }
89+ var objectMapperCopy = objectMapper .copy ();
90+ configureObjectMapper (objectMapperCopy );
91+ this .objectMapper = objectMapperCopy ;
92+ }
93+
8494 @ Nullable
8595 protected Query getQueryForIndex (String indexName , Object indexValue ) {
8696
@@ -93,9 +103,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) {
93103 }
94104
95105 private ObjectMapper buildObjectMapper () {
96-
97106 ObjectMapper objectMapper = new ObjectMapper ();
107+ this .configureObjectMapper (objectMapper );
108+ return objectMapper ;
109+ }
98110
111+ private void configureObjectMapper (ObjectMapper objectMapper ) {
99112 // serialize fields instead of properties
100113 objectMapper .setVisibility (PropertyAccessor .ALL , JsonAutoDetect .Visibility .NONE );
101114 objectMapper .setVisibility (PropertyAccessor .FIELD , JsonAutoDetect .Visibility .ANY );
@@ -108,8 +121,6 @@ private ObjectMapper buildObjectMapper() {
108121 objectMapper .registerModules (SecurityJackson2Modules .getModules (getClass ().getClassLoader ()));
109122 objectMapper .addMixIn (MongoSession .class , MongoSessionMixin .class );
110123 objectMapper .addMixIn (HashMap .class , HashMapMixin .class );
111-
112- return objectMapper ;
113124 }
114125
115126 @ Override
0 commit comments