title: org.springframework.security.core.userdetails.User cannot be cast to All.User
body: <p>Hi i have been getting a ClassCastException when trying to get the active username that is logged in.</p>
  
  <p>Error below-</p>
  
  <pre><code>org.springframework.security.core.userdetails.User cannot be cast to All.User
  </code></pre>
  
  <p>The method where the error is coming from is: </p>
  
  <pre><code>    public String getCurrentUser(){
            String username;
            Object user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(user instanceof UserDetails){
                  username = ((UserDetails) user).getUsername();
              }
              else{
                  username = "";
              }
              return username;
  
          }
  </code></pre>
  
  <p>I have a UserServiceImpl class:</p>
  
  <pre><code>@Component
  public class UserServiceImpl implements UserService{
  
      @Autowired
      private UserRepository userRepository;
      @Autowired
      private UserRoleRepository roleRepository;
      @Autowired
      private BCryptPasswordEncoder bCryptPasswordEncoder;
  
      @Override
      public void save(User user) {
          user.setPass(bCryptPasswordEncoder.encode(user.getPass()));
          user.setRole(new ArrayList&lt;&gt;((Collection&lt;? extends UserRole&gt;) roleRepository.findAll()));
          userRepository.save(user);
      }
  
      @Override
      public User findByUsername(String username) {
          return userRepository.findByUsername(username);
      }
  
  }
  </code></pre>
  
  <p>User class:</p>
  
  <pre><code>@Entity
  @Table(name = "user")
  public class User {
      @Id
      @Column(name = "username")
      private String username;
  
      @Column(name ="pass")
      private String pass;
  
      @Column(name = "email")
      private String email;
  
      @Column(name = "firstname")
      private String firstName;
  
      @Column(name = "lastname")
      private String lastname;
  
      @Column(name = "address")
      private String address; 
  
      @Column(name = "telephone")
      private long phone;
  
      @OneToMany(mappedBy = "owner")
      List&lt;AuctionItem&gt;itemList;
  
      @OneToMany(mappedBy = "user")
      List&lt;ItemSold&gt;purchaseList;
  
      @OneToMany(mappedBy = "user")
      List&lt;UserRole&gt; role;
  
      private String passwordConfirm;
  
      public List&lt;AuctionItem&gt; getItemList() {
          return itemList;
      }
  
      public void setItemList(List&lt;AuctionItem&gt; itemList) {
          this.itemList = itemList;
      }
  
      public List&lt;ItemSold&gt; getPurchaseList() {
          return purchaseList;
      }
  
      public void setPurchaseList(List&lt;ItemSold&gt; purchaseList) {
          this.purchaseList = purchaseList;
      }
  
      public List&lt;UserRole&gt; getRole() {
          return role;
      }
  
      public void setRole(List&lt;UserRole&gt; role) {
          this.role = role;
      }
  
  
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPass() {
          return pass;
      }
  
      public void setPass(String pass) {
          this.pass = pass;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      public long getPhone() {
          return phone;
      }
  
      public void setPhone(long phone) {
          this.phone = phone;
      }
  
      public String getPasswordConfirm() {
          return passwordConfirm;
      }
  
      public void setPasswordConfirm(String passwordConfirm) {
          this.passwordConfirm = passwordConfirm;
      }
  
  
  }
  </code></pre>
  
  <p>Any ideas of what the problem could be?</p>