To get username of the authenticated user, we have to use getName()
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String currentUser = auth.getName();
Refer: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/security/Principal.html#getName()