How can I display the current logged in User with Spring Boot Thymeleaf?

后端 未结 4 682
故里飘歌
故里飘歌 2021-01-18 19:18

I am trying to display the details of the current user however I keep getting errors. I tried accessing the authenticated user from the template but that did not work as I w

4条回答
  •  -上瘾入骨i
    2021-01-18 20:00

    The problem is here:

    return new 
    org.springframework.security.core.userdetails.User(user.getEmailAddress(),
            user.getPassword(),
            mapRolesToAuthorities(user.getRoles()));
    

    You lose the reference to your User entity. Change it to:

    return user;
    

    For this to work, you need to update your User entity to implement UserDetails interface:

    public class User implements UserDetails {
        // some new methods to implement
    }
    

    Then, your Thymleaf code should work. Another way of getting the firstName would be:

    
    

提交回复
热议问题