AbstractMockServerSpec() { // Default instance to be re-used across requests, unless one is configured explicitly this.sessionManager = new DefaultWebSessionManager(); }
@Override public void expireSession(ServerWebExchange exchange) { this.setSessionId(exchange, ""); }
private boolean isExpired(Instant now) { if (this.state.get().equals(State.EXPIRED)) { return true; } if (checkExpired(now)) { this.state.set(State.EXPIRED); return true; } return false; }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }
@Override public Mono<WebSession> getSession(ServerWebExchange exchange) { return Mono.defer(() -> retrieveSession(exchange) .switchIfEmpty(this.sessionStore.createWebSession()) .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session)))); }
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) { return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange)) .concatMap(this.sessionStore::retrieveSession) .next(); }
@Override public boolean isStarted() { return this.state.get().equals(State.STARTED) || !getAttributes().isEmpty(); }
@Override public Mono<WebSession> createWebSession() { Instant now = this.clock.instant(); this.expiredSessionChecker.checkIfNecessary(now); return Mono.fromSupplier(() -> new InMemoryWebSession(now)); }
@Override protected HttpHandler createHttpHandler() { this.sessionManager = new DefaultWebSessionManager(); this.handler = new TestWebHandler(); return WebHttpHandlerBuilder.webHandler(this.handler).sessionManager(this.sessionManager).build(); }
@Test public void multipleSessionIds() { List<String> ids = Arrays.asList("not-this", "not-that", this.updateSession.getId()); when(this.sessionStore.retrieveSession("not-this")).thenReturn(Mono.empty()); when(this.sessionStore.retrieveSession("not-that")).thenReturn(Mono.empty()); when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(ids); WebSession actual = this.sessionManager.getSession(this.exchange).block(); assertNotNull(actual); assertEquals(this.updateSession.getId(), actual.getId()); } }
public void checkIfNecessary(Instant now) { if (this.checkTime.isBefore(now)) { removeExpiredSessions(now); } }
/** * 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; removeExpiredSessions(); }
@Override public boolean isExpired() { return isExpired(clock.instant()); }
private boolean checkExpired(Instant currentTime) { return isStarted() && !this.maxIdleTime.isNegative() && currentTime.minus(this.maxIdleTime).isAfter(this.lastAccessTime); }
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) { return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange)) .concatMap(this.sessionStore::retrieveSession) .next(); }
/** * Build the {@code MockServerWebExchange} instance. */ public MockServerWebExchange build() { return new MockServerWebExchange(this.request, this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager()); } }
private boolean isExpired(Instant now) { if (this.state.get().equals(State.EXPIRED)) { return true; } if (checkExpired(now)) { this.state.set(State.EXPIRED); return true; } return false; }
@Test(expected = IllegalArgumentException.class) public void setSessionIdWhenNullIdThenIllegalArgumentException() { String id = null; this.idResolver.setSessionId(this.exchange, id); }
/** * Check for expired sessions and remove them. Typically such checks are * kicked off lazily during calls to {@link #createWebSession() create} or * {@link #retrieveSession retrieve}, no less than 60 seconds apart. * This method can be called to force a check at a specific time. * @since 5.0.8 */ public void removeExpiredSessions() { this.expiredSessionChecker.removeExpiredSessions(this.clock.instant()); }
/** * Build the {@code MockServerWebExchange} instance. */ public MockServerWebExchange build() { return new MockServerWebExchange(this.request, this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager()); } }