This commit is contained in:
Rossen Stoyanchev 2017-09-06 11:01:22 -04:00
parent 8ad14ae95c
commit c7d54c8b52
5 changed files with 47 additions and 38 deletions

View File

@ -60,7 +60,7 @@ class DefaultWebSession implements WebSession {
/** /**
* Constructor for creating a brand, new session. * Constructor for creating a new session instance.
* @param idGenerator the session id generator * @param idGenerator the session id generator
* @param clock for access to current time * @param clock for access to current time
*/ */
@ -86,7 +86,8 @@ class DefaultWebSession implements WebSession {
} }
/** /**
* Constructor for creating a new session with an updated last access time. * Copy constructor to re-create a session at the start of a new request
* refreshing the last access time of the session.
* @param existingSession the existing session to copy * @param existingSession the existing session to copy
* @param lastAccessTime the new last access time * @param lastAccessTime the new last access time
*/ */

View File

@ -40,6 +40,7 @@ public class DefaultWebSessionManager implements WebSessionManager {
private WebSessionStore sessionStore = new InMemoryWebSessionStore(); private WebSessionStore sessionStore = new InMemoryWebSessionStore();
/** /**
* Configure the id resolution strategy. * Configure the id resolution strategy.
* <p>By default an instance of {@link CookieWebSessionIdResolver}. * <p>By default an instance of {@link CookieWebSessionIdResolver}.
@ -74,6 +75,7 @@ public class DefaultWebSessionManager implements WebSessionManager {
return this.sessionStore; return this.sessionStore;
} }
@Override @Override
public Mono<WebSession> getSession(ServerWebExchange exchange) { public Mono<WebSession> getSession(ServerWebExchange exchange) {
return Mono.defer(() -> return Mono.defer(() ->

View File

@ -31,13 +31,13 @@ import org.springframework.web.server.ServerWebExchange;
*/ */
public class HeaderWebSessionIdResolver implements WebSessionIdResolver { public class HeaderWebSessionIdResolver implements WebSessionIdResolver {
/** /** Default value for {@link #setHeaderName(String)}. */
* The default header name
*/
public static final String DEFAULT_HEADER_NAME = "SESSION"; public static final String DEFAULT_HEADER_NAME = "SESSION";
private String headerName = DEFAULT_HEADER_NAME; private String headerName = DEFAULT_HEADER_NAME;
/** /**
* Set the name of the session header to use for the session id. * Set the name of the session header to use for the session id.
* The name is used to extract the session id from the request headers as * The name is used to extract the session id from the request headers as
@ -50,6 +50,15 @@ public class HeaderWebSessionIdResolver implements WebSessionIdResolver {
this.headerName = headerName; this.headerName = headerName;
} }
/**
* Return the configured header name.
* @return the configured header name
*/
public String getHeaderName() {
return this.headerName;
}
@Override @Override
public List<String> resolveSessionIds(ServerWebExchange exchange) { public List<String> resolveSessionIds(ServerWebExchange exchange) {
HttpHeaders headers = exchange.getRequest().getHeaders(); HttpHeaders headers = exchange.getRequest().getHeaders();
@ -67,11 +76,4 @@ public class HeaderWebSessionIdResolver implements WebSessionIdResolver {
this.setSessionId(exchange, ""); this.setSessionId(exchange, "");
} }
/**
* Return the configured header name.
* @return the configured header name
*/
private String getHeaderName() {
return this.headerName;
}
} }

View File

@ -39,11 +39,34 @@ public class InMemoryWebSessionStore implements WebSessionStore {
private static final IdGenerator idGenerator = new JdkIdGenerator(); private static final IdGenerator idGenerator = new JdkIdGenerator();
private Clock clock = Clock.system(ZoneId.of("GMT")); private Clock clock = Clock.system(ZoneId.of("GMT"));
private final Map<String, WebSession> sessions = new ConcurrentHashMap<>(); private final Map<String, WebSession> sessions = new ConcurrentHashMap<>();
/**
* Configure the {@link Clock} to use to set lastAccessTime on every created
* session and to calculate if it is expired.
* <p>This may be useful to align to different timezone or to set the clock
* back in a test, e.g. {@code Clock.offset(clock, Duration.ofMinutes(-31))}
* in order to simulate session expiration.
* <p>By default this is {@code Clock.system(ZoneId.of("GMT"))}.
* @param clock the clock to use
*/
public void setClock(Clock clock) {
Assert.notNull(clock, "'clock' is required.");
this.clock = clock;
}
/**
* Return the configured clock for session lastAccessTime calculations.
*/
public Clock getClock() {
return this.clock;
}
@Override @Override
public Mono<WebSession> retrieveSession(String id) { public Mono<WebSession> retrieveSession(String id) {
return (this.sessions.containsKey(id) ? Mono.just(this.sessions.get(id)) : Mono.empty()); return (this.sessions.containsKey(id) ? Mono.just(this.sessions.get(id)) : Mono.empty());
@ -70,27 +93,6 @@ public class InMemoryWebSessionStore implements WebSessionStore {
}); });
} }
/**
* Configure the {@link Clock} to use to set lastAccessTime on every created
* session and to calculate if it is expired.
* <p>This may be useful to align to different timezone or to set the clock
* back in a test, e.g. {@code Clock.offset(clock, Duration.ofMinutes(-31))}
* in order to simulate session expiration.
* <p>By default this is {@code Clock.system(ZoneId.of("GMT"))}.
* @param clock the clock to use
*/
public void setClock(Clock clock) {
Assert.notNull(clock, "'clock' is required.");
this.clock = clock;
}
/**
* Return the configured clock for session lastAccessTime calculations.
*/
public Clock getClock() {
return this.clock;
}
private Mono<Void> changeSessionId(String oldId, WebSession session) { private Mono<Void> changeSessionId(String oldId, WebSession session) {
this.sessions.remove(oldId); this.sessions.remove(oldId);
this.sessions.put(session.getId(), session); this.sessions.put(session.getId(), session);

View File

@ -19,8 +19,6 @@ import reactor.core.publisher.Mono;
import org.springframework.web.server.WebSession; import org.springframework.web.server.WebSession;
import java.time.Instant;
/** /**
* Strategy for {@link WebSession} persistence. * Strategy for {@link WebSession} persistence.
* *
@ -31,8 +29,12 @@ import java.time.Instant;
public interface WebSessionStore { public interface WebSessionStore {
/** /**
* Creates the WebSession that can be stored by this WebSessionStore. * Create a new WebSession.
* @return the session * <p>Note that this does nothing more than create a new instance.
* The session can later be started explicitly via {@link WebSession#start()}
* or implicitly by adding attributes -- and then persisted via
* {@link WebSession#save()}.
* @return the created session instance
*/ */
Mono<WebSession> createWebSession(); Mono<WebSession> createWebSession();
@ -51,7 +53,7 @@ public interface WebSessionStore {
Mono<Void> removeSession(String sessionId); Mono<Void> removeSession(String sessionId);
/** /**
* Update the last accessed time to now. * Update the last accessed timestamp to "now".
* @param webSession the session to update * @param webSession the session to update
* @return the session with the updated last access time * @return the session with the updated last access time
*/ */