0

我用 REST 学习 Spring Security,我有 2 个 MySQL 表,一个用于用户,一个用于权限。权限表中的 user_id 是用户表中 id 的外键。我想将权限表中的这些角色与 Spring Security 一起用于基于角色的授权。但是我一开始就有问题。

当我尝试执行 POST 请求以在这两个表中插入一些数据时,权限表中的 user_id 为 NULL。

这是代码:

用户实体:

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class ApplicationUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String username;
    private String password;

    @OneToMany(mappedBy="applicationUser", cascade=CascadeType.ALL)
    private List<Authorities> authorities;

    public ApplicationUser() {

    }

    public long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public List<Authorities> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(List<Authorities> authorities) {
        this.authorities = authorities;
    }

}

机关单位:

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class Authorities {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String role;

    @JsonIgnore
    @ManyToOne(cascade= {CascadeType.DETACH, CascadeType.MERGE, 
            CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="user_id")
    private ApplicationUser applicationUser;

    public Authorities() {

    }

    public long getId() {
        return id;
    }

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

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public ApplicationUser getApplicationUser() {
        return applicationUser;
    }

    public void setUser(ApplicationUser applicationUser) {
        this.applicationUser = applicationUser;
    }
}

休息控制器:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private ApplicationUserRepository applicationUserRepository;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping("/sign-up")
    public List<ApplicationUser> getUsers() {
        return applicationUserRepository.findAll();
    }

    @PostMapping("/sign-up")
    public void signUp(@RequestBody ApplicationUser user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        applicationUserRepository.save(user);
    }
}

当我尝试使用这些数据发出 POST 请求时:

{
   "username": "ion",
   "password": "1234",
   "authorities": [
   {
       "role": "ROLE_USER"
   }
   ]
}

响应为 200,但 MySQL 表如下所示:

在此处输入图像描述

在此处输入图像描述

所以,用户表看起来不错,但权限表的 user_id 单元格为 NULL,它应该是 82。

我想知道如何解决这个问题,任何反馈都将不胜感激。

4

0 回答 0