title: How can I display the current logged in User with Spring Boot Thymeleaf?
body: <p>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 was getting this error: </p>
  
  <p>Method getFirstName() cannot be found on org.springframework.security.core.userdetails.User type</p>
  
  <p>I was trying to get the information from a controller and then saving it in a string and passsing the string to a template but that wasn't working either.</p>
  
  <p>Here is my SecurityConfig class:</p>
  
  <pre><code>    @Configuration
   public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  private UserService userService;
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http
              .authorizeRequests()
                  .antMatchers(
                          "/registration",
                          "/js/**",
                          "/css/**",
                          "/img/**",
                          "/webjars/**").permitAll()
                  .anyRequest().authenticated()
              .and()
                  .formLogin()
                      .loginPage("/login")
                          .permitAll()
              .and()
                  .logout()
                      .invalidateHttpSession(true)
                      .clearAuthentication(true)
                      .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                      .logoutSuccessUrl("/login?logout")
              .permitAll();
  }
  
  @Bean
  public BCryptPasswordEncoder passwordEncoder(){
      return new BCryptPasswordEncoder();
  }
  
  @Bean
  public DaoAuthenticationProvider authenticationProvider(){
      DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
      auth.setUserDetailsService(userService);
      auth.setPasswordEncoder(passwordEncoder());
      return auth;
  }
  
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.authenticationProvider(authenticationProvider());
  
  }
  </code></pre>
  
  <p>Here is my UserService Class:</p>
  
  <pre><code> public interface UserService extends UserDetailsService {
  
  User findByEmailAddress(String emailAddress);
    //  User findByFirstName(String firstName);
  
  User save(UserRegistrationDto registration);
  }
  </code></pre>
  
  <p>Here is my UserServiceImpl class:</p>
  
  <pre><code> @Service
  public class UserServiceImpl implements UserService {
  
  @Autowired
  private UserRepository userRepository;
  
  @Autowired
  private BCryptPasswordEncoder passwordEncoder;
  
  @Override
  public UserDetails loadUserByUsername(String emailAddress) throws 
  UsernameNotFoundException {
      User user = userRepository.findByEmailAddress(emailAddress);
      if (user == null){
          throw new UsernameNotFoundException("Invalid username or 
  password.");
      }
      return new 
  org.springframework.security.core.userdetails.User(user.getEmailAddress(),
              user.getPassword(),
              mapRolesToAuthorities(user.getRoles()));
  }
  
  public User findByEmailAddress(String emailAddress){
      return userRepository.findByEmailAddress(emailAddress);
  }
  
  public User save(UserRegistrationDto registration){
      User user = new User();
      user.setFirstName(registration.getFirstName());
      user.setSurname(registration.getSurname());
      user.setEmailAddress(registration.getEmailAddress());
      user.setPassword(passwordEncoder.encode(registration.getPassword()));
      user.setRoles(Arrays.asList(new Role("ROLE_USER")));
      return userRepository.save(user);
  }
  
  private Collection&lt;? extends GrantedAuthority&gt; 
  mapRolesToAuthorities(Collection&lt;Role&gt; roles){
      return roles.stream()
              .map(role -&gt; new SimpleGrantedAuthority(role.getName()))
              .collect(Collectors.toList());
  }
  
  
  }
  </code></pre>
  
  <p>Here is some code from the template class where I'm trying to get the information:</p>
  
  <p>th:text ="${#authentication.getPrincipal().getFirstName()}"></p>
  
  <p>th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></p>
  
  <p> </p>
  
  <p>This is the login controller. The parts I have commented out was another way I was trying to get the current users details:</p>
  
  <pre><code>@Controller
  //@RequestMapping("/login")
  public class MainController {
  
  //    @GetMapping("/")
  //    public String root() {
  //        return "userProfile1";
  //    }
  
  @GetMapping("/login")
  public String login(Model model) {
      return "login";
  
  }
  
   //   @GetMapping
    //  public String displayUserAccount(@ModelAttribute("user") @Valid             
  UserRegistrationDto userDto, BindingResult result, Model model) {
  //    
  // 
  //      model.addAttribute("firstName", ((UserRegistrationDto)         
  auth).getEmailAddress());
  //      
  //      model.addAttribute("emailAddress", userDto.getEmailAddress());
  //        model.addAttribute("firstName", userDto.getFirstName());
  //        model.addAttribute("surname", userDto.getSurname());
  //        model.addAttribute("age", userDto.getAge());
  //        model.addAttribute("gender", userDto.getGender());
  //        model.addAttribute("dob", userDto.getDob());
  //       // return "redirect:/registration?success";
    //  return "userProfile1";
  //      
    //  }
  
  @ResponseBody
  public String currentUserName(Authentication auth) {
      ((UserRegistrationDto) auth).getEmailAddress();
      return  "userProfile1";
  
  
  }
  
  
    } 
  </code></pre>
  
  <p>This is all over the place I'm sorry! Thanks so much for anyone who helps :D</p>