Access token not propagated in Spring Boot 2.7

Viewed 46

We use Spring Boot with an OIDC integration to provide authentication and authorization to our users. Our application acts as Client in the OIDC code flow, calling downstream Resource servers through Http requests to serve user requests. To have the Client pass the access token of the authenticated user to Resource servers we use the ServletOAuth2AuthorizedClientExchangeFilterFunction and apply that to the org.springframework.web.reactive.function.client.WebClient handling downstream requests.

We recently upgraded from Spring Boot 2.6.7 to 2.7.3 and discovered that the Authentication header containing the access token is no longer added to outgoing requests, if those requests are scheduled on a thread other than the one serving the original request:

public class MyController {

    public Mono<ProductSearchResult> searchByName(SearchProductsQuery query) {

        List<String> sortOrder = new ArrayList<>();

        System.out.println("--OUTER: " + Thread.currentThread().getName());
        return resourceServere.searchByName(query)
                .doOnNext(searchResponse -> sortOrder.addAll(searchResponse.getIds()))
                .flatMap(searchResponse -> Mono.zip(
                        getSomething(searchResponse.getIds()),
                        getSomethingElse(searchResponse.getIds()),
                        Mono.just(searchResponse.pagination())))
                .map(tuple3 -> SearchResultMapper.map(tuple3.getT1(), tuple3.getT2(), tuple3.getT3()));
    }

    private Mono<List<String>> getSomething(List<String> ids) {
        System.out.println("--INNER: " + Thread.currentThread().getName());

        if (ids.isEmpty()) {
            return Mono.just(new ArrayList<>());
        }
        return otherResourceServerClient.getStuff(ids);
    }
}

Which prints

--OUTER: http-nio-8080-exec-6
--INNER: reactor-http-nio-3

Debugging ServletOAuth2AuthorizedClientExchangeFilterFunction we discovered that the request in the http-nio-thread has the Authentication:

Debugger stopped in ServletOAuth2AuthorizedClientExchangeFilterFunction

whereas the request in the reactor-http thread does not:

Debugger stopped in ServletOAuth2AuthorizedClientExchangeFilterFunction

I can certainly provide more information about our setup, I'm just a bit uncertain what would be relevant. For starters though, we depend on both spring-starter-webflux and spring-boot-starter-web, as well as on spring-boot-starter-security

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

Curious to hear if anyone has experienced the same issue?

0 Answers
Related