Forward port of issue #9159 to 6.0.x.

Comment From: marcusdacoregio

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