/** * Attempts to load the client registration id from the current {@link Authentication} * @return */ private Mono<String> clientRegistrationId(Mono<Authentication> authentication) { return authentication .filter(t -> this.defaultOAuth2AuthorizedClient && t instanceof OAuth2AuthenticationToken) .cast(OAuth2AuthenticationToken.class) .map(OAuth2AuthenticationToken::getAuthorizedClientRegistrationId); }
/** * Attempts to load the client registration id from the current {@link Authentication} * @return */ private Mono<String> clientRegistrationId(Mono<Authentication> authentication) { return authentication .filter(t -> this.defaultOAuth2AuthorizedClient && t instanceof OAuth2AuthenticationToken) .cast(OAuth2AuthenticationToken.class) .map(OAuth2AuthenticationToken::getAuthorizedClientRegistrationId); }
@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<OAuth2AuthorizationRequest> loadAuthorizationRequest( ServerWebExchange exchange) { String state = getStateParameter(exchange); if (state == null) { return Mono.empty(); } return getStateToAuthorizationRequest(exchange, false) .filter(stateToAuthorizationRequest -> stateToAuthorizationRequest.containsKey(state)) .map(stateToAuthorizationRequest -> stateToAuthorizationRequest.get(state)); }
/** * Return a new {@code DataBuffer} composed from joining together the given * {@code dataBuffers} elements. Depending on the {@link DataBuffer} type, * the returned buffer may be a single buffer containing all data of the * provided buffers, or it may be a zero-copy, composite with references to * the given buffers. * <p>If {@code dataBuffers} produces an error or if there is a cancel * signal, then all accumulated buffers will be * {@linkplain #release(DataBuffer) released}. * <p>Note that the given data buffers do <strong>not</strong> have to be * released. They will be released as part of the returned composite. * @param dataBuffers the data buffers that are to be composed * @return a buffer that is composed from the {@code dataBuffers} argument * @since 5.0.3 */ public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) { Assert.notNull(dataBuffers, "'dataBuffers' must not be null"); return Flux.from(dataBuffers) .collectList() .filter(list -> !list.isEmpty()) .map(list -> list.get(0).factory().join(list)) .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release); }
@Override public Mono<OAuth2AuthorizationRequest> resolve(ServerWebExchange exchange) { return this.authorizationRequestMatcher.matches(exchange) .filter(matchResult -> matchResult.isMatch()) .map(ServerWebExchangeMatcher.MatchResult::getVariables) .map(variables -> variables.get(DEFAULT_REGISTRATION_ID_URI_VARIABLE_NAME)) .cast(String.class) .flatMap(clientRegistrationId -> resolve(exchange, clientRegistrationId)); }
/** * Get a single instance. * * @param id The application identifier. * @return The registered application. */ @GetMapping(path = "/instances/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public Mono<ResponseEntity<Instance>> instance(@PathVariable String id) { LOGGER.debug("Deliver registered instance with ID '{}'", id); return registry.getInstance(InstanceId.of(id)) .filter(Instance::isRegistered) .map(ResponseEntity::ok) .defaultIfEmpty(ResponseEntity.notFound().build()); }
@Override public Mono<Authentication> authenticate(Authentication authentication) { return Mono.justOrEmpty(authentication) .filter(a -> a instanceof BearerTokenAuthenticationToken) .cast(BearerTokenAuthenticationToken.class) .map(BearerTokenAuthenticationToken::getToken) .flatMap(this.jwtDecoder::decode) .flatMap(this.jwtAuthenticationConverter::convert) .cast(Authentication.class) .onErrorMap(JwtException.class, this::onError); }
@Override public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException denied) { Map<String, String> parameters = new LinkedHashMap<>(); if (this.realmName != null) { parameters.put("realm", this.realmName); } return exchange.getPrincipal() .filter(AbstractOAuth2TokenAuthenticationToken.class::isInstance) .cast(AbstractOAuth2TokenAuthenticationToken.class) .map(token -> errorMessageParameters(token, parameters)) .switchIfEmpty(Mono.just(parameters)) .flatMap(params -> respond(exchange, params)); }
private Mono<ServerWebExchange> currentServerWebExchange() { return Mono.subscriberContext() .filter(c -> c.hasKey(ServerWebExchange.class)) .map(c -> c.get(ServerWebExchange.class)); }
private Mono<ServerWebExchange> currentServerWebExchange() { return Mono.subscriberContext() .filter(c -> c.hasKey(ServerWebExchange.class)) .map(c -> c.get(ServerWebExchange.class)); }
/** * Gets the {@code Mono<SecurityContext>} from Reactor {@link Context} * @return the {@code Mono<SecurityContext>} */ public static Mono<SecurityContext> getContext() { return Mono.subscriberContext() .filter( c -> c.hasKey(SECURITY_CONTEXT_KEY)) .flatMap( c-> c.<Mono<SecurityContext>>get(SECURITY_CONTEXT_KEY)); }
@Override public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) { return authentication .filter(a -> a.isAuthenticated()) .flatMapIterable( a -> a.getAuthorities()) .map(g -> g.getAuthority()) .any(a -> this.authorities.contains(a)) .map( hasAuthority -> new AuthorizationDecision(hasAuthority)) .defaultIfEmpty(new AuthorizationDecision(false)); }
@GetMapping(path = "/applications/{name}", produces = MediaType.APPLICATION_JSON_VALUE) public Mono<ResponseEntity<Application>> application(@PathVariable("name") String name) { return this.toApplication(name, registry.getInstances(name).filter(Instance::isRegistered)) .filter(a -> !a.getInstances().isEmpty()) .map(ResponseEntity::ok) .defaultIfEmpty(ResponseEntity.notFound().build()); }
@Override public Mono<Void> notify(InstanceEvent event) { if (!enabled) { return Mono.empty(); } return repository.find(event.getInstance()) .filter(instance -> shouldNotify(event, instance)) .flatMap(instance -> doNotify(event, instance)) .doOnError(ex -> getLogger().error("Couldn't notify for event {} ", event, ex)) .then(); }
@Override public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) { return authentication .filter(this::isNotAnonymous) .map(a -> new AuthorizationDecision(a.isAuthenticated())) .defaultIfEmpty(new AuthorizationDecision(false)); }
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return this.requiresAuthenticationMatcher.matches(exchange) .filter( matchResult -> matchResult.isMatch()) .flatMap( matchResult -> this.authenticationConverter.convert(exchange)) .switchIfEmpty(chain.filter(exchange).then(Mono.empty())) .flatMap( token -> authenticate(exchange, chain, token)); }
@Override public Mono<TraceContext> getTraceContextLater() { return Mono.subscriberContext() .filter(it -> it.hasKey(Span.class) || it.hasKey(brave.propagation.TraceContext.class)).map(it -> { if (it.hasKey(Span.class)) { return new BraveTraceContext(it.get(Span.class).context()); } return new BraveTraceContext(it.get(brave.propagation.TraceContext.class)); }); } }
@GetMapping("/**") Mono<String> pathWithinApplicationFromContext() { return Mono.subscriberContext() .filter(c -> c.hasKey(ServerWebExchange.class)) .map(c -> c.get(ServerWebExchange.class)) .map(e -> e.getRequest().getPath().pathWithinApplication().value()); } }
@Test public void contextForLibraryReactivePutNoContext() { Mono<String> put = doPut("www.example.com", Mono.just("Walter")) .filter(t -> t.getT1() < 300) .map(Tuple2::getT2); StepVerifier.create(put) .expectNext("PUT <Walter> sent to www.example.com") .verifyComplete(); } }