|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.session.data.mongo; |
| 17 | + |
| 18 | +import static org.springframework.session.data.mongo.MongoSessionUtils.*; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +import org.bson.Document; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.springframework.beans.factory.InitializingBean; |
| 30 | +import org.springframework.context.ApplicationEvent; |
| 31 | +import org.springframework.context.ApplicationEventPublisher; |
| 32 | +import org.springframework.context.ApplicationEventPublisherAware; |
| 33 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 34 | +import org.springframework.data.mongodb.core.index.IndexOperations; |
| 35 | +import org.springframework.lang.Nullable; |
| 36 | +import org.springframework.session.FindByIndexNameSessionRepository; |
| 37 | +import org.springframework.session.events.SessionCreatedEvent; |
| 38 | +import org.springframework.session.events.SessionDeletedEvent; |
| 39 | +import org.springframework.session.events.SessionExpiredEvent; |
| 40 | + |
| 41 | +/** |
| 42 | + * Session repository implementation which stores sessions in Mongo. Uses {@link AbstractMongoSessionConverter} to |
| 43 | + * transform session objects from/to native Mongo representation ({@code DBObject}). Repository is also responsible for |
| 44 | + * removing expired sessions from database. Cleanup is done every minute. |
| 45 | + * |
| 46 | + * @author Jakub Kubrynski |
| 47 | + * @author Greg Turnquist |
| 48 | + * @since 2.2.0 |
| 49 | + */ |
| 50 | +public class MongoIndexedSessionRepository |
| 51 | + implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean { |
| 52 | + |
| 53 | + /** |
| 54 | + * The default time period in seconds in which a session will expire. |
| 55 | + */ |
| 56 | + public static final int DEFAULT_INACTIVE_INTERVAL = 1800; |
| 57 | + /** |
| 58 | + * the default collection name for storing session. |
| 59 | + */ |
| 60 | + public static final String DEFAULT_COLLECTION_NAME = "sessions"; |
| 61 | + private static final Logger logger = LoggerFactory.getLogger(MongoIndexedSessionRepository.class); |
| 62 | + private final MongoOperations mongoOperations; |
| 63 | + private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; |
| 64 | + private String collectionName = DEFAULT_COLLECTION_NAME; |
| 65 | + private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( |
| 66 | + Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); |
| 67 | + private ApplicationEventPublisher eventPublisher; |
| 68 | + |
| 69 | + public MongoIndexedSessionRepository(MongoOperations mongoOperations) { |
| 70 | + this.mongoOperations = mongoOperations; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public MongoSession createSession() { |
| 75 | + |
| 76 | + MongoSession session = new MongoSession(); |
| 77 | + |
| 78 | + if (this.maxInactiveIntervalInSeconds != null) { |
| 79 | + session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); |
| 80 | + } |
| 81 | + |
| 82 | + publishEvent(new SessionCreatedEvent(this, session)); |
| 83 | + |
| 84 | + return session; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void save(MongoSession session) { |
| 89 | + this.mongoOperations.save(Assert.requireNonNull(convertToDBObject(this.mongoSessionConverter, session), |
| 90 | + "convertToDBObject must not null!"), this.collectionName); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + @Nullable |
| 95 | + public MongoSession findById(String id) { |
| 96 | + |
| 97 | + Document sessionWrapper = findSession(id); |
| 98 | + |
| 99 | + if (sessionWrapper == null) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper); |
| 104 | + |
| 105 | + if (session != null && session.isExpired()) { |
| 106 | + publishEvent(new SessionExpiredEvent(this, session)); |
| 107 | + deleteById(id); |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + return session; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Currently this repository allows only querying against {@code PRINCIPAL_NAME_INDEX_NAME}. |
| 116 | + * |
| 117 | + * @param indexName the name if the index (i.e. {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME}) |
| 118 | + * @param indexValue the value of the index to search for. |
| 119 | + * @return sessions map |
| 120 | + */ |
| 121 | + @Override |
| 122 | + public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName, String indexValue) { |
| 123 | + |
| 124 | + return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue)) |
| 125 | + .map(query -> this.mongoOperations.find(query, Document.class, this.collectionName)) |
| 126 | + .orElse(Collections.emptyList()).stream() |
| 127 | + .map(dbSession -> convertToSession(this.mongoSessionConverter, dbSession)) |
| 128 | + .collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession)); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void deleteById(String id) { |
| 133 | + |
| 134 | + Optional.ofNullable(findSession(id)).ifPresent(document -> { |
| 135 | + MongoSession session = convertToSession(this.mongoSessionConverter, document); |
| 136 | + if (session != null) { |
| 137 | + publishEvent(new SessionDeletedEvent(this, session)); |
| 138 | + } |
| 139 | + this.mongoOperations.remove(document, this.collectionName); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void afterPropertiesSet() { |
| 145 | + |
| 146 | + IndexOperations indexOperations = this.mongoOperations.indexOps(this.collectionName); |
| 147 | + this.mongoSessionConverter.ensureIndexes(indexOperations); |
| 148 | + } |
| 149 | + |
| 150 | + @Nullable |
| 151 | + private Document findSession(String id) { |
| 152 | + return this.mongoOperations.findById(id, Document.class, this.collectionName); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { |
| 157 | + this.eventPublisher = eventPublisher; |
| 158 | + } |
| 159 | + |
| 160 | + private void publishEvent(ApplicationEvent event) { |
| 161 | + |
| 162 | + try { |
| 163 | + this.eventPublisher.publishEvent(event); |
| 164 | + } catch (Throwable ex) { |
| 165 | + logger.error("Error publishing " + event + ".", ex); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) { |
| 170 | + this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; |
| 171 | + } |
| 172 | + |
| 173 | + public void setCollectionName(final String collectionName) { |
| 174 | + this.collectionName = collectionName; |
| 175 | + } |
| 176 | + |
| 177 | + public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) { |
| 178 | + this.mongoSessionConverter = mongoSessionConverter; |
| 179 | + } |
| 180 | +} |
0 commit comments