Spring Boot Add Roles to User Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the video spring boot address 2 user i am nam amin from code java net through this video you will learn how to implement user role assignment function in a springboard application in details i will show you how to create a raw entity class and raw repository interface how to add rows to user in unit test how to add default role to user in registration and how to update rows of user in web form note that in this video i will start with a springboard project which he developed in the video springboot registration and login with database so i recommend you to follow that video before coming to this one we will be working on the java project using the flowing technology boot web spring data jpi with hybrid framework spring security time leaf and mysql database and i'm using the phone supply programs java development kit open jdk spring tools with ide sql server and my sql workbench all right let's start with this springboot project it is for user registration and login let me start this springboard application you see this is the main class this is the user entity class with the field id email password first name last name this is a spring security configuration file for authentication here you see okay this application has started yeah okay now let me access this springboard application in chrome browser here http localhost phone number att enter okay and in this application i can rest the new user here okay email name it got about it password first name last name click sign up okay you have signed up successfully and in the database you can see here in my sequel workbench the database called java db here in the user table here select rows and you can see we have a new row for the newly created user name i mean here okay now let me log in okay i am in and after i have logged in successfully it shows the user's listing page and currently there's only one user naming here so that's the functionality of the existing springboot application now we are going to update the project so a user can be assigned one or more rows so there will be a new table in the database the rows table with two columns id and name here and you know the entity relationship between users and rows is many too many a user can be assigned one or more rows and a row can be assigned to one or more users so that's why we need to have an intermediate table users closed here come back to the project and let's stop the application for now and we create a new entity class here new class row and in this entity class we have just two fields that are id and name and we use the entity annotation and the table annotation to map with the table or rows in the database rows here and for the id field here we specify the annotation id and generated value with the strategy is generation type dot identity so i think you are very familiar with these annotations okay and for the field name we use a column annotation and specify the attribute nullable equal force and the maximum length is about 45 characters for the row name and then generate the letter and setup methods for the fields in the class idea name the red okay and we need to have some constructors here in the red contractor using fuse here generate okay we need to have some contractors this contractor text only row name okay and another constructor that text only pro id here okay and another contractor that text id name and the default contractor which is required by hibernate like this okay that's the code for the raw entity class and in the user entity class we need to specify the many too many entity relationship with the rows okay so here we declare set collection of row here set row cross equal new headshot here okay and we need to use the annotation many too many and also use an annotation joy table yeah and we specify some attributes for the zoid table the name the joy table name is users underscore rose and column is enjoy column name is user underscore id and a reverse zoid column reverse enjoy column is the row underscore id okay that's for the many too many annotations my mistake you need to add many many many too many annotations like this okay so this is a mapping for the many too many entity between user and raw yeah okay and then we generate that and set a method for the set collection here rows here they are red and we also add a new method for adding a new row to this user add row and parameter is the row object here this rows add row okay that's it for the entity classic row and user now we can start the application to let hybrid framework as well at the tables as in the application properties file we specify the attribute i'm let ddr auto equipment here okay and in the control view here you see it executed the sql statements to create a table rows here and in the mutable users underscore rows here and let me check in the mysql workbench here refresh the database okay and you see the new table rows here with two columns id and name and the intermediate table users and rows here to remain the many too many entities between user and row here perfect right next let's create the raw repository interface to use a spring data jpi so create a new interface here role repository and extends jpi repository interface extends jpi repository here with the type of the entity is a role and time of the ide is long okay and then we write a unit test class for test creating some rows new class in the test source directory here the class name is new oral repository test okay this is a spring data jpa test so we need to use the annotation and data jpa test and we want to execute a sql statement against the real database so we set this annotation auto configure test and database uh the blast equal none when we want that hybrid we commit the data not rollback so we specify the analysis row by force yeah and in this test class sorry you need to add import for the none and then achieve you here none value here okay replace none okay and in this test class we need to have a reference to the row or repository row repository here okay alright and we write the test method assignment test method test correct rows okay then we put some raw objects here row user is a new row and passing the name is user and raw admin equal new row admin and row customer equal new row customer and then we proceed these three row objects into the database repo save or here list of user admin and customer and then we assert this test method like this repo file we got all the raw objects from the database list rows and we assert that the size of the list row is equal to three because here we have three or objects list rows not net size and is equal to three that's that's it okay and before running this test method now let me check in the database in the rows table here okay you see the table is empty now now let me run this library test method run as resume test okay the test has been such a phone and in the console you can see it prints some sql statements insert into rows table here three times for three oral objects here surface i would and let me check in the database here executes the select sql statement here and you see now we have three rows inserted into the rows table here user admin and customer next last code a new unit test for positive use object associated with raw so in the project we have the user repository test class here okay and write a new test method test public void test create a new user sorry the method name should be test add narrow to new user okay and in this test class we need to have a reference to a raw repository so we change this name to user report private row repository row report okay and we copy this code to create a new new user object here no okay email is ravi.com.com password rv2020 first name ravi last name kumar and we need to add a raw object by name so in the raw repository interface here we declare new method here public row file by name with parameter is the role name okay and we use the custom query like this select from row where dot name equal to the value of the first parameter which is the name here okay here in the text method here we add a row object by name like this row repo file by name and we want to assign the row user to this news object okay overall user and then we add this row object to the user object or user.addrow row user here and then we proceed this user object user repo step user okay and we return a new accept user object here and we assert this test method like this set user get rows size is equal to 1 because this user object is associated with a raw object okay now save the change and before running this test method let me check in the database here in the user's table we have just only one user here and in the intermediate table users rows here it is empty okay now let me run this test method run as usually test okay the test has been so cellphone and you can see it prints the sql statement to select a row from the rows table by name here and then insert a new user to the user table and then insert a new row into the intermediable users and rows yeah now let me check my sql database here in the user table here you see we got a new row for the user ravi kumar inserted here the password is not encoded because i don't use the spring security password encoder no issue and in the user rows table here and you see a new row already inserted with user id 2 and row id 1 here user id 2 is the id of the new user here and row id one is a wrong user here perfect right and next let's code another test method for [Music] testing add to rows to an existing user okay here's a new test method public void test add rows to existing user okay and firstly we need to add a user by id user repo 5 by 90 and we passed the id 1 1 as the long value and that okay this is a with id1 in the database is username i mean here okay and then we add two row objects from the database we want to assign the raw admin and customer to this user so here we can use this code to add the row user okay and instead of writing a new row object like this we can also specify row id like this row row row admin equal new row and we pass id here in the database the row admin has the id 2 here you see and we pass the id to here and then we put add the row row admin to this user object and then we proceed in the user object and then we assert that this user has two rows aside yeah okay that's it and run this test method known as unit test okay social phone and you see it prints the squares statement to select user by id here and then select row by name here and then select a row by user id here and it searched into user's rows table here two rows into the internet table now let me check the database in the rows table sorry in the user's rows table here so we've got two new rows inserted for the user id one here aside row id one and two here id one and two as a rows using an admin yeah exactly next we update the user's listing paste here to display the rows of the user in the table let me refresh this paste okay i have to log in okay you see we have two use us ravi kuma now come back to the project and open the html paste for users here and we add a new column for the rows here the rows and we display the rows of the user here user dot rows because in the user entity class we have the collection rows aoc in the user entity class we have the collection rows here and in the raw entity class we need to overwrite the two string methods so it will display the row name send it to string just return the row name this name okay and test refresh log in again okay now you see the row names appear here for the username i mean he has two rows admin and user and the user ravi kumar has one row user here next we will show the row names of the country log in user actors username here okay come back to the project and open the user html paste and we have the code that displays the full name of the current login user full name is the property in the user details class here custom user details here this class wraps the user entity class here and the method headphone name here okay so to display the row names of the currently login user we can use time leaf crt with html code like this in a span tag like this sec colon authentication equal principle dot authorities yeah rose and we need to add xml namespace for timeline i adjust spring security here now that in the maven project file you need to have the dependency for time diff i just spring 35 like this okay so this is a xml namespace so you won't run the running of the ide okay the prefix should be set here okay and then in the user details class custom user details class in the method that authorities there this method get authorities will be called when we use this attribute principle not authority is there so we need you to implement the code here firstly we need to get the rows from the user we have rows here let's try to set up rows here and we declare a list of simple granted authority here authorities yeah equal new arraylist okay and we return the list authorities there and we iterate through each row object is a row set here or else we add new simple granted authority object to the list here add new simple granted authority and passing your own name all that name that's it for the red authorities method in the user details class okay save changes and now we test refresh and log in um we got an error here uh if she failed to lazily initial collection of raw use other rows yeah that means in the user entity class we need to specify the attribute fetch type for this many too many leadership so when user object is unloaded as associated the raw objects will be loaded as well so here we specify fetch equal fast type error okay save the change okay and test again okay now you see it and displays the row names of the controller when are here now i mean and flow by their own names yeah let me sign out and loneliness the user ravikuma ravi not kuma at zlu.com i can't log in because in the database the user ravi kumar was created in unit test without encoded password as you can see here so let me um copy the same password as the user name i mean here okay and apply okay okay log in again okay i have looking successfully as the user ravi comma and you can see the row name is user here exactly next we are going to update the user registration process to assign the default to a new user okay come back to the project and this is a spring mvc controller class that handles all the requests to the application rest source app and process administration here it's better to create a service class for the user business so i create a new class here user service service annotation and we move the code in the controller here's a user repository here and the business method save user with default raw and then we copy the code that persists user object here user here and we need to have a reference row repository private row repository here row report and change this name to user repo okay and we want that when the new user is the rest wheel size default row is user role so we read the user row by row name yeah row repo file by name by by name user row user and we add this row to the news object user add draw or kfc and in the controller class we call we need to have no reference to the service here user service service and here we call the service set user with default row here and passing the user object that's it and here for the list user functionality here and we move the code here public list user list o and we just return user repo file okay and we call service list over here okay okay save the changes then now we can test the user registration process that saves the user with default okay now i will rest a new user email mic you get mike not yes yes gmail.com password first name my client name that's okay and click sign up okay you have signed up successfully let me check in the database in the user table you see a new row inserted for the user market there and in the user's rows table you see you see we have a new association with user id 3 and raw id 1 here id 3 is news my guest here 91 is a raw user here exactly and i will log in yes the new user mike waits here okay you see role name of the user my guest is user here the default role assigned to a new user after registration here user and now i will show you how to implement the edit user function that allows us to update the rows of the user visually on the web form okay come back to the project and firstly we update the user listing table to add a edit button after the rows here i just see a hyperlink for editing a user here edit the url is gh colon sref equal to slash users slash and edit and followed by the user id user dot id okay refresh we got an error here sorry okay we have the edit time link here click and we got the white label error paste because no analog method so we code the analog method in the controller class here read mapping slash user slash edit and path variable id public string edit user and we map the value of the id of user in the url as you can see we have the id2 in the url here edit slash id2 so we use here part variable annotation id and by to a variable long id and a model object from spring mvc okay now in this method we return the logical view name to the user form user form and we need to implement a new method in the service class to rather use object by id public user yet long id and we return the user repo file by id passing the user id and you get okay very simple and here we call service net id and return user object and we put this new object onto the model and attribute user user okay and in this project we have the site formula we can reuse the code in this html paste for the edit user for copy paste and change the name to user underscore form and we modify the code the title is and it user the heading is added user and it gives a and the action of the form is now slash user safe and the binding object is user okay email password first name last name the submit button is update and for editing we need to have a hidden field that binds to the id property of the user entity object input type equal hidden and change fill by to the field id okay okay save the change and we can test the edit user form now okay you see the edit form appears for the user with id2 with the email ravi kumar first name ravi last name kumar and now we need to display the assign rows to this user after the last name here okay and in the user service class we need to have method that returns a list of rows here public list or all that rows and simply return a row repo file and in the handler method edit user here we need to add a list of row objects service you know that rows here now list rows and we put this list onto the model list rows yeah so in the view we can show all the rows okay save the changes for the java code and for the html code with timeless yeah we put a new dip section for the rows of the user okay so the label is in the side closed and here we use a time limit h loop t h block ghh row object in the list rows here list rows we send in the handler method the list rows here and then for each row object we generate input of type checkbox check box okay um and then we yeah by this input to the fuel rows of the user entity class entity class the field rows here okay and then we set the value for the checkbox is roll on id and the text for this checkbox gh text equal row.net okay save the change and refresh the edit user file you can see the outside rows for the user ravi comma you can see the row usually checked here meaning that this user is assigned a row user here we need to have some actual white space among the rose name so here we can specify the boot chop class class imagine two okay refresh you see it looks better right so here on this edit user form we can change the rows for the user by check or uncheck the role names okay so far so good let me go to the user listing paste list underscore users i need to log in okay and click edit for the user my get who has a raw user here edit you can see the row user is selected here and go back and edit the user name i mean with two rows admin and user here edit you see the two rows user and admin are checked here so as you can see timeless does all the binding work for you you just buy the checkbox to the field here and set the value and set the text here and timeline all the things behind the scene for you so it is very simple now let's implement the handler method for saving the user details with updated rows now in the form we have the action is last user slash save here okay and then we got a new hana method in the controller class here post mapping with the url like this public string save user user object that maps to the user on the farm okay and here we call the service method to save the user it is different than the save user with default row here public ui save user object here and we just copy the code that encodes the user password and we delete the code that adds the default rows okay that's it for the save method and in the hana method we call service save user yeah and we return the reduction to the user listing path return redirect unless users okay that's it about the changes and now we can test update user function okay now let me login let's do the login okay and let me chain update the rows for the user ravi kumar here edit and i select all three rows yeah you see all three rows user admin and customer checked and enter password and click update okay social phone and now you can see the role names for the user ravi is customer user admin here perfect right let me check in the database in the raw user's rows table here for the user id 2 graphic kumar we have three associations uh with the raw id1 raw id 2 and ready 3 here exactly perfect now let me edit another user username i mean here i change the row to only customer here and i need to enter password okay okay social phone and you see the raw ops username i mean got updated to only customer here and let me check in the database for the username i mean with id1 here id1 and you see with userid 1 it has only one association with the raw d3 which is the raw customer here so that's how to update the rows for user in a web form you see it is very simple using time leaf and spring boot you just use a checkbox like this and implement the handler method like this and the timeline and springboot does do everything for you
Info
Channel: Code Java
Views: 37,995
Rating: undefined out of 5
Keywords: Java, Spring Boot, Spring Security, Spring boot add role to user, Spring Boot user roles example, spring boot add roles, spring security roles, spring security authorization, spring security user roles, spring security assign roles
Id: p18O0hSmQyM
Channel Id: undefined
Length: 46min 12sec (2772 seconds)
Published: Thu Apr 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.