diff --git a/pom.xml b/pom.xml index 69741f8..b0a61b9 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ 1.3 2.9.1 4.12 + 1.18.0 2.18.3 3.8.0 1.9.0 @@ -541,6 +542,12 @@ spring-security-core + + org.projectlombok + lombok + ${lombok.version} + + diff --git a/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java b/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java index 45767fb..40d29d3 100644 --- a/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java +++ b/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java @@ -47,7 +47,7 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter private static final Log LOG = LogFactory.getLog(AbstractMongoSessionConverter.class); - protected static final String EXPIRE_AT_FIELD_NAME = "expireAt"; + static final String EXPIRE_AT_FIELD_NAME = "expireAt"; private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT"; diff --git a/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java b/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java index 902ee06..e0acbf2 100644 --- a/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java +++ b/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java @@ -145,12 +145,14 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter @Override public String translate(String propertyName) { - if (propertyName.equals("id")) { - return "_id"; - } else if (propertyName.equals("_id")) { - return "id"; - } else { - return propertyName; + + switch (propertyName) { + case "id": + return "_id"; + case "_id": + return "id"; + default: + return propertyName; } } } diff --git a/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java b/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java index 32168dd..3d802c7 100644 --- a/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java +++ b/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java @@ -18,12 +18,13 @@ package org.springframework.session.data.mongo; import static org.springframework.session.data.mongo.MongoSessionUtils.*; +import lombok.Setter; + import java.time.Duration; import java.util.Collections; -import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import org.bson.Document; import org.slf4j.Logger; @@ -34,14 +35,11 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.index.IndexOperations; -import org.springframework.data.mongodb.core.query.Query; import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionExpiredEvent; -import com.mongodb.DBObject; - /** * Session repository implementation which stores sessions in Mongo. Uses * {@link AbstractMongoSessionConverter} to transform session objects from/to native Mongo @@ -57,6 +55,8 @@ import com.mongodb.DBObject; public class MongoOperationsSessionRepository implements FindByIndexNameSessionRepository, ApplicationEventPublisherAware, InitializingBean { + private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class); + /** * The default time period in seconds in which a session will expire. */ @@ -67,13 +67,11 @@ public class MongoOperationsSessionRepository */ public static final String DEFAULT_COLLECTION_NAME = "sessions"; - private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class); - private final MongoOperations mongoOperations; - private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; - private String collectionName = DEFAULT_COLLECTION_NAME; - private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( + @Setter private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; + @Setter private String collectionName = DEFAULT_COLLECTION_NAME; + @Setter private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); private ApplicationEventPublisher eventPublisher; @@ -91,15 +89,13 @@ public class MongoOperationsSessionRepository } publishEvent(new SessionCreatedEvent(this, session)); - + return session; } @Override public void save(MongoSession session) { - - DBObject sessionDbObject = convertToDBObject(this.mongoSessionConverter, session); - this.mongoOperations.save(sessionDbObject, this.collectionName); + this.mongoOperations.save(convertToDBObject(this.mongoSessionConverter, session), this.collectionName); } @Override @@ -114,8 +110,10 @@ public class MongoOperationsSessionRepository MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper); if (session.isExpired()) { + publishEvent(new SessionExpiredEvent(this, session)); deleteById(id); + return null; } @@ -134,26 +132,17 @@ public class MongoOperationsSessionRepository @Override public Map findByIndexNameAndIndexValue(String indexName, String indexValue) { - HashMap result = new HashMap(); - - Query query = this.mongoSessionConverter.getQueryForIndex(indexName, indexValue); - - if (query == null) { - return Collections.emptyMap(); + return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue)) + .map(query -> this.mongoOperations.find(query, Document.class, this.collectionName)) + .orElse(Collections.emptyList()) + .stream() + .map(dbSession -> convertToSession(this.mongoSessionConverter, dbSession)) + .collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession)); } - List mapSessions = this.mongoOperations.find(query, Document.class, this.collectionName); - - for (Document dbSession : mapSessions) { - MongoSession mapSession = convertToSession(this.mongoSessionConverter, dbSession); - result.put(mapSession.getId(), mapSession); - } - - return result; - } - @Override public void deleteById(String id) { + Optional.ofNullable(findSession(id)) .ifPresent(document -> { publishEvent(new SessionDeletedEvent(this, convertToSession(this.mongoSessionConverter, document))); @@ -168,22 +157,10 @@ public class MongoOperationsSessionRepository this.mongoSessionConverter.ensureIndexes(indexOperations); } - Document findSession(String id) { + private Document findSession(String id) { return this.mongoOperations.findById(id, Document.class, this.collectionName); } - public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) { - this.mongoSessionConverter = mongoSessionConverter; - } - - public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; - } - - public void setCollectionName(String collectionName) { - this.collectionName = collectionName; - } - @Override public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher; diff --git a/src/main/java/org/springframework/session/data/mongo/MongoSession.java b/src/main/java/org/springframework/session/data/mongo/MongoSession.java index db75447..6dc91a8 100644 --- a/src/main/java/org/springframework/session/data/mongo/MongoSession.java +++ b/src/main/java/org/springframework/session/data/mongo/MongoSession.java @@ -15,14 +15,18 @@ */ package org.springframework.session.data.mongo; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + import java.time.Duration; import java.time.Instant; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import org.springframework.session.Session; @@ -33,6 +37,7 @@ import org.springframework.session.Session; * @author Greg Turnquist * @since 1.2 */ +@EqualsAndHashCode(of = {"id"}) public class MongoSession implements Session { /** @@ -40,12 +45,12 @@ public class MongoSession implements Session { */ private static final char DOT_COVER_CHAR = '\uF607'; - private String id; + @Getter private String id; private long createdMillis = System.currentTimeMillis(); private long accessedMillis; private long intervalSeconds; - private Date expireAt; - private Map attrs = new HashMap(); + @Getter @Setter private Date expireAt; + private Map attrs = new HashMap<>(); public MongoSession() { this(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL); @@ -62,21 +67,13 @@ public class MongoSession implements Session { setLastAccessedTime(Instant.ofEpochMilli(this.createdMillis)); } - public String getId() { - return this.id; - } - public String changeSessionId() { - String changedId = generateId(); + + String changedId = UUID.randomUUID().toString(); this.id = changedId; return changedId; } - private String generateId() { - return UUID.randomUUID().toString(); - } - - @Override public T getAttribute(String attributeName) { return (T) this.attrs.get(coverDot(attributeName)); @@ -84,13 +81,9 @@ public class MongoSession implements Session { public Set getAttributeNames() { - HashSet result = new HashSet<>(); - - for (String key : this.attrs.keySet()) { - result.add(uncoverDot(key)); - } - - return result; + return this.attrs.keySet().stream() + .map(MongoSession::uncoverDot) + .collect(Collectors.toSet()); } public void setAttribute(String attributeName, Object attributeValue) { @@ -136,14 +129,6 @@ public class MongoSession implements Session { return this.intervalSeconds >= 0 && new Date().after(this.expireAt); } - public Date getExpireAt() { - return this.expireAt; - } - - public void setExpireAt(Date expireAt) { - this.expireAt = expireAt; - } - static String coverDot(String attributeName) { return attributeName.replace('.', DOT_COVER_CHAR); } @@ -151,24 +136,4 @@ public class MongoSession implements Session { static String uncoverDot(String attributeName) { return attributeName.replace(DOT_COVER_CHAR, '.'); } - - @Override - public boolean equals(Object o) { - - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - MongoSession that = (MongoSession) o; - - return this.id.equals(that.id); - } - - @Override - public int hashCode() { - return this.id.hashCode(); - } } diff --git a/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java b/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java index 132d20f..3e09771 100644 --- a/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java +++ b/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java @@ -17,6 +17,9 @@ package org.springframework.session.data.mongo; import static org.springframework.session.data.mongo.MongoSessionUtils.*; +import lombok.Getter; +import lombok.Setter; + import java.time.Duration; import org.bson.Document; @@ -54,12 +57,12 @@ public class ReactiveMongoOperationsSessionRepository private final ReactiveMongoOperations mongoOperations; - private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; - private String collectionName = DEFAULT_COLLECTION_NAME; - private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( + @Getter @Setter private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; + @Getter @Setter private String collectionName = DEFAULT_COLLECTION_NAME; + @Setter private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); - private MongoOperations blockingMongoOperations; + @Setter private MongoOperations blockingMongoOperations; private ApplicationEventPublisher eventPublisher; public ReactiveMongoOperationsSessionRepository(ReactiveMongoOperations mongoOperations) { @@ -110,11 +113,11 @@ public class ReactiveMongoOperationsSessionRepository } /** - * Gets the {@link MongoSession} by the {@link MongoSession#getId()} or null if no + * Gets the {@link MongoSession} by the {@link MongoSession#getId()} or {@link Mono#empty()} if no * {@link MongoSession} is found. * * @param id the {@link MongoSession#getId()} to lookup - * @return the {@link MongoSession} by the {@link MongoSession#getId()} or null if no + * @return the {@link MongoSession} by the {@link MongoSession#getId()} or {@link Mono#empty()} if no * {@link MongoSession} is found. */ @Override @@ -159,34 +162,6 @@ public class ReactiveMongoOperationsSessionRepository return this.mongoOperations.findById(id, Document.class, this.collectionName); } - public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) { - this.mongoSessionConverter = mongoSessionConverter; - } - - public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; - } - - public Integer getMaxInactiveIntervalInSeconds() { - return maxInactiveIntervalInSeconds; - } - - public void setCollectionName(String collectionName) { - this.collectionName = collectionName; - } - - public String getCollectionName() { - return collectionName; - } - - public MongoOperations getBlockingMongoOperations() { - return this.blockingMongoOperations; - } - - public void setBlockingMongoOperations(MongoOperations blockingMongoOperations) { - this.blockingMongoOperations = blockingMongoOperations; - } - @Override public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher;