RBAC (role-based access control, role-based access controls) is where users are associated with permissions through roles.
Understanding that a user has multiple roles, a role has multiple permissions. constitutes a "user-role-permissions" authorization model. And the relationship between them is many-to-many.
For the user, login different account password, belong to different roles, can have different operations.
(before writing any code, think about what we are going to do, what we need to do about it, what the basics are, what the relationship is, and how to integrate them.)
If you don't think about it, writing down the code will cause repeated changes that waste time. )
1. First we need 3 tables The User Table Role Table action table, the User Table role table is a many-to-many relationship, the role table and the action table is a many-to-many relationship. So we need 2 relationship tables,
That is, User_role and role_action, with the knowledge for the hibernate many-to-many table association, the following paste code.
First, the T_user class.
PrivateString ID; PrivateString name; PrivateString age; PrivateString sex; Privateset<t_role> role =NewHashset<>(); PrivateString T_name; PrivateString T_password; @ManyToMany (Fetch=Fetchtype.eager) @JoinTable (name= "User_role", Joincolumns[Email protected] (name= "user_id", referencedcolumnname= "id"), Inversejoincolumns[Email protected] (name= "role_id", referencedcolumnname= "id") ) PublicSet<t_role>Getrole () {returnrole; } Public voidSetrole (set<t_role>role) { This. Role =role; }//other set get omitted
T_role class
PrivateString ID; PrivateString name; Privateset<t_user> user =NewHashset<>(); PrivateSet<menu> Menu =NewHashset<>(); PublicT_role () {} @ManyToMany (Fetch=Fetchtype.eager) @JoinTable (name= "User_role", Joincolumns[Email protected] (name= "role_id", referencedcolumnname= "id"), Inversejoincolumns[Email protected] (name= "user_id", referencedcolumnname= "id")) PublicSet<t_user>GetUser () {returnuser; } Public voidSetUser (set<t_user>user) { This. user =user; } @ManyToMany (Fetch=Fetchtype.eager) @JoinTable (name= "Role_menu", Joincolumns[Email protected] (name= "role_id", referencedcolumnname= "id"), Inversejoincolumns[Email protected] (name= "menu_id", referencedcolumnname= "id")) PublicSet<menu>GetMenu () {returnmenu; } Public voidSetMenu (set<menu>menu) { This. Menu =menu; }
Menu class (That is, action Class)
PrivateString ID; PrivateString name; PrivateString icon; PrivateString URL; PrivateString __parentid; PrivateString type; Privateset<t_role> role =NewHashset<>(); @ManyToMany (Fetch=Fetchtype.eager) @JoinTable (name= "Role_menu", Joincolumns[Email protected] (name= "menu_id", referencedcolumnname= "id"), Inversejoincolumns[Email protected] (name= "role_id", referencedcolumnname= "id")) PublicSet<t_role>Getrole () {returnrole; } Public voidSetrole (set<t_role>role) { This. Role =role; }//other setget omitted
Here is the connection table
Role_menu
Foreign key of Role_menu
The simple description below menu_id mapped the menu table role_id mapped the role table
Here is the User_role table
Foreign key of User_role
Ibid. is not in explanation;
As long as you understand the many-to-many relationship between Hibernate, the above table will be very simple.
With the 5 tables above, we can get the associated role from the user table, and then get the associated action from the role table. Vice versa.
The table is set up and we implement the basic data below.
Authorization design of SSH integrated Easyui