Skip to content

MFA should not merge Authentication instances with different Principal.getName() #18112

@zyro23

Description

@zyro23

Describe the bug
when using the new MFA support with factors belonging to different principals, you end up with an Authentication that has the principal of the last factor and all authorities of the different principals.

To Reproduce

  • require multiple factors
  • authenticate using one factor
  • authenticate using a second factor as a different user

Expected behavior

  • i guess either the authentication attempt for the second factor should fail
  • or the authentication from the first factor should be discarded

Sample

sample app (password and ott) will be referenced asap:

  • run
  • login as user / password
  • request an ott for username admin (i.e. remove the username input readonly attribute)
  • use the generated ott (console out / info log)
  • see log for the resulting Authentication
2025-10-28T05:52:12.326+01:00 DEBUG 23184 --- [demo] [nio-8080-exec-6] s.w.a.o.OneTimeTokenAuthenticationFilter : Set SecurityContextHolder to OneTimeTokenAuthentication [Principal=org.springframework.security.core.userdetails.User [Username=admin, Password=[PROTECTED], Enabled=true, AccountNonExpired=true, CredentialsNonExpired=true, AccountNonLocked=true, Granted Authorities=[ROLE_ADMIN]], Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[ROLE_ADMIN, FactorGrantedAuthority [authority=FACTOR_OTT, issuedAt=2025-10-28T04:52:12.323991700Z], ROLE_USER, FactorGrantedAuthority [authority=FACTOR_PASSWORD, issuedAt=2025-10-28T04:49:26.419396Z]]]

note that the behavior is the same, if, for example, the first factor is a passkey and the second is username/password.

Metadata

Metadata

Assignees

Labels

in: coreAn issue in spring-security-coretype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions