title: class org.springframework.security.core.userdetails.User cannot be cast
body: <p>I use SpringSecurity in my web-project. When I try to login with username and password, I have this error in my stacktrace :</p>
  <pre><code> class org.springframework.security.core.userdetails.User cannot be cast to class com.cesi.cuberil.service.JwtUser (org.springframework.security.core.userdetails.User and com.cesi.cuberil.service.JwtUser are in unnamed module of loader 'app')
      at com.cesi.cuberil.controller.AuthController.login(AuthController.java:116) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.2.jar:5.3.2]
  </code></pre>
  <p>Below, I will put classes where I think the problem comes from.
  Here my JwtUser.java :</p>
  <pre><code>package com.cesi.cuberil.service;
  
  import com.cesi.cuberil.model.User;
  import lombok.Data;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  
  import java.util.Collection;
  
  
  @Data
  public class JwtUser implements UserDetails {
  
  
      private final String username;
      private final String password;
      private final boolean enabled;
      private final Collection&lt;? extends GrantedAuthority&gt; authorities;
  
  
      private static final long serialVersionUID = 1L;
      private User user;
      public JwtUser(String username, String password, Boolean enabled,  Collection&lt;? extends GrantedAuthority&gt; authorities) {
  
          this.username = username;
          this.password = password;
          this.enabled = enabled;
          this.authorities = authorities;
      }
  
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          return authorities;
      }
  
      @Override
      public String getPassword() {
          return password;
      }
  
      @Override
      public String getUsername() {
          return username;
      }
  
      @Override
      public boolean isAccountNonExpired() {
          return true;
      }
  
      @Override
      public boolean isAccountNonLocked() {
          return true;
      }
  
      @Override
      public boolean isCredentialsNonExpired() {
          return true;
      }
  
      @Override
      public boolean isEnabled() {
          return enabled;
      }
  }
  
  </code></pre>
  <p>Here my AuthController.java :</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/api/v1/auth&quot;)
  @AllArgsConstructor
  public class AuthController {
      @Autowired
      AuthenticationManager authenticationManager;
  
      @Autowired
      PasswordEncoder encoder;
  
      @Autowired
      JwtUtils jwtUtils;
  
  
      @PostMapping(&quot;/login&quot;)
      public ResponseEntity&lt;?&gt; login(@Valid @RequestBody LoginRequest loginRequest) {
          Authentication authentication = authenticationManager.authenticate(
                  new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
  
          SecurityContextHolder.getContext().setAuthentication(authentication);
          String jwt = jwtUtils.generateJwtToken(authentication);
  
          JwtUser myUserDetails = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
          List&lt;String&gt; roles = myUserDetails.getAuthorities().stream()
                  .map(item -&gt; item.getAuthority())
                  .collect(Collectors.toList());
  
          return ResponseEntity.ok(new JwtResponse(jwt, myUserDetails.getUser().getUserId(), myUserDetails.getUsername(), myUserDetails.getUser().getEmail(), roles));
      }
  }
  </code></pre>
  <p>Here my UserDetailsServiceImpl.java :</p>
  <pre><code>@Service
  @AllArgsConstructor
  public class UserDetailsServiceImpl implements UserDetailsService {
      @Autowired
      private UserRepository userRepository;
  
      public User getByUsername(String username) {
          return userRepository.findByUsername(username);
      }
  
      @Override
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          User user = this.getByUsername(username);
          if (user == null) {
              throw new UsernameNotFoundException(&quot;user not found&quot;);
          } else {
              List&lt;GrantedAuthority&gt; listAuthorities = new ArrayList&lt;GrantedAuthority&gt;();
              listAuthorities.add(new SimpleGrantedAuthority(&quot;ROLE_USER&quot;));
              return new org.springframework.security.core.userdetails.User(username, user.getPassword(), true, true, true, true, listAuthorities);
          }
      }
  }
  </code></pre>
  <p>For information, I use java 11 and the lastest version of Spring Boot.</p>