Forward port of issue #11347 to 6.0.x.

Comment From: marcusdacoregio

Duplicate of https://github.com/spring-projects/spring-security/issues/11940