Description
Summary
Trying oauth2 client_credentials integration with webclient but getting the IllegalStateException.
Configuration
Using a custom provider. Here's the configuration:
spring:
security:
oauth2:
client:
registration:
iam:
client-id: my_client_id
client-secret: my_secret
provider: as
clientAuthenticationMethod: basic
authorizationGrantType: client_credentials
provider:
as:
token-uri: https://my_authorization_server/as/oauth/token
My web security is as follows:
http
.authorizeExchange()
.pathMatchers(resourceServerProperties.getPublicEndpoints().toArray(new String[]{}))
.permitAll()
.anyExchange().authenticated()
.and()
.csrf()
.disable()
.oauth2ResourceServer()
.jwt()
.jwtDecoder(nimbusReactiveJwtDecoder)
.and()
.and()
.oauth2Client()
Configuring webclient as follows:
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepo,
authorizedClientRepo);
oauth.setDefaultClientRegistrationId(iamReactiveClientProperties.getDefaultClientRegistrationName());
oauth.setDefaultOAuth2AuthorizedClient(true);
return WebClient
.builder()
.exchangeStrategies(ExchangeStrategies
.builder()
.codecs(clientDefaultCodecsConfigurer -> {
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON));
}).build())
.clientConnector(clientHttpConnector(iamReactiveClientProperties))
.filter(oauth)
.build()
Webclient usage:
webClient
.get()
.uri("https://link")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(User.class)
.log()
Logs:
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onSubscribe([Fuseable] MonoFlatMap.FlatMapMain)
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | request(unbounded)
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | request(unbounded)
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.P.2 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:23.023Z [reactor-http-nio-2] INFO r.M.FlatMap.1 - | onContextUpdate(Context2{interface org.springframework.web.server.ServerWebExchange=org.springframework.web.server.adapter.DefaultServerWebExchange@5011f74c, interface org.springframework.security.core.context.SecurityContext=MonoFlatMap})
10:50:24.024Z [reactor-http-nio-2] ERROR r.M.FlatMap.1 - | onError(java.lang.IllegalStateException: Only one connection receive subscriber allowed.)
10:50:24.024Z [reactor-http-nio-2] ERROR r.M.FlatMap.1 -
java.lang.IllegalStateException: Only one connection receive subscriber allowed.
at reactor.netty.channel.FluxReceive.startReceiver(FluxReceive.java:277)
at reactor.netty.channel.FluxReceive.subscribe(FluxReceive.java:127)
Version
Spring version: 5.1.1.RELEASE
Reactor Netty version: 0.8.2.RELEASE
Reactor Core version: 3.2.2.RELEASE
Sample
https://github.com/hfgbarrigas/oauth2client-webflux-error-sample