Forward port of issue #13939 to 6.1.x.

Comment From: marcusdacoregio

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