/** * A shortcut for {@link #withSecurityContext(Mono)} * @param authentication the {@link Authentication} to be used * @return a Reactor {@link Context} that contains the {@code Mono<SecurityContext>} */ public static Context withAuthentication(Authentication authentication) { return withSecurityContext(Mono.just(new SecurityContextImpl(authentication))); } }
/** * A shortcut for {@link #withSecurityContext(Mono)} * @param authentication the {@link Authentication} to be used * @return a Reactor {@link Context} that contains the {@code Mono<SecurityContext>} */ public static Context withAuthentication(Authentication authentication) { return withSecurityContext(Mono.just(new SecurityContextImpl(authentication))); } }
@Test public void setContextAndGetContextThenEmitsContext() { SecurityContext expectedContext = new SecurityContextImpl( new TestingAuthenticationToken("user", "password", "ROLE_USER")); Mono<SecurityContext> context = Mono.subscriberContext() .flatMap( c -> ReactiveSecurityContextHolder.getContext()) .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); StepVerifier.create(context) .expectNext(expectedContext) .verifyComplete(); }
@Test public void setContextAndClearAndGetContextThenEmitsEmpty() { SecurityContext expectedContext = new SecurityContextImpl( new TestingAuthenticationToken("user", "password", "ROLE_USER")); Mono<SecurityContext> context = Mono.subscriberContext() .flatMap( c -> ReactiveSecurityContextHolder.getContext()) .subscriberContext(ReactiveSecurityContextHolder.clearContext()) .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); StepVerifier.create(context) .verifyComplete(); }
/** * A shortcut for {@link #withSecurityContext(Mono)} * @param authentication the {@link Authentication} to be used * @return a Reactor {@link Context} that contains the {@code Mono<SecurityContext>} */ public static Context withAuthentication(Authentication authentication) { return withSecurityContext(Mono.just(new SecurityContextImpl(authentication))); } }
@Override public Context currentContext() { Context context = delegate.currentContext(); if (context.hasKey(CONTEXT_DEFAULTED_ATTR_NAME)) { return context; } context = context.put(CONTEXT_DEFAULTED_ATTR_NAME, Boolean.TRUE); Authentication authentication = securityContext.getAuthentication(); if (authentication == null) { return context; } Context toMerge = ReactiveSecurityContextHolder.withSecurityContext( Mono.just(this.securityContext)); return toMerge.putAll(context); }
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain webFilterChain) { Supplier<Mono<SecurityContext>> context = exchange.getAttribute(ATTRIBUTE_NAME); if (context != null) { exchange.getAttributes().remove(ATTRIBUTE_NAME); return webFilterChain.filter(exchange) .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(context.get())); } return webFilterChain.filter(exchange); } }
@Override public Context currentContext() { Context context = delegate.currentContext(); if (context.hasKey(CONTEXT_DEFAULTED_ATTR_NAME)) { return context; } context = context.put(CONTEXT_DEFAULTED_ATTR_NAME, Boolean.TRUE); Authentication authentication = securityContext.getAuthentication(); if (authentication == null) { return context; } Context toMerge = ReactiveSecurityContextHolder.withSecurityContext( Mono.just(this.securityContext)); return toMerge.putAll(context); }
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain webFilterChain) { Supplier<Mono<SecurityContext>> context = exchange.getAttribute(ATTRIBUTE_NAME); if (context != null) { exchange.getAttributes().remove(ATTRIBUTE_NAME); return webFilterChain.filter(exchange) .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(context.get())); } return webFilterChain.filter(exchange); } }
protected Mono<Void> onAuthenticationSuccess(Authentication authentication, WebFilterExchange webFilterExchange) { ServerWebExchange exchange = webFilterExchange.getExchange(); SecurityContextImpl securityContext = new SecurityContextImpl(); securityContext.setAuthentication(authentication); return this.securityContextRepository.save(exchange, securityContext) .then(this.authenticationSuccessHandler .onAuthenticationSuccess(webFilterExchange, authentication)) .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext))); }