我正在使用REST学习Spring Security,我有2个MySQL表,一个用于用户,一个用于授权。权限表中的user_id是用户表中id的外键。我想使用带有弹簧安全性的授权表中的这些角色进行基于角色的授权。但是我一开始就有问题。
当我尝试执行POST请求以在这2个表中插入一些数据时,authorities表中的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;
}
}
REST控制器:
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。
我想知道如何解决此问题,我们将不胜感激。