The following code was working with spring boot 2.7.5, but it does not work anymore with version 2.7.6. ReactiveSecurityContextHolder.getContext() now returns empty.

@Component
public class BusinessClass {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  private final ComponentClass componentClass;

  public BusinessClass(ComponentClass componentClass) {
    this.componentClass = componentClass;
  }

  public RetrySpec getRetrySpec() {
    return Retry.max(1)
        .doBeforeRetryAsync(
            retrySignal ->
                ReactiveSecurityContextHolder.getContext()
                    .doOnSuccess(
                        securityContext ->
                            LOGGER.info(
                                "The securityContext is null : " + Objects.isNull(securityContext)))
                    .flatMap(securityContext -> componentClass.doBeforeRetry()));
  }
}

@Component
public class ComponentClass {

  public Mono<String> getValue() {
    return Mono.just("String value");
  }

  public Mono<Void> doBeforeRetry() {
    return Mono.empty();
  }
}

You can test the problem with the following test class. The test succeed with version 2.7.5 but not with 2.7.6

@ExtendWith(MockitoExtension.class)
class BusinessClassTest {

  @Mock ComponentClass componentClass;
  @Mock SecurityContext securityContext;

  @InjectMocks BusinessClass businessClass;

  @Test
  @DisplayName("doBusinessLogic should call 'doBeforeRetry' before retry")
  void doBusinessLogic() {

    PublisherProbe<Void> doBeforeRetryProbe = PublisherProbe.empty();
    Context context = ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext));

    given(componentClass.getValue())
        .willReturn(Mono.error(new RuntimeException()))
        .willReturn(Mono.just("someValue"));
    given(componentClass.doBeforeRetry()).willReturn(doBeforeRetryProbe.mono());

    StepVerifier.create(
            Mono.defer(() -> componentClass.getValue())
                .retryWhen(businessClass.getRetrySpec())
                .contextWrite(context))
        .expectNext("someValue")
        .verifyComplete();

    doBeforeRetryProbe.assertWasSubscribed();
  }
}

I'm not sure if the problem is with spring-boot or project-reactor since project-reactor was updated in the new spring-boot version. It could be this change: https://github.com/reactor/reactor-core/pull/3262/files#diff-a5639cea15f6765fecf7b44413831dc92cc0c8f45c1314ae8f4fb2959c60a623, but I'm really not sure.

Thanks in advance

Comment From: pascalgosselin

I was able to "correct" the problem with spring-boot 2.7.6 by downgrading reactor-core from 3.4.25 to 3.4.24. So I'm going to close this issue and open it in reactore-core

Comment From: scottfrederick

@pascalgosselin Thanks for letting us know. When you open the Reactor issue, you can add a comment here with a link to it in case others run into a similar problem.

Comment From: pascalgosselin

Here's the reactor-core issue: https://github.com/reactor/reactor-core/issues/3314