用户角色系统和与其他实体的映射问题 Spring 启动

System of User-Role and mapping with the other entities issue Spring boot

我尝试使用 spring 启动、spring 安全、Hibernate/JPA 和 JWT 在我的应用程序中设置角色用户系统,所以我首先创建了两个实体一:

AppUser 实体:

@Entity
@Data
@AllArgsConstructor @NoArgsConstructor

public class AppUser implements Serializable {

    @Id @GeneratedValue
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    @ManyToMany(fetch = FetchType.EAGER)
    private Collection<AppRole> roles = new ArrayList<>();

}

AppRole 实体:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AppRole implements Serializable {

    @Id @GeneratedValue
    private Long id;
    private String roleName;

}

因此,使用 jwt 和 spring 安全性对不同用户(管理员、客户端)进行身份验证一切顺利。

在做这个系统之前,我创建了两个实体:admin 和 client,但是正如你现在看到的这个新实现,我只有一个名为 AppUser 的实体(因为 admin 和 client 具有相同的属性)。

这个应用程序现在让我着迷的问题是管理员应该管理一些实体,包括 'Client'(现在不存在),'Projects',和 客户应管理实体 'Intervention'。

项目实体:

 @Entity
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
   public class Project implements Serializable{

    @Id @GeneratedValue
    private long id;
    private String description;


      }

遵循旧的 UML_Classes 示例:新实施之前的项目 + 管理员:

UML classe

那么我将如何实现这些实体之间的关系呢?例如,如果他是管理员,实体 AppUser 将如何管理实体项目?

等等

如果有任何有用的东西请 link 谢谢你将我重定向到它,我不知道要搜索什么才能得到我想要的东西。

PS:此应用程序被视为 API 以 spring 引导作为后端,以 angular4 客户端作为前端,在客户端中我可以得到哪个用户是经过身份验证和他获得的角色,我想知道我将如何实现其他实体,例如管理员管理的项目,ORM 端,这样我就可以在数据库 Mysql 中看到所有这些实体,我应该添加任何角色或什么?

提前致谢。

如果计划为管理功能和用户级功能提供单独的 API 端点,那么您可以使用 Web 安全配置来要求身份验证,并且只允许特定角色调用这些端点。
如果端点不是分开的,那么您仍然可以使用 spring 身份验证,但必须获取当前用户和角色,并根据他们试图执行的操作授权或拒绝。spring security java config

编辑
如果你打算专业地做这件事,你可以看看 Auditable 这会将用户和时间戳信息添加到您的记录中

如果您想手动执行此操作,请将 String createdByowner 添加到对象。然后在 jpa interface 中为对象添加方法定义,如 public List<Thing> getThingByOwner(String username);