gpt4 book ai didi

Spring Boot Data Rest JPA - 实体自定义创建(用户)

转载 作者:行者123 更新时间:2023-12-04 19:03:18 27 4
gpt4 key购买 nike

我正在努力学习Spring。我使用以下工具使用 Spring Boot 创建了一个项目:

  • Spring 数据 JPA
  • Spring 数据 REST
  • Spring HATEOAS
  • Spring 安全

  • 我正在尝试创建一个 User 实体。我希望用户有一个加密的密码(+ salt)。

    当我对 POST 执行 /api/users 时,我成功地创建了一个新用户。
    {
    "firstname":"John",
    "lastname":"Doe",
    "email":"johndoe@example.com",
    "password":"12345678"
    }

    但我有两个问题:
  • 密码以明文形式保存
  • 盐为空

  • +----+---------------------+-----------+----------+----------+------+
    | id | email | firstname | lastname | password | salt |
    +----+---------------------+-----------+----------+----------+------+
    | 1 | johndoe@example.com | John | Doe | 12345678 | NULL |
    +----+---------------------+-----------+----------+----------+------+


    我认为的问题是使用了默认构造函数而不是我创建的另一个构造函数。我是 Spring 和 JPA 的新手,所以我一定错过了一些东西。这是我的代码。

    User.java
    @Entity
    @Table(name = "users")
    public class User{

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    public String firstname;

    @Column(nullable = false)
    public String lastname;

    @Column(nullable = false, unique = true)
    public String email;

    @JsonIgnore
    @Column(nullable = false)
    public String password;

    @JsonIgnore
    @Column
    private String salt;

    public User() {}

    public User(String email, String firstname, String lastname, String password) {
    this.email = email;
    this.firstname = firstname;
    this.lastname = lastname;
    this.salt = UUID.randomUUID().toString();
    this.password = new BCryptPasswordEncoder().encode(password + this.salt);
    }


    @JsonIgnore
    public String getSalt() {
    return salt;
    }

    @JsonProperty
    public void setSalt(String salt) {
    this.salt = salt;
    }

    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 Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }

    public String getLastname() {
    return lastname;
    }

    public void setLastname(String lastname) {
    this.lastname = lastname;
    }

    @JsonIgnore
    public String getPassword() {
    return password;
    }

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

    }

    UserRepository.java
    public interface UserRepository extends JpaRepository<User, Long> {

    public User findByEmail(String email);

    public User findByEmailAndPassword(String email, String password);
    }

    Application.java
    @SpringBootApplication
    public class Application {


    public static void main(String[] args) {
    SpringApplication.run(Application .class, args);
    }

    }

    此外,如果有人发现我做错了什么,我想指出我应该在哪里/如何放置用户登录代码(解密)。

    谢谢。

    最佳答案

    所以,这是我解决我的问题的方法:我创建了一个 Controller 作为我的自定义端点,然后我创建了一个服务,我在其中放置了创建用户所需的逻辑。这是代码:

    UserController.java

    @Controller
    public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/api/register")
    @ResponseBody
    public Long register(@RequestBody User user) {
    return userService.registerUser(user);
    }

    ...

    }

    用户服务 .java
    @Service
    public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Long registerUser(User user) {
    user.setPassword(new BCryptPasswordEncoder().encode(password));
    userRepository.save(user);
    return user.getId();
    }

    ...

    }

    所以通过做一个 POST
    {
    "firstname":"John",
    "lastname":"Doe",
    "email":"johndoe@example.com",
    "password":"12345678"
    }

    /api/register 中,我现在可以使用散列密码创建用户。

    关于Spring Boot Data Rest JPA - 实体自定义创建(用户),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407169/

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