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 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 lastAccessTime the new last access time
*/

View File

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

View File

@ -31,13 +31,13 @@ import org.springframework.web.server.ServerWebExchange;
*/
public class HeaderWebSessionIdResolver implements WebSessionIdResolver {
/**
* The default header name
*/
/** Default value for {@link #setHeaderName(String)}. */
public static final String DEFAULT_HEADER_NAME = "SESSION";
private String headerName = DEFAULT_HEADER_NAME;
/**
* 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
@ -50,6 +50,15 @@ public class HeaderWebSessionIdResolver implements WebSessionIdResolver {
this.headerName = headerName;
}
/**
* Return the configured header name.
* @return the configured header name
*/
public String getHeaderName() {
return this.headerName;
}
@Override
public List<String> resolveSessionIds(ServerWebExchange exchange) {
HttpHeaders headers = exchange.getRequest().getHeaders();
@ -67,11 +76,4 @@ public class HeaderWebSessionIdResolver implements WebSessionIdResolver {
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 Clock clock = Clock.system(ZoneId.of("GMT"));
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
public Mono<WebSession> retrieveSession(String id) {
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) {
this.sessions.remove(oldId);
this.sessions.put(session.getId(), session);

View File

@ -19,8 +19,6 @@ import reactor.core.publisher.Mono;
import org.springframework.web.server.WebSession;
import java.time.Instant;
/**
* Strategy for {@link WebSession} persistence.
*
@ -31,8 +29,12 @@ import java.time.Instant;
public interface WebSessionStore {
/**
* Creates the WebSession that can be stored by this WebSessionStore.
* @return the session
* Create a new WebSession.
* <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();
@ -51,7 +53,7 @@ public interface WebSessionStore {
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
* @return the session with the updated last access time
*/