Авторизация пользователя происходит по Email и пароль. Как сделать чтобы на странице отображался авторизованный пользователь не по Email, а по любому другому полю из базы. Использую 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);
}
Чтобы вернуть залогиненного пользователя использую метод
public String findLoggedInUsername() {
Object userDetails =
SecurityContextHolder.getContext().getAuthentication().getDetails();
if (userDetails instanceof UserDetails) {
return ((UserDetails) userDetails).getUsername();
}
return null;
}