@Override public String getId() { return this.delegate.getId(); }
@Override public String getId() { return this.delegate.getId(); }
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(); }
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(); }
@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()); } }
@Test public void existingSession() { String sessionId = this.updateSession.getId(); when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.singletonList(sessionId)); WebSession actual = this.sessionManager.getSession(this.exchange).block(); assertNotNull(actual); assertEquals(sessionId, actual.getId()); }
@Test public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId() { when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList()); WebSession session = this.sessionManager.getSession(this.exchange).block(); assertSame(this.createSession, session); String sessionId = this.createSession.getId(); when(this.createSession.isStarted()).thenReturn(true); this.exchange.getResponse().setComplete().block(); verify(this.sessionStore).createWebSession(); verify(this.sessionIdResolver).setSessionId(any(), eq(sessionId)); verify(this.createSession).save(); }
@Before public void setUp() throws Exception { when(this.createSession.save()).thenReturn(Mono.empty()); when(this.createSession.getId()).thenReturn("create-session-id"); when(this.updateSession.getId()).thenReturn("update-session-id"); when(this.sessionStore.createWebSession()).thenReturn(Mono.just(this.createSession)); when(this.sessionStore.retrieveSession(this.updateSession.getId())).thenReturn(Mono.just(this.updateSession)); this.sessionManager = new DefaultWebSessionManager(); this.sessionManager.setSessionIdResolver(this.sessionIdResolver); this.sessionManager.setSessionStore(this.sessionStore); MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); }
@Test public void retrieveExpiredSession() { WebSession session = this.store.createWebSession().block(); assertNotNull(session); session.getAttributes().put("foo", "bar"); session.save().block(); String id = session.getId(); WebSession retrieved = this.store.retrieveSession(id).block(); assertNotNull(retrieved); assertSame(session, retrieved); // Fast-forward 31 minutes this.store.setClock(Clock.offset(this.store.getClock(), Duration.ofMinutes(31))); WebSession retrievedAgain = this.store.retrieveSession(id).block(); assertNull(retrievedAgain); }
@Test public void lastAccessTimeIsUpdatedOnRetrieve() { WebSession session1 = this.store.createWebSession().block(); assertNotNull(session1); String id = session1.getId(); Instant time1 = session1.getLastAccessTime(); session1.start(); session1.save().block(); // Fast-forward a few seconds this.store.setClock(Clock.offset(this.store.getClock(), Duration.ofSeconds(5))); WebSession session2 = this.store.retrieveSession(id).block(); assertNotNull(session2); assertSame(session1, session2); Instant time2 = session2.getLastAccessTime(); assertTrue(time1.isBefore(time2)); }
@Test // SPR-17051 public void sessionInvalidatedBeforeSave() { // Request 1 creates session WebSession session1 = this.store.createWebSession().block(); assertNotNull(session1); String id = session1.getId(); session1.start(); session1.save().block(); // Request 2 retrieves session WebSession session2 = this.store.retrieveSession(id).block(); assertNotNull(session2); assertSame(session1, session2); // Request 3 retrieves and invalidates WebSession session3 = this.store.retrieveSession(id).block(); assertNotNull(session3); assertSame(session1, session3); session3.invalidate().block(); // Request 2 saves session after invalidated session2.save().block(); // Session should not be present WebSession session4 = this.store.retrieveSession(id).block(); assertNull(session4); }
private String getStartedSessionId(WebSession session) { return (session != null && session.isStarted()) ? session.getId() : null; }
@Override public String getId() { return this.delegate.getId(); }
@Override public String getId() { return this.delegate.getId(); }
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(); }
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(); }