Forward port of https://github.com/spring-projects/spring-security/issues/17898

Comment From: jzheaux

Completed in d0f93fa6d8338149943ae640c53db07de827867f