spring-security 错误原因:java.lang.非法参数异常:实体必须具有ID

cngwdvgl  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(129)

我不明白这是什么样的神秘主义.这个方法位于类UserService中:

@Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = repository.getByEmail(email.toLowerCase());
        if (user == null) {
            throw new UsernameNotFoundException("User " + email + " isn't found");
        }
       AuthorizedUser authorizedUser = new AuthorizedUser(user); // 1
       Objects.requireNonNull(authorizedUser);                   // 2
       System.out.println(new AuthorizedUser(user) + " 1");      // 3
       int userId = authorizedUser.getId();                      // 4
       return authorizedUser;                                    // 5
    }

在注解中编号的行中,为什么第1-3行的一切都很顺利,第3行转到控制台:

UserTo{id=100000, name='VadimUserAdmin', email='vadim@gmail.com'} 1

在第4行中,抛出了一个异常:

Caused by: java.lang.IllegalArgumentException: Entity must has id
    at org.springframework.util.Assert.notNull(Assert.java:201)
    at topjava.quest.HasId.id(HasId.java:14)
    at topjava.quest.AuthorizedUser.getId(AuthorizedUser.java:22)
    at topjava.quest.service.UserService.loadUserByUsername(UserService.java:64)

我该怎么处理这个?
Spring.安全性.版本-5.6.2
休眠。版本-5.6.5.最终版
类授权用户:

public class AuthorizedUser extends org.springframework.security.core.userdetails.User {

    @Serial
    private static final long serialVersionUID = 1L;

    private UserTo userTo;

    public AuthorizedUser(User user) {
        super(user.getEmail(), user.getPassword(), true, true, true, true, user.getRoleSet());
        setUserTo(Util.userAsTo(user));
    }

    public int getId() {
        return userTo.id();
    }

    public void setUserTo(UserTo userTo) {
        userTo.setPassword(null);
        this.userTo = userTo;
    }

    @Override
    public String toString() {
        return userTo.toString();
    }
}

类UserTo:

public class UserTo extends BaseTo implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @NotBlank
    @Size(min = 2, max = 100)
    @ApiModelProperty(example = "New name")
    private final String name;

    @Email
    @NotBlank
    @Size(max = 100)
    @ApiModelProperty(example = "newmame@gmail.com")
    private final String email;

    @NotBlank
    @Size(min = 6, max = 32)
    @ApiModelProperty(example = "newmame123")
    private String password;

    @ConstructorProperties({"id", "name", "email", "password"})
    public UserTo(Integer id, String name, String email, String password) {
        super(id);
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "UserTo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

类BaseTo:

public class BaseTo implements HasId {

    @ApiModelProperty(hidden = true)
    protected Integer id;

    public BaseTo() {
    }

    public BaseTo(Integer id) {
        this.id = id;
    }

    @Override
    public Integer getId() {
        return null;
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }
}

类具有ID:

public interface HasId {
    Integer getId();
    void setId(Integer id);

    default boolean isNew() {
        return getId() == null;
    }

    default int id() {
        Assert.notNull(getId(), "Entity must has id");
        return getId();
    }
}

我做错了什么?

w3nuxt5m

w3nuxt5m1#

调用HasId.id时,从BaseTo.class调用方法getId(),该方法返回null。将该方法更改为

getId(){
  return this.id;
}

相关问题