12. Keycloak Admin Client Spring Boot Integration: Creating New Users Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
salers welcome back to another tutorial in this tutorial it's about spring boot integration with kick loock using the admin client so I want to show you how we can integrate our spring boot project with the Ki loock server and using the Java SDK admin client as you can see this user if you go to this our kick loock you can see we have this we have two users bisy and test one so what you want to do we want to create another user Cod test to so I will show you how you can do this so if I run this a new user has been created so if I come here and reload this page you can see this user is is the user that we just created and we can also log in with this user so this tutorial I will show you all the steps so you can copy this and you can delete also this user so we can test to delete this user we want to delete this user user two and I can say execute and you can see we get a success after deleting the US user and I can refresh here the user has been deleted so I want to show you how you can do this with spring Boot and integ with this KY clock and we are using the latest version and also we are going to update our server to use the latest divion on previous tutorial we set up Ki loock and we created a server and you created a user this user we were using the the portal so in this tutorial I want to show you how we can create a user using the kick loog Java SDK which is the admin client so right now as you can see we have this version of um of this this is the kick loock SDK that you want to use for the admin and we are on version 22.0.1 but let's go to our admin so for for for you to be able to use this current version you your kick loock version version must match with your kick clock server so if you go to this admin if you go to S information you can see the version that I'm currently using it's 16.0 16.10 and it's an old SDK so we must upgrade our server to to this latest which was released on 4 October by the day of making this video 22.0.1 so I updated the updated our to compos so that we can use the latest version which is 2 2.04 so now you can you can stop your your your server and you can use this new script so let me first up upgrade our server to this latest and we can continue so you can log into your server you can log into your server and in your Docker composer file that you use on the previous tutorial here I have this toer composer file so I the Ser still is running so I can do compose down I want to stop this this server so that I can upgrade [Music] it okay so what I want to do right now is like I want to remove this to comos then I can start then I can create a new one with the latest with the latest environment variable so I say remove then I will say I create a new one which is an empty this is an empty file and you can copy you can copy your pre you can copy this doer you can look it into our spring board project you can copy thiso which is for my SE if you want to use post Grace I already have this one for post but for my we just copy this to compos so I'm just copying this and I can and run the application using the latest one just copy as it is and run it so I copied it so after doing this I can say doer compose up let's see if everything is working fine so you can see it's now downloading the kick loock version 22.0.1 so this is the same version and this the version must match 22.0.1 so that I can use this Le this version from our spring boot project so let's wait for the for the for this to [Music] start so right now the application is now running so let's run Let's test this one here currently it was version 16.10 so let's test this one so the latest version does not have the O so we the O was removed so what I want to do is just I want to copy this URL let me copy the URL and open it with another so let me open it here where it doesn't have an o you can see I open the URL without this is the new the new version server that you have so the password I put my password so the password is the one that is in this which is this one the kick loock admin and your password that you set on your project so for me I I updated that password and I'm going to use my secret password so this is my pass password let me sign in and I will save this one so you can see the previous is there we are using we are connecting to the existing my database so the previous the previous room is there for for application okay let me just use this this one I showed you in the previous tutorial how you can create a room a room so if you go to clients here this is the admin client that we want to use when we want to log in so you can go to this admin client so this admin client on this admin client we want to have access to create a user from the rest API or from the SDK so to enable that we must enable client authentication and this service accounts R just enable about this and in here we must [Music] update just add this and click save so after to save this it's because we want to see a credentials we want to you have a client secret and client credentials that's why we enabled client authentication Service here and adding that URL so after saving this we now have this section tab which is cred so you have to copy our credential our client secret we I will show you we copy we come the year later and copy this one then in our service savings account we if you want to create a user from the admin from the rest API which is this you can see this these are the rest end points that we can use but we are going to use an SDK which you call the same methods so what you want to do is that we must enable we must give a permission to that client to be able to create a user to be able to get user to query users so to add that permission you go to saice account R and you come you can assign a new row and click here fter and filter by clients and we want to look for manage real management which is manage users so we can go here we are looking for users is not in this section we need to assign this row so after assigning this row we are now okay and this admin client will be able to will be able to create a a user so what you want to do to test if you are able to create a client with the with the rest so let's copy the client the client ID we already know it's admin client and in here we have this client Secret I copy the client secret and I'm going to use Postman we must call this end point to get an access token and if you have that access token we are going to use this rest end point which is admin rooms and this one and we want to copy our let's copy we need to copy our [Music] room copy our room this is the room that you are going to use so we the URL we have to update with the G that you are going to use [Music] always the first end point that we are going to use is the one to get the access token with the client you can see the client which is the admin client and the secret so if you send a request like this we get this token so if we have this token now we want to create a user using the rest and the end point for creating a user using the rest is this one so here the username I can say test one this is going to be my it's going to be a username and here I can say test one and I will remove this let me remove that and the account is enabl you must set a username the email first name last name credentials which is the user is going to use to log in and if you want to add this user into a certain group and the attributes you can add different M different groups so the end point you just use this end point just know you your server admin admin rooms your room which is spring boot code and the endpoint which is users so if so for me we have to use the authorization here from authorization you have to pick be token and this is the token you can use this token which is for admin Cent you can copy the token and you can put it here after doing this if we send this end point you can see this user it's a test one so so let's come here and go to users and see how many users do we have we have this one which is bis and we want to create another one with rest and this one we created through the portal so what you want to do is to hit this end point you can see it means 200 we get access means a user has been created so let's refresh this page and see our new user this is the new user that we created right now and the email is not verified so I can mark it as verified and click and click save and with this user we can log in with this user using the same endpoint this is the endpoint let's go to the login endpoint for login endpoint this is the test one and this is the password for this user so for this endpoint this is the we are going to use a client ID we created our custom client ID and the username which is this one that we used here and the password which is the test one 2 3 which is this one and the grant type is a password because we are using username and password let's check if we have this client ID because we are now we want to use this client ID so let's go to the client [Music] IDs let's go to the clients do you have that one yes we have the we have the client this is the client that you that we are going to use this is our custom client so we are okay so what you want to do let's justest to log in you can see we have this now we have this new user and this user is logged in so if you go to this user if you go to this user if you go to this user test one and you can go to sessions you can see the session the time that we logged in and the this is our in the client ID that we used and the IP address so all of this we it's fully managed by Kick loock so we want to use this rest apis in our spring boot project I showed you the dependence that we are going to use and this dependence is this one 22.0.1 so this same version that we have here so we are using this version 22.0 point4 so this same version is the same version that we are seeing here which is this this one on our Ki loock server so the vision must match so so that we can avoid the issues of errors with the SDK so let's add this dependent in our mafin so in our maffin we must add the the cred the dependency for kick loock and this dependencies we must add for the rest for the rest easy because they use a rest easy so let's add this for the rest this one for kick loock and the one for the rest easy this one is is the one that will send request to the to the admin so we can we are using it maybe you're familiar with rest template or fan client but here they they used rest easy so we have to use this rest easy you must add this dependence so after adding so in my properties I'm Now setting the versions so the version of rest is that I'm going to use 625 final and our sa version for KY clock then I will refresh the dependencies so that you can download latest dependencies so dependencies has been downloaded so we want to add some configurations here on previous tutorials we already added some configurations for JWT when I showed you how to use Ki loock and sign in with with a certain URL but right now I want to configure the one for the beans for our Bean Ki loock Bean so I can call it beans I want to create beans and this class this is the class that I add then we say configuration so that they can be loaded when the application start starts and the the beIN that I want to you to create is called K loock which is coming from the SDK so K loock this is the bean that I want to add and here I'll just say add Bean then it will return we just say use a ky clock Builder you say KY clock we want to use this key CL Builder dot Builder so in our Builder we want to set a server URL server URL we configure this one a Ser URL is is this URL you can see this is going to be our server URL this this one which is key clock this one that we are using here this is going to be our server URL so we can go configure it later then here the next thing we want to configure the room the room is the one that we created this one which is spring put code then we want to set a grand [Music] type let's set a grand type here and we want to Let's see we want to set a client ID also and the client secret and we just say build so this credentials that you want to do here curent secret here so this credentials you can see the grand type the saer URL and the grand type the grand type going is we are going to use client H credentials so I can say all o client credentials this one you want to use this one so there are different gr types that are there we can use password you can use o code you can use menu them but we want to use this one which is client credentials so why I why did I choose this client credentials this is the one that we used here you can see when you before we created a user we just used this Grand type which is client credentials so we are configuring this these configs on our key KY clock Builder and this is the client secret that you're going to use the client ID and the client credentials so let's add this configurations into our application yo so I will just add this and explain to you so I created this configurations in application the room that you are going to use spring spring boot code and the domain which is code with b.com and the admin client which is the client that is going that you are going to use to create a user and I showed you how to say how to add some roles on the on the portal so that the this client will be able to create users so it's like we added those RS like permissions and this is the client secret and this client secret is the same as this one and this is the client ID and also and this ID that we are going to use which is for authentication we are going to use it later and our domain which is this one which is the code with b.com so if you come back to our bins these are the configurations so for the client now we are going to replace this with the for URL I will just say Ser URL and for the r i just say R we are now taking them from the config from the application yo and for the secret this is going to be our secret and this is going to be the the client so by doing this we we can now start to use this a this bin which is the this bin kick lock to create a user so this one is responsible for login for for for for authentication and authorization of our client create credentials so let's now go to the implementation of creating a new user so we going to create a new a new service which which I'm going to call a let's create a new package and this package you can call it kick loock and we want to create a new service let's create a new interface and I can say it keep clock user service right and it's going to be an interface right and in in in our interface uh it will going to return for now let's just say an object let's just say General an object and this one is going to be create user we are going to use this one to create a user the first method is to create a user if you want to create a user we need a dto for creating a user so let's go to our DS and copy one from this and or let me say Java class and it's a type record I'm using Java 17 so this one it's about user registration record like this these are the fs that we need we need a username when creating a user I showed you we need a first name or we need an email string first [Music] name last name so far these are the fs that I'm thinking we need also a [Music] password a user password so this is the one that I'm thinking right now so let's go to our our service this one and let's we want to turn this also and we can pass it here and we need also an object for now let's say just a user representation let's say us a representation this one which is from then say get user by ID this here will pass our string ID user ID so this one is coming from Ki loock for each user if you want with the user that we created we can get those details by doing that so we can also add another one and I can call it void this one and I can say delete [Music] user delete user by ID and you can pass the the user ID so let's create the implementation of this class new Java class and we can call it imp add in and this going to so before you forget anything to avoid like now Point exceptions just add service that's what I do and let's add I'm using l l book so let's add SF 4J this one we going to use for our log our log and remember it's private here private we want to use this bin which is kick loog so let's say private Ki loock like this and let's create a Constructor [Music] so you can see it has it auto wired this bin this kick loock so we are going to use everywhere so I avoided to to initialize I avoided to initialize this bin every time when I want to use this so we just create one object that is available throughout the application and and this application is the kick loock so after doing this let's add an implementation of this which is our implementation let's Implement all these method to create a user to get a user and to delete a user so in here that's where we are starting our logic to create a user so if you want to create a user with this key clock I showed you about us this user representation this is the one that you use when you want to create a user or you want to get a user or to delete a user so this object is the one that we are going to use so when in here we want to use this object user representation so this user representation you just say a user we want to create a user equals to new we want to enable the user set enabled we want to enable the user so after when creating the user we must we want to enable the user after creating it or set yeah let you say set username by order and the username is coming from this from this object do get username or it's username it's now we are now using then you want to set email set email pting [Music] name last name we can say set email verified let's just say the email has been verified let's just set it to true on the later videos I will show you how we can verify the emails through the end point so for now just do this so after doing this we need another one which is a credential so here we created a user right we are going to create this user but we must assign credentials to this user so we want to say credentials [Music] representation so for the credentials representation we want to set a value the value is the password that we we added here this is the password and temporary we want to set it to false we want want this user to be able to log in set temporary to false I think by default it's true the type of this password I can say set type for the type we are going to use this one which is password so for this user for this user we want to set the credentials now we want to for after creating this user we want to assign a password for this user so what you want to do you there's a user set credentials I think it's a this one set credentials it's it allows a list of credentials so this one is going to be a list AR a list so what you can do just say list list of this and import and you say I will just say list equals to new array list and in this list this is going to be list of credentials. add credentials presentations and I will add this list here like this so right now I added this credentials I assigned this cred to this user so a user can have multiple credentials either password either TP secret all of these credentials can be assigned to this user we want to save this record so we want to use the room the room the kick loock room result so what we can do we just say kick loock this one click loock R and we need to pass our room this is the same room that we added into our beans this one is the same so just copy this room and come and put it here just put it here it's going to be the same room right so then this is going to be our our room result so with this room we want to use a user so there are different types so we want to use a user which is a a users this one a users because you want to create a user so you want to use a a type of the resource of users so you can see this in here we get a list of end points these are the same end points that we were calling here so they are called R they called user resource because you are calling to a certain resource so you want to create a new resource so let me do this let's go back so after doing this I just say a create a user and we can create a user now and we get a response so this is the end point to create a user so if you create a User it's either successful or not successful if in case of any of we have a duplicates email or duplicate usernames the user won't be created so let's just check for a success here uh we want to say or I can say if status if status when we create a user H it's going to be 2011 so if you create new thing like in post put if you something record is successful we if you create a new record we the return Response Code supposed to 2011 that's the standard and that's the best way when creating API so this API is returning to1 so what you can do here but I don't like this way of doing this let's use objects from ja I think it was Ed Java 8 whereby we just say 2011 [Music] comma yeah let's use this one so this one if it's success we want to rain this user that we created so if he not successful we are going to have an we are going to have an error coming from this API so what I want to do now is like on this response we want to read an entity we want to read an entity and this entity it will return a message so okay before I do that H by now it's either returning in now or so what I want to do let's just do this let me comment this one I know here there's an error if this is now and that error will come to our API so let's leave it this let's leave this like let's leave it like this so here I want to extract this code and I right click and do refactor and I want to extract this method and it does all everything for me which is good resource and this is the one that I want to use [Music] so here let's put in line so for forget user forget user if I have user ID I want I want to use this same method which is a user I want to use this method so it's going to be so here it's get resource then get we pass the user ID and then we change it to representation and it's going to be a user so this end point is going to get a user by ID so this is the resource to get a user by ID you can see this one it's going to be the delete so for for a delete we just do the same delete and we pass the user ID and it will return a su a success so I showed you how to create a user now how to get a user and how to delete a user with kick log so what's next now we want to create our our API our controller which is going to create a user delete a user and get a user so let's now create a new controller and this controller here we have apis we have straps so let's just create this one and this one I can call it this API I can call it klock is for user kick loock user API this is my custom one this is my custom one so here I just say we can copy what what you already have just copy this so for this it's going to be users it's going to be users and for the service this is the service that I want to use I'll just say private and uh let me import this and I can make it final it's a Constructor I added or Constructor it means this one is now a Constructor so there's no need to create a a Constructor and the method that I want to use first is the we want to create a method to post a user so the method we want to post means you want to create a user so we just say it post mapping like this then make and the name of our method is public uh we want to return this object which is user registration record we want to return this one you can we can import this then you say create user and you want to also pass the same the same object so here we must say at request body that's why I love spring framework it's easy so to to create a user I'm just going to use this service which is Ki loog user service implementation I just want to return this do create user and the user record that's all that's all now we we are able to create a user from the from our from our rest so from the from the rest so I Will Show You by in but we are still have an an one issue that you want to solve so I will show you after this after adding this after adding other end points so the other end point that you want to use here it's get user so this one get user this one to get a user we want to use a a loged in user so here it's going to be a get mapping so when a user is logged in on the application we must be able to to get a user so here I just say principal so this principal is from Spring Security to get a to get the user from the token so a logged in user so I can get user by ID and here I just say get name so get name is going to be our user and it's supposed to be user user representation this one we are going to have this one so this is going to be our get so let's add another one which is delete so here it's not get mapping it's delete mapping and we want to delete the user we can delete the user I can delete myself my user or I can so let's say delete user so I want to delete user delete user by ID I maybe someone wants to delete the user the admin of the application string user ID so here I just say delete user by ID of which it is a void and it's user ID so here it's supposed to return nothing so what you can do here you just and then you just say user ID and you can say path variable here supposed to be at path variable like this after doing this we can delete a user so we want to test our implementation using po I don't want to use post man here but I told you we have an issue here when we want when a user creates when we want to cre and when we are creating a user on application we must allow that end point to be a to be to be open to be public so that it we since our application is is security behind so we must allow that end point this one for post to be able to use it without any issue with security or any issue with authentication so what I can do before I show that before we fix that I want you to be familiar also with the security how it works maybe in case we don't know why why I'm doing that so what we want to do we have this one which is API integration so let me run this application so our application has been started so we want to I want to so we integrated this application with the Saga if you watched our previous tutorial so I want to access this application on this Sport and 1995 and here so what I do is that I will come here and I will say Local Host at P 90 95 and I add this API integration I put double then I will say swaga UI HTML then if I open this it open the Swagger so these are the end points that we added right so we want to create a user so to create a user you can see this this is the post method of creating a user and you want to send these variables and if I try to send these variables here username I will just say Test 2 and email is Test 2 at gmail at gmail.com T name test two is going to the last name password is test 81 2 3 4 so if I try to hit this end point you can see you're going to have issue with authentication to this end point so we don't have permission to to to create a user so this endpoint is supposed to be public without logging in we have to create this user so what you want to do let's go to our security which is our web security and let's look for post in our post method because it's the one that you want to to then for post we just say users so if you go to here we want to allow this end point to be we want to allow this to be public which is a post mapping so that's why I added this into our the post method will be filtered so let's run the application again so the application has started again so in our KY loock here in our Administration we can see the number of let's log in again we have test one but we want to create another test two with the implementation of spring so I want to do let's execute this you can see it's a it's a success no era now of four of four four of of 41 so let's look into the administration part we have this new user which is test two and this is the user and this is the user ID so let's go again to our swaga and see if our our get method is working and let's click try out the problem that you have with this x it's it's going to return 401 because it's supposed to be called with the user who has logged in so this one is a test to so we want to log in let's go to our login end point which I showed you it's going to be a test to and let's hit this end point and get a token so in our swager let's go back to our swager and on this authent authorizer let's add our token here which we created with the postman and click close And let's test get user so we I have an issue it failed to return to that because we using the old logic so we want we have to remove this o because this new version of R does not have an O so let's restart this as you can see after run our application we get again a 401 with the with a valid token it's because of that end point so let's test again now okay our room here it's because of our room let's update our room and let me run again so I updated I removed the the O which was here and I updated our room because we are now using this new room which is spring boot code so let's test again you can see after updating our room and the and removing the O path path path variable you can see we are now able to get a user who is logged in which is test2 so if you want to delete that record it's also the same we can just copy we have the ID we just copy the ID and we can come here to the delete method which is here and then I'll add the I say try out and I add the ID and I can execute this one and we get a success so let's see if the user is still exist if I go to users you can see the user has been deleted so thank you guys for watching this video I hope you are now understanding the kick loock and I showed you how you can integrate with the spring framework on how we can configure these clients testing with postmen testing with the swaga and developing it and the all the dependen that are required to run this project so if you are new to this channel May kindly subscribe click the notification Bell so that you be notified whenever upload new videos Sal coders let's meet you on another kick next video
Info
Channel: Code With Bisky
Views: 7,566
Rating: undefined out of 5
Keywords: Keycloak Admin Client Spring Boot Integration: Creating New Users, Keycloak Admin Client, Spring Boot Integration, Authorization Tutorial, User Management with Keycloak, Java Web Development, Keycloak API, Spring Boot Security, Java Web Applications, Developer Tutorial, Code Integration, Keycloak User Management, Secure API, Java Programming, Java Development, Keycloak Features, Spring Boot Projects, Authentication Implementation, Keycloak Documentation
Id: IYAI1gEUVfw
Channel Id: undefined
Length: 50min 31sec (3031 seconds)
Published: Sun Oct 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.