org.springframework.security.core.userdetails.User无法强制转换为All.User

时间:2017-04-02 14:47:34

标签: spring spring-boot spring-security

我在尝试获取登录的活动用户名时遇到了ClassCastException。

以下错误 -

org.springframework.security.core.userdetails.User cannot be cast to All.User

错误来自的方法是:

    public String getCurrentUser(){
          String username;
          Object user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
          if(user instanceof UserDetails){
                username = ((UserDetails) user).getUsername();
            }
            else{
                username = "";
            }
            return username;

        }

我有一个UserServiceImpl类:

@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<>((Collection<? extends UserRole>) roleRepository.findAll()));
        userRepository.save(user);
    }

    @Override
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

}

用户类:

@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<AuctionItem>itemList;

    @OneToMany(mappedBy = "user")
    List<ItemSold>purchaseList;

    @OneToMany(mappedBy = "user")
    List<UserRole> role;

    private String passwordConfirm;

    public List<AuctionItem> getItemList() {
        return itemList;
    }

    public void setItemList(List<AuctionItem> itemList) {
        this.itemList = itemList;
    }

    public List<ItemSold> getPurchaseList() {
        return purchaseList;
    }

    public void setPurchaseList(List<ItemSold> purchaseList) {
        this.purchaseList = purchaseList;
    }

    public List<UserRole> getRole() {
        return role;
    }

    public void setRole(List<UserRole> 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;
    }


}

关于问题可能是什么的任何想法?

0 个答案:

没有答案