我有3个MySQL表,并且正在使用它们进行具有角色的Spring Security配置。我想使用RESTful填充其中的2个。
该应用程序可以成功运行,但此刻我从MySQL Workbench填充这些表,并希望使用RESTful填充它们。但是我不知道该怎么做,当我执行POST请求时,JSON对象应该是什么样子?例如,角色表默认情况下具有以下两个值:ROLE_ADMIN和ROLE_USER,因此我不想在此表中添加任何新值,但是如何添加新用户并填充用户表和user_role表?
这些是实体:
用户实体:
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.JoinColumn;
@Entity
@Table(name = "appuser")
public class ApplicationUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private long id;
private String username;
private String password;
@ManyToMany(fetch = FetchType.EAGER,
cascade = {CascadeType.DETACH, CascadeType.MERGE,
CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "appuser_role",
joinColumns = @JoinColumn(name = "appuser_id"),
inverseJoinColumns = @JoinColumn(name = "approle_id"))
private List<Role> 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<Role> getAuthorities() {
return authorities;
}
public void setAuthorities(List<Role> authorities) {
this.authorities = authorities;
}
}
角色实体:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.springframework.security.core.GrantedAuthority;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "approle")
public class Role {
@Id
@Column(name = "role_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String authority;
@JsonIgnore
@ManyToMany(mappedBy = "authorities")
private Set<ApplicationUser> users = new HashSet<>();
public Role() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public Set<ApplicationUser> getUsers() {
return users;
}
public void setUsers(Set<ApplicationUser> users) {
this.users = users;
}
@Override
public String toString() {
return authority;
}
}