gpt4 book ai didi

java - Spring 中未找到默认构造函数

转载 作者:行者123 更新时间:2023-12-01 11:10:01 24 4
gpt4 key购买 nike

我刚刚将我的类移动到一个使用 Spring Security Authentication User 类扩展的对象,但我无法弄清楚我需要构建哪个默认构造函数!如果我输入类的参数,它会将它们转换为静态并使 hibernate 无法映射该类,如果我什么都不输入,它会抛出错误!

@Entity
@Table(name="USER")
public class UserData extends User {


@Id
@GeneratedValue(strategy=GenerationType.AUTO)
int iduser;
int accountstatus;
public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPass(String password) {
this.password = password;
}
String username;
String password;

//Profile Data
String nomprofile;
String prenprofile;
String mailprofile;
String adressprofile;
int phoneprofile;
Date datenaissanceprofile;
char sexeuser;
String imagepath;

public UserData() {
super(username, password, authorities); //throws error
}

public UserData(String username,
String password,
boolean enabled,
boolean accountNonExpired,
boolean credentialsNonExpired,
boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities,
int iduser,
int accountstatus,
String nomprofile,
String prenprofile,
String mailprofile,
String adressprofile,
int phoneprofile,
Date datenaissanceprofile,
char sexeuser,
String imagepath) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.username = username;
this.password = password;
this.iduser = iduser;
this.accountstatus = accountstatus;
this.nomprofile = nomprofile;
this.prenprofile = prenprofile;
this.mailprofile = mailprofile;
this.adressprofile = adressprofile;
this.phoneprofile = phoneprofile;
this.datenaissanceprofile = datenaissanceprofile;
this.sexeuser = sexeuser;
this.imagepath = imagepath;
}

public int getIduser() {
return iduser;
}
public void setIduser(int iduser) {
this.iduser = iduser;
}


public int getAccountstatus() {
return accountstatus;
}
public void setAccountstatus(int accountstatus) {
this.accountstatus = accountstatus;
}


public String getNomprofile() {
return nomprofile;
}
public void setNomprofile(String nomprofile) {
this.nomprofile = nomprofile;
}
public String getPrenprofile() {
return prenprofile;
}
public void setPrenprofile(String prenprofile) {
this.prenprofile = prenprofile;
}
public String getMailprofile() {
return mailprofile;
}
public void setMailprofile(String mailprofile) {
this.mailprofile = mailprofile;
}
public String getAdressprofile() {
return adressprofile;
}
public void setAdressprofile(String adressprofile) {
this.adressprofile = adressprofile;
}
public int getPhoneprofile() {
return phoneprofile;
}
public void setPhoneprofile(int phoneprofile) {
this.phoneprofile = phoneprofile;
}
public Date getDatenaissanceprofile() {
return datenaissanceprofile;
}
public void setDatenaissanceprofile(Date datenaissanceprofile) {
this.datenaissanceprofile = datenaissanceprofile;
}
public char getSexeuser() {
return sexeuser;
}
public void setSexeuser(char sexeuser) {
this.sexeuser = sexeuser;
}
public String getImagepath() {
return imagepath;
}
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
}

最佳答案

与其从 spring-security 扩展用户类,不如实现 spring security 给出的 UserDetails 接口(interface)。请阅读下面的代码。为您的项目提供自定义 userDetailsS​​ervice 实现。我希望这对您有帮助-http://docs.spring.io/spring-security/site/docs/3.0.x/reference/technical-overview.html#d0e1613

 public class User implements UserDetails{

private UserData userData;
private List<String> roles;

public User(UserData user){
this.userData=user;

}



public Collection<? extends GrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> authorities=new ArrayList<SimpleGrantedAuthority>();
for (String role : userData.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}

public String getPassword() {
return user.getPassword();
}

public String getUsername() {
// TODO Auto-generated method stub
return user.getUsername();
}

public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}

public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}

public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}

public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}

}

关于java - Spring 中未找到默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32488881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com