![]() ![]() If you’re new to Spring Data JPA, kind check this quick start guide. We define the getUserByUsername() method annotated by a JPA query to be used by Spring Security for authentication. }This interface is a subtype of CrudRepository defined by Spring Data JPA so Spring will generate implementation class at runtime. Public interface UserRepository extends CrudRepository u FROM User u WHERE u.username = :username") Code UserRepositoryNext, create the UserRepository interface with the following code: package net.codejava Refer to this tutorial for details about Hibernate many-to-many relationship mapping. }Here, you can see we use a Set of Roles in the User class to map a unidirectional many-to-many association from User to Role, e.g. remaining getters and setters are not shown for brevity Private boolean = CascadeType.ALL, fetch = "users_roles", Public class User = GenerationType.IDENTITY) }The second class is User: package net.codejava Public class Role = GenerationType.IDENTITY) The first class is Role: package net.codejava Code Entity ClassesNext, we need to create two entity classes that map with the tables users and roles in the database. Note that by default, if the Spring Security library present in the classpath, the users must login to use the application. Configure DependenciesMake sure that the Maven build file includes the following dependency declaration for Spring Web, Spring Data JPA, Spring Security, Thymeleaf, MySQL JDBC driver and Thymeleaf extras for Spring Security: =passwordModify URL, username and password matching your MySQL database. Configure Data Source PropertiesTo use Spring Boot with Spring Data JPA and Hibernate, configure database connection information in the application.properties as follows: -auto=none INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (5, 4) - user admin has role ADMINSo that’s the setup for the database. INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (4, 3) - user namhm has role EDITOR INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (4, 2) - user namhm has role CREATOR INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (3, 3) - user john has role EDITOR INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (2, 2) - user alex has role CREATOR INSERT INTO `users` (`username`, `password`, `enabled`) VALUES ('admin', '$2a$10$IqTJTjn39IU5.7sSCDQxzu3xug6z/LPU6IF0azE/8CkHCwYEnwBX.', '1') Note that the passwords are encoded in BCrypt format and same as usernames.And execute the following script to assign permissions to users based on the table above: INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (1, 1) - user patrick has role USER INSERT INTO `users` (`username`, `password`, `enabled`) VALUES ('namhm', '$2a$10$GQT8bfLMaLYwlyUysnGwDu6HMB5G.tin5MKT/uduv2Nez0.DmhnOq', '1') ![]() INSERT INTO `users` (`username`, `password`, `enabled`) VALUES ('john', '$2a$10$E2UPv7arXmp3q0LzVzCBNeb4B4AtbTAGjkefVDnSztOwE7Gi圆kea', '1') ![]() INSERT INTO `users` (`username`, `password`, `enabled`) VALUES ('alex', '$2a$10$.tP2OH3dEG0zms7vek4ated5AiQ.EGkncii0OpCcGq4bckS9NOULu', '1') INSERT INTO `roles` (`name`) VALUES ('ADMIN') Run the following SQL statements to create 5 users in the users table: INSERT INTO `users` (`username`, `password`, `enabled`) VALUES ('patrick', '$2a$10$cTUErxQqYVyU2qmQGIktpup5chLEdhD2zpzNEyYqmxrHHJbSNDOG.', '1') INSERT INTO `roles` (`name`) VALUES ('EDITOR') INSERT INTO `roles` (`name`) VALUES ('CREATOR') `role_id` int(11) NOT NULL AUTO_INCREMENT,ĬONSTRAINT `role_fk` FOREIGN KEY (`role_id`) REFERENCES `roles` (`role_id`),ĬONSTRAINT `user_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)Įxecute the following INSERT statements to insert 4 roles into the roles table: INSERT INTO `roles` (`name`) VALUES ('USER') `user_id` int(11) NOT NULL AUTO_INCREMENT, That’s why we need to have the intermediate table users_roles to realize that many-to-many association.You can execute the following MySQL script to create these tables: CREATE TABLE `users` ( The entity relationship between users and roles is many-to-many because a user can have one or more roles and a role can be assigned to be one or more users. Design and Create TablesFor role-based authorization with credentials and authorities stored in database, we have to create the following 3 tables: The userstable stores credentials and the roles table stores authorities (rights). ![]() The role USER allows user to view all products the role CREATOR is permission to create new products the role EDITOR is for editing products and the role ADMIN gives all permissions to the users. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |