Set expireAt properly to support TTL indexing.
This commit is contained in:
committed by
Greg Turnquist
parent
841fcb9cf4
commit
82cf365ba7
@@ -17,6 +17,7 @@ package org.springframework.session.data.mongo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -46,6 +47,7 @@ import com.mongodb.util.JSON;
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Greg Turnquist
|
||||
* @author Michael Ruf
|
||||
* @since 1.2
|
||||
*/
|
||||
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
|
||||
@@ -54,6 +56,7 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
|
||||
|
||||
private static final String ATTRS_FIELD_NAME = "attrs.";
|
||||
private static final String PRINCIPAL_FIELD_NAME = "principal";
|
||||
private static final String EXPIRE_AT_FIELD_NAME = "expireAt";
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@@ -108,6 +111,7 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
|
||||
|
||||
try {
|
||||
DBObject dbSession = (DBObject) JSON.parse(this.objectMapper.writeValueAsString(source));
|
||||
dbSession.put(EXPIRE_AT_FIELD_NAME, source.getExpireAt());
|
||||
dbSession.put(PRINCIPAL_FIELD_NAME, extractPrincipal(source));
|
||||
return dbSession;
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -119,10 +123,14 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
|
||||
@Nullable
|
||||
protected MongoSession convert(Document source) {
|
||||
|
||||
Date expireAt = source.getDate(EXPIRE_AT_FIELD_NAME);
|
||||
source.remove(EXPIRE_AT_FIELD_NAME);
|
||||
String json = source.toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
|
||||
|
||||
try {
|
||||
return this.objectMapper.readValue(json, MongoSession.class);
|
||||
MongoSession mongoSession = this.objectMapper.readValue(json, MongoSession.class);
|
||||
mongoSession.setExpireAt(expireAt);
|
||||
return mongoSession;
|
||||
} catch (IOException e) {
|
||||
LOG.error("Error during Mongo Session deserialization", e);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user