User login occurs via email and password. How to make the page display an authorized user not by email, but by any other field from the database. I use UserDetailsService.

@Transactional(readOnly = true) public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { User user = userRepository.findByEmail(email); Set<GrantedAuthority> grantedAuthorities = new HashSet<>(); for (Role role : user.getRoles()) { grantedAuthorities.add(new SimpleGrantedAuthority(role.getName())); } return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), grantedAuthorities); } 

To return the logged in user using the method

  public String findLoggedInUsername() { Object userDetails = SecurityContextHolder.getContext().getAuthentication().getDetails(); if (userDetails instanceof UserDetails) { return ((UserDetails) userDetails).getUsername(); } return null; } 

    1 answer 1

     <div th:text="${#authentication.name}"></div>