如何在用户-角色权限

时间:2018-09-07 16:33:52

标签: spring-boot jpa spring-data-jpa spring-data one-to-many

我有一个带有spring数据jpa的spring boot应用程序(Spring boot 2)。我有3个MYSQL表来存储用户信息,角色和权限

  • 用户将包含基本的用户详细信息,例如username,password firstname,lastname。
  • 角色代表用户角色,例如Admin,User,Staff,test(用户可以具有多个角色)
  • 权限具有3种可能,分别是读取,写入,自定义(每个角色都有很多权限)
  • UserRole-用户和角色的联合表
  • RolePermission-角色和权限的联合表

正在寻找Spring引导服务,如果未根据User表验证用户,则将返回false;如果成功验证了用户,则响应应包含用户角色和权限。

我能够构建示例休息服务,但无法设置以下内容

  1. 如何在此要求中为oneToMany映射配置实体类
  2. 存储库界面中对应的查询是什么

请参阅实体表

@Entity
@Table( name = "TURBINE_USER" )
public class PortalUser {

public PortalUser() {

}

public PortalUser(long userID ,String userName , String password , String firstName, String lastName, String email) {
    this.userID = userID;
    this.userName = userName;
    this.password = password;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email =  email;
}


@Id
@Column(name="USER_ID",unique=true)
private long userID;

@NotNull
@Column(name="LOGIN_NAME",unique=true)
private String userName;

@NotNull
@Column(name="PASSWORD_VALUE")
private String password;

@NotNull
@ColumnDefault("")
@Column(name="FIRST_NAME")
private String firstName;

@NotNull
@ColumnDefault("")
@Column(name="LAST_NAME")
private String lastName;

@Column(name="EMAIL")
private String email;

@Column(name="CONFIRM_VALUE")
private String confirmValue;

@NotNull

@Column(name="CREATED")
private Timestamp createdDt;

@NotNull

@Column(name="MODIFIED")
private Timestamp modified;

@NotNull
@Column(name="LAST_LOGIN")
private Timestamp lastLogin;

@Column(name="DISABLED")
private char disabled;

@Column(name="OBJECTDATA")
private byte[]  objectData;

@NotNull
@Column(name="PASSWORD_CHANGED")
private Timestamp passwordChanged;
 // getters and setters
}


@Entity
@Table(name =  "TURBINE_ROLE")
public class Role {
    @Id
    @Column(name=  "ROLE_ID",unique  = true)
    private long roleId;

    @Column(name= "ROLE_NAME")
    private String roleName;

}


@Entity
@Table(name ="TURBINE_PERMISSION")
public class Permission {

    @Id
    @Column(name=  "PERMISSION_ID")
    private long permissionId;

    @Column(name= "PERMISSION_NAME")
    private String name;
}

@Entity
@Table(name= "TURBINE_USER_GROUP_ROLE")
public class UserRoles {

    @Column(name="USER_ID")
    private PortalUser user;

    @Column(name="ROLE_ID")
    private Role roles;
}


@Entity
@Table(name="TURBINE_ROLE_PERMISSION")
public class RolePermission {

    @Column(name= "ROLE_ID")
    private Role roleId;

    @Column(name= "PERMISSIONID")
    private Permission permissionId;
}

1 个答案:

答案 0 :(得分:1)

用户与角色,角色和权限实体之间存在ManyToMany关系。您不应将Join表定义为实体,它们是由JPA实现生成/管理的。

例如,User和Role实体的ManyToMany注释:

public class PortalUser {
    ...

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "User_Role",
        joinColumns = {@JoinColumn(name = "user_id")},
        inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private Set<Role> roles;
}

经过CMobileCom JPA的测试。

免责声明:我是CMobileCom JPA的开发人员,这是一种适用于Java和Android的轻量级JPA实现。