带有额外字段的Spring Boot UserDetailsservice多用户
我有一个Spring Boot项目,该项目有3种类型的用户(管理员,专家,客户),并且该应用程序适用于在现场注册的专家,以提供诸如将计算机修复的服务诸如向网站询问帮助的客户固定计算机之类的服务。
我具有不同类型的用户
类型的继承。
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "USER_TYPE", discriminatorType = DiscriminatorType.INTEGER)
public abstract class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private Set<String> roles = new HashSet<>();
// getter & setter...
}
@Entity
@DiscriminatorValue("1")
public class Admin extends User {
}
@Entity
@DiscriminatorValue("2")
public class Expert extends User {
private Byte[] expertPhoto;
private String password;
// some other fields & getter & setter...
}
@Entity
@DiscriminatorValue("3")
public class Customer extends User {
private Long credit;
private Set<CustomerOrder> orders = new HashSet<>();
// some other fields & getter & setter...
}
我想使用Spring Boot Security并实现UserDetailsservice,我的问题是当我有不同的用户类型(专家,客户等)时如何设计?
我希望用户能够使用一个用户名担任不同的角色(管理员,专家,客户)。
我应该如何设计我的系统来解决这些要求?
I have a spring boot project that has 3 types of users (Admin, Expert, Customer) and the application is for Experts that register on site for giving services like fixing computers to Customers that are asking help in site.
I have an inheritance of different kind of User
types as following.
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "USER_TYPE", discriminatorType = DiscriminatorType.INTEGER)
public abstract class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private Set<String> roles = new HashSet<>();
// getter & setter...
}
@Entity
@DiscriminatorValue("1")
public class Admin extends User {
}
@Entity
@DiscriminatorValue("2")
public class Expert extends User {
private Byte[] expertPhoto;
private String password;
// some other fields & getter & setter...
}
@Entity
@DiscriminatorValue("3")
public class Customer extends User {
private Long credit;
private Set<CustomerOrder> orders = new HashSet<>();
// some other fields & getter & setter...
}
I want to use spring boot security and implement UserDetailsService, my problem is that how to design when I have different User types (Expert, Customer, etc.)?
I want users to be able to have different roles (admin, expert, customer) with one username.
How should I design my system to solve these requirements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的角色模式似乎有些偏离。最好拥有一种
用户
的单一类型,然后用新的角色
实体填充它。新的用户
实体将看起来如下:角色
实体看起来像这样:然后,您需要实现
org.springframework.security.core.core。 UserDetails.user
接口用作userDetailsService
的Spring Security Class user 的具体实现。请注意,此类也称为用户
,并且与系统上的用户
类不同。然后,您可以通过实现
org.springframework.security.core.core.userdetails.userdetailsservice
,可以创建userDetailsService
。您将获得
userDetailsservice
是在我们刚创建的myuserdetail
格式中加载用户。这样就是这样:Your role modal seems a bit off. It is better to have a single type of
User
and fill it with list of a newRole
entity. The newUser
entity will look like the following:And the
Role
entity will look like this:Then, you need to implement
org.springframework.security.core.userdetails.User
interface to use as a concrete implementation of spring security classUser
on yourUserDetailsService
. Notice that this class is also calledUser
and is different than theUser
class on your system.Then, you can create your
UserDetailsService
by implementingorg.springframework.security.core.userdetails.UserDetailsService
of spring security.What you will achieve
UserDetailsService
is to load the user in theMyUserDetail
format we just created. It will be something like this: