Spring Session updated to 2.0.0 #1261

pull/1303/head
Nikita 7 years ago
parent db8b3147bc
commit 47becf1ee5

@ -258,8 +258,8 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.session</groupId> <groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId> <artifactId>spring-session-core</artifactId>
<version>[1.2.2,)</version> <version>[2.0.0,)</version>
<scope>provided</scope> <scope>provided</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

@ -15,6 +15,8 @@
*/ */
package org.redisson.spring.session; package org.redisson.spring.session;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -35,7 +37,6 @@ import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.MapSession; import org.springframework.session.MapSession;
import org.springframework.session.Session; import org.springframework.session.Session;
@ -51,7 +52,7 @@ import org.springframework.session.events.SessionExpiredEvent;
public class RedissonSessionRepository implements FindByIndexNameSessionRepository<RedissonSessionRepository.RedissonSession>, public class RedissonSessionRepository implements FindByIndexNameSessionRepository<RedissonSessionRepository.RedissonSession>,
PatternMessageListener<String> { PatternMessageListener<String> {
final class RedissonSession implements ExpiringSession { final class RedissonSession implements Session {
private String principalName; private String principalName;
private final MapSession delegate; private final MapSession delegate;
@ -63,9 +64,9 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
principalName = resolvePrincipal(delegate); principalName = resolvePrincipal(delegate);
Map<String, Object> newMap = new HashMap<String, Object>(3); Map<String, Object> newMap = new HashMap<String, Object>(3);
newMap.put("session:creationTime", delegate.getCreationTime()); newMap.put("session:creationTime", delegate.getCreationTime().toEpochMilli());
newMap.put("session:lastAccessedTime", delegate.getLastAccessedTime()); newMap.put("session:lastAccessedTime", delegate.getLastAccessedTime().toEpochMilli());
newMap.put("session:maxInactiveInterval", delegate.getMaxInactiveIntervalInSeconds()); newMap.put("session:maxInactiveInterval", delegate.getMaxInactiveInterval().getSeconds());
map.putAll(newMap); map.putAll(newMap);
updateExpiration(); updateExpiration();
@ -76,8 +77,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
} }
private void updateExpiration() { private void updateExpiration() {
if (delegate.getMaxInactiveIntervalInSeconds() >= 0) { if (delegate.getMaxInactiveInterval().getSeconds() >= 0) {
map.expire(delegate.getMaxInactiveIntervalInSeconds(), TimeUnit.SECONDS); map.expire(delegate.getMaxInactiveInterval().getSeconds(), TimeUnit.SECONDS);
} }
} }
@ -95,11 +96,11 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
Set<Entry<String, Object>> entrySet = map.readAllEntrySet(); Set<Entry<String, Object>> entrySet = map.readAllEntrySet();
for (Entry<String, Object> entry : entrySet) { for (Entry<String, Object> entry : entrySet) {
if ("session:creationTime".equals(entry.getKey())) { if ("session:creationTime".equals(entry.getKey())) {
delegate.setCreationTime((Long) entry.getValue()); delegate.setCreationTime(Instant.ofEpochMilli((Long) entry.getValue()));
} else if ("session:lastAccessedTime".equals(entry.getKey())) { } else if ("session:lastAccessedTime".equals(entry.getKey())) {
delegate.setLastAccessedTime((Long) entry.getValue()); delegate.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue()));
} else if ("session:maxInactiveInterval".equals(entry.getKey())) { } else if ("session:maxInactiveInterval".equals(entry.getKey())) {
delegate.setMaxInactiveIntervalInSeconds((Integer) entry.getValue()); delegate.setMaxInactiveInterval(Duration.ofSeconds((Long) entry.getValue()));
} else { } else {
delegate.setAttribute(entry.getKey(), entry.getValue()); delegate.setAttribute(entry.getKey(), entry.getValue());
} }
@ -172,38 +173,38 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
} }
@Override @Override
public long getCreationTime() { public Instant getCreationTime() {
return delegate.getCreationTime(); return delegate.getCreationTime();
} }
@Override @Override
public void setLastAccessedTime(long lastAccessedTime) { public void setLastAccessedTime(Instant lastAccessedTime) {
delegate.setLastAccessedTime(lastAccessedTime); delegate.setLastAccessedTime(lastAccessedTime);
if (map != null) { if (map != null) {
map.fastPut("session:lastAccessedTime", lastAccessedTime); map.fastPut("session:lastAccessedTime", lastAccessedTime.toEpochMilli());
updateExpiration(); updateExpiration();
} }
} }
@Override @Override
public long getLastAccessedTime() { public Instant getLastAccessedTime() {
return delegate.getLastAccessedTime(); return delegate.getLastAccessedTime();
} }
@Override @Override
public void setMaxInactiveIntervalInSeconds(int interval) { public void setMaxInactiveInterval(Duration interval) {
delegate.setMaxInactiveIntervalInSeconds(interval); delegate.setMaxInactiveInterval(interval);
if (map != null) { if (map != null) {
map.fastPut("session:maxInactiveInterval", interval); map.fastPut("session:maxInactiveInterval", interval.getSeconds());
updateExpiration(); updateExpiration();
} }
} }
@Override @Override
public int getMaxInactiveIntervalInSeconds() { public Duration getMaxInactiveInterval() {
return delegate.getMaxInactiveIntervalInSeconds(); return delegate.getMaxInactiveInterval();
} }
@Override @Override
@ -211,6 +212,11 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
return delegate.isExpired(); return delegate.isExpired();
} }
@Override
public String changeSessionId() {
return delegate.changeSessionId();
}
} }
private static final Logger log = LoggerFactory.getLogger(RedissonSessionRepository.class); private static final Logger log = LoggerFactory.getLogger(RedissonSessionRepository.class);
@ -243,7 +249,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
@Override @Override
public void onMessage(String pattern, String channel, String body) { public void onMessage(String pattern, String channel, String body) {
if (createdTopic.getPatternNames().contains(pattern)) { if (createdTopic.getPatternNames().contains(pattern)) {
RedissonSession session = getSession(body); RedissonSession session = findById(body);
if (session != null) { if (session != null) {
publishEvent(new SessionCreatedEvent(this, session)); publishEvent(new SessionCreatedEvent(this, session));
} }
@ -256,10 +262,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
RedissonSession session = new RedissonSession(id); RedissonSession session = new RedissonSession(id);
if (session.load()) { if (session.load()) {
session.clearPrincipal(); session.clearPrincipal();
publishEvent(new SessionDeletedEvent(this, session));
} else {
publishEvent(new SessionDeletedEvent(this, id));
} }
publishEvent(new SessionDeletedEvent(this, session));
} else if (expiredTopic.getPatternNames().contains(pattern)) { } else if (expiredTopic.getPatternNames().contains(pattern)) {
if (!body.contains(":")) { if (!body.contains(":")) {
return; return;
@ -269,10 +273,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
RedissonSession session = new RedissonSession(id); RedissonSession session = new RedissonSession(id);
if (session.load()) { if (session.load()) {
session.clearPrincipal(); session.clearPrincipal();
publishEvent(new SessionExpiredEvent(this, session));
} else {
publishEvent(new SessionExpiredEvent(this, id));
} }
publishEvent(new SessionExpiredEvent(this, session));
} }
} }
@ -292,7 +294,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
public RedissonSession createSession() { public RedissonSession createSession() {
RedissonSession session = new RedissonSession(); RedissonSession session = new RedissonSession();
if (defaultMaxInactiveInterval != null) { if (defaultMaxInactiveInterval != null) {
session.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval); session.setMaxInactiveInterval(Duration.ofSeconds(defaultMaxInactiveInterval));
} }
return session; return session;
} }
@ -303,7 +305,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
} }
@Override @Override
public RedissonSession getSession(String id) { public RedissonSession findById(String id) {
RedissonSession session = new RedissonSession(id); RedissonSession session = new RedissonSession(id);
if (!session.load() || session.isExpired()) { if (!session.load() || session.isExpired()) {
return null; return null;
@ -312,8 +314,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
} }
@Override @Override
public void delete(String id) { public void deleteById(String id) {
RedissonSession session = getSession(id); RedissonSession session = findById(id);
if (session == null) { if (session == null) {
return; return;
} }
@ -368,7 +370,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito
Set<String> sessionIds = set.readAll(); Set<String> sessionIds = set.readAll();
Map<String, RedissonSession> result = new HashMap<String, RedissonSession>(); Map<String, RedissonSession> result = new HashMap<String, RedissonSession>();
for (String id : sessionIds) { for (String id : sessionIds) {
RedissonSession session = getSession(id); RedissonSession session = findById(id);
if (session != null) { if (session != null) {
result.put(id, session); result.put(id, session);
} }

@ -186,6 +186,8 @@ public class RedissonSessionManagerTest {
write(executor, "test", "1234"); write(executor, "test", "1234");
Cookie cookie = cookieStore.getCookies().get(0); Cookie cookie = cookieStore.getCookies().get(0);
Thread.sleep(50);
Assert.assertEquals(1, listener.getSessionCreatedEvents()); Assert.assertEquals(1, listener.getSessionCreatedEvents());
Assert.assertEquals(0, listener.getSessionDeletedEvents()); Assert.assertEquals(0, listener.getSessionDeletedEvents());

Loading…
Cancel
Save