天道酬勤,学无止境

spring-webflux

Webflux timeout for a chain of Mono

i'm using Spring Webflux and trying to understand the timeout concept for a chain of Monos. For example, there's a chain of Mono calls: myService.firstOperation() .then(myService.secondOperation()) ... .then(myService.nOperation()) .timeout(3000L) How is timeout going to be applied: 1) For the operations in general (sum time of operations) 2) For ech operation (each operation should take no longer than timeout) 3) Only for the last operation (nOperation) ? I'm almost sure that timeout is applied to the last publisher. If so, how can timeout be applied to to the sum of operations?

2022-05-03 05:49:30    分类:问答    java   spring   reactive-programming   spring-webflux   project-reactor

Is there a way to modify the default coroutine context created by Spring?

Using Spring WebFlux and Kotlin Coroutines, methods in controllers are usually (if they invoke other suspending functions) marked with the suspend modifier. That means that Spring implicitly creates coroutines to call them, and it does that with some coroutine context . Is there a way to have control over that coroutine context? In particular, I'd like to add MDCContext to every coroutine context that Spring creates.

2022-05-02 23:52:45    分类:问答    spring-webflux   kotlin-coroutines

Spring-webflux filter to fetch the request body

I need to fetch the entire request body in filter and convert it into String. Below is my code but nothing is getting printed on console. @Component public class WebFilter01 implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) { Flux<DataBuffer> requestBody = serverWebExchange.getRequest().getBody(); Flux<String> decodedRequest = requestBody.map(databuffer -> { return decodeDataBuffer(databuffer); }); decodedRequest.doOnNext(s -> System.out.print(s)); return webFilterChain.filter(serverWebExchange); } protected String

2022-05-02 23:28:36    分类:问答    spring   filter   nonblocking   spring-webflux   reactive

How to customize the Authorization header of the OAuth2 token request using spring-security-oauth2 with a WebClient?

I am trying to upgrade to spring security 5.5.1 on a WebClient call. I found out that the oauth2 clientId and secret are now URL encoded in AbstractWebClientReactiveOAuth2AccessTokenResponseClient, but my token provider does not support this (for example if the secret contains a + character it works only when it is sent as a + not as %2B). I understand this is seen as a bug fix from spring-security side ), but I cannot make the token provider change its behavior easily. So I tried to find a way to work around this. The [documentation] (https://docs.spring.io/spring-security/site/docs/current

2022-05-02 14:26:57    分类:问答    spring-security   spring-webflux   spring-security-oauth2   spring-webclient

Web ignoring using spring-webflux

In spring-mvc is possible to extends from WebSecurityConfigurerAdapter , override configure(WebSecurity web) and do somethink like this: @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(AUTH_WHITE_LIST); } The main benefit of this approach is that spring-security even will not try to decode passed token. Is it possible to do pretty much the same but using webflux? I know that i can do like this: @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) throws Exception { http.csrf().disable() .authorizeExchange()

2022-05-02 13:56:02    分类:问答    java   spring-security   spring-webflux

Spring Webflux returning null back to controller

Every time I think I understand Webflux and project reactor, I find out I have no idea. So I making some API calls... I want to call 1 first ... Get information back use that information, to make subsequent calls. so I do this like so public Mono<ResponseObject> createAggregatedRecords(RecordToPersist recordToPersist){ return randomApiClient.createRecord(recordToPersist) .flatMap(result -> { return Mono.zip( webClientInstance.createOtherRecord1(result.getChildRecord1()), webClientInstance2.createOtherRecord2(result.getChildRecord2()), webClientInstance3.createOtherRecord3(result

2022-05-02 09:38:30    分类:问答    spring-webflux   project-reactor   WebFlux

Proxy configuration using Reactor Webclient in Java returns 405 Method not allowed

I am trying to make a proxy configuration using reactor webclient but it throws 405 method not allowed whilst when I was trying to make the same configuration using org.apache.http.client.HttpClient it returns 200 status and in Postman also it returns 200 status. I am using GET request in all the ways. HttpClient httpClient = HttpClient.create().tcpConfiguration(tcpClient -> { tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.httpConnectionTimeout * 1000); tcpClient = tcpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP) .host("localhost") .port(20003) .build())

2022-05-02 04:23:42    分类:问答    java   spring-webflux   project-reactor   reactor-netty

Java Spring Boot Webflux - Mono response when there is no http body, but just http status

Small question regarding Spring Boot Webflux 2.5.0 and how to deal with a http response without body. By "without body" I mean: For instance, a web application I consume the rest API and have no control returns: HTTP status code 200 HTTP body {"foo": "bar"} With Spring Webflux, we can easily write something like: public Mono<FooBar> sendRequest(SomeRequest someRequest) { return webClient.mutate() .baseUrl("https://third-party-rest-api.com:443") .build() .post() .uri(/someroute) .body(BodyInserters.fromValue(someRequest)) .retrieve().bodyToMono(FooBar.class); } public class FooBar { private

2022-05-02 03:25:10    分类:问答    java   spring-boot   spring-webflux

How does Spring (Spring Boot) provide CSRF token to frontend?

I am trying to add CSRF protection to a project which is based on Spring-Boot (particularly WebFlux). What I have tried so far is below security configuration. @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http. ... .and() .csrf() .csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()) .requireCsrfProtectionMatcher( new NegatedServerWebExchangeMatcher( new OrServerWebExchangeMatcher( Arrays.asList( ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, FORM_LOGIN_URL), ServerWebExchangeMatchers .pathMatchers(HttpMethod.GET

2022-05-02 02:50:51    分类:问答    angular   spring   spring-boot   spring-security   spring-webflux

Java Spring Webflux, logging the time taken for an outbound http call

Small question regarding how to log the time spent for a http request please. To avoid confusion, this question is regarding logs (not metrics), this question is about Webflux, this question is about outbound call, I am the client, trying to make a call to a server, and I need to time this operation, but from a log perspective. Based on this snippet I am using: @Override public Mono<String> sendOutboundRequest() { return webClient.post().retrieve().bodyToMono(String.class); } So far, I tried .log() but this is not writing the time spent directly, this solution needs another layer of parsing of

2022-05-01 21:50:31    分类:问答    java   spring-webflux