@Override public Mono<WebSession> session() { return this.exchange.getSession(); }
@Override public Mono<WebSession> getSession() { return this.delegate.getSession(); }
private Mono<Map<String, Object>> initAttributes(ServerWebExchange exchange) { if (this.sessionAttributePredicate == null) { return EMPTY_ATTRIBUTES; } return exchange.getSession().map(session -> session.getAttributes().entrySet().stream() .filter(entry -> this.sessionAttributePredicate.test(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); }
@Override public Mono<WebSession> getSession() { return getDelegate().getSession(); }
private Mono<Map<String, Object>> getSessionAttributes(ServerWebExchange exchange) { return exchange.getSession().map(WebSession::getAttributes); }
@Override @SuppressWarnings("unchecked") public <T extends OAuth2AuthorizedClient> Mono<T> loadAuthorizedClient(String clientRegistrationId, Authentication principal, ServerWebExchange exchange) { Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty"); Assert.notNull(exchange, "exchange cannot be null"); return exchange.getSession() .map(this::getAuthorizedClients) .flatMap(clients -> Mono.justOrEmpty((T) clients.get(clientRegistrationId))); }
@Override public Mono<WebSession> getSession() { return getDelegate().getSession(); }
@Override protected Mono<Object> resolveName(String name, MethodParameter parameter, ServerWebExchange exchange) { return exchange.getSession() .filter(session -> session.getAttribute(name) != null) .map(session -> session.getAttribute(name)); }
@Override public Mono<Object> resolveArgument( MethodParameter parameter, BindingContext context, ServerWebExchange exchange) { Mono<WebSession> session = exchange.getSession(); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(parameter.getParameterType()); return (adapter != null ? Mono.just(adapter.fromPublisher(session)) : Mono.from(session)); }
@Override public GatewayFilter apply(Object config) { return (exchange, chain) -> exchange.getSession() .map(WebSession::save) .then(chain.filter(exchange)); } }
@Override public Mono<Void> saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal, ServerWebExchange exchange) { Assert.notNull(authorizedClient, "authorizedClient cannot be null"); Assert.notNull(exchange, "exchange cannot be null"); return exchange.getSession() .doOnSuccess(session -> { Map<String, OAuth2AuthorizedClient> authorizedClients = getAuthorizedClients(session); authorizedClients.put(authorizedClient.getClientRegistration().getRegistrationId(), authorizedClient); session.getAttributes().put(this.sessionAttributeName, authorizedClients); }) .then(Mono.empty()); }
private void assertSessionStartedIs(boolean expected) { Mono<Boolean> isStarted = this.exchange.getSession().map(WebSession::isStarted); StepVerifier.create(isStarted) .expectNext(expected) .verifyComplete(); } }
return exchange.getSession() .flatMap(session -> { Map<String, Object> attributes = sessionAttributesHandler.retrieveAttributes(session);
@Override public Mono<Void> handle(ServerWebExchange exchange) { if (exchange.getRequest().getQueryParams().containsKey("expire")) { return exchange.getSession().doOnNext(session -> { // Don't do anything, leave it expired... }).then(); } else if (exchange.getRequest().getQueryParams().containsKey("changeId")) { return exchange.getSession().flatMap(session -> session.changeSessionId().doOnSuccess(aVoid -> updateSessionAttribute(session))); } else if (exchange.getRequest().getQueryParams().containsKey("invalidate")) { return exchange.getSession().doOnNext(WebSession::invalidate).then(); } else { return exchange.getSession().doOnSuccess(this::updateSessionAttribute).then(); } }
@Test public void loadAuthorizationRequestWhenSessionAndNoRequestThenEmpty() { Mono<OAuth2AuthorizationRequest> setAttrThenLoad = this.exchange.getSession() .map(WebSession::getAttributes).doOnNext(attrs -> attrs.put("foo", "bar")) .then(this.repository.loadAuthorizationRequest(this.exchange)); StepVerifier.create(setAttrThenLoad) .verifyComplete(); }
@Test public void clearModelAttributeFromSession() throws Exception { WebSession session = this.exchange.getSession().block(Duration.ZERO); assertNotNull(session); TestBean testBean = new TestBean("Session Bean"); session.getAttributes().put("bean", testBean); TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); context.getSessionStatus().setComplete(); context.saveModel(); assertEquals(0, session.getAttributes().size()); }
@Test public void retrieveModelAttributeFromSession() throws Exception { WebSession session = this.exchange.getSession().block(Duration.ZERO); assertNotNull(session); TestBean testBean = new TestBean("Session Bean"); session.getAttributes().put("bean", testBean); TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); context.saveModel(); assertEquals(1, session.getAttributes().size()); assertEquals("Session Bean", ((TestBean) session.getRequiredAttribute("bean")).getName()); }
@Test public void saveModelAttributeToSession() throws Exception { TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); WebSession session = this.exchange.getSession().block(Duration.ZERO); assertNotNull(session); assertEquals(0, session.getAttributes().size()); context.saveModel(); assertEquals(1, session.getAttributes().size()); assertEquals("Bean", ((TestBean) session.getRequiredAttribute("bean")).getName()); }
@Test public void removeAuthorizationRequestWhenPresentThenFoundAndRemoved() { Mono<OAuth2AuthorizationRequest> saveAndRemove = this.repository .saveAuthorizationRequest(this.authorizationRequest, this.exchange) .then(this.repository.removeAuthorizationRequest(this.exchange)); StepVerifier.create(saveAndRemove).expectNext(this.authorizationRequest) .verifyComplete(); StepVerifier.create(this.exchange.getSession() .map(WebSession::getAttributes) .map(Map::isEmpty)) .expectNext(true) .verifyComplete(); }
@Test // SPR-15674 (in comments) public void mutateDoesNotCreateNewSession() { WebTestClient client = WebTestClient .bindToWebHandler(exchange -> { if (exchange.getRequest().getURI().getPath().equals("/set")) { return exchange.getSession() .doOnNext(session -> session.getAttributes().put("foo", "bar")) .then(); } else { return exchange.getSession() .map(session -> session.getAttributeOrDefault("foo", "none")) .flatMap(value -> { DataBuffer buffer = toDataBuffer(value); return exchange.getResponse().writeWith(Mono.just(buffer)); }); } }) .build(); // Set the session attribute EntityExchangeResult<Void> result = client.get().uri("/set").exchange() .expectStatus().isOk().expectBody().isEmpty(); ResponseCookie session = result.getResponseCookies().getFirst("SESSION"); // Now get attribute client.mutate().build() .get().uri("/get") .cookie(session.getName(), session.getValue()) .exchange() .expectBody(String.class).isEqualTo("bar"); }