Forward port of issue #13940 to 6.2.x.

Comment From: marcusdacoregio

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