Forward port of https://github.com/spring-projects/spring-security/pull/11827
Forward port of #11827