In this video, let's try to implement authentication provider and do authentication of the user aspect
of our business requirements for that.
Let's try to check what we have done previously.
So previously, we have an implementation of user detailed service and from my default authentication
provider, which is Darwell authentication provider.
A call will happen to your user by user name, and this will go to the database and leverage my password
encoders and check everything.
Once authentication is successful, user details will be sent back to my provider with the conversion
from user details to authentication is happening, which is up in the previous will do so.
Now, I don't want to follow this user detail service and I don't want to tightly coupled with this
spring security mechanism of user tritle service and user details, I just want to have my own implementation.
Ignoring this default provider interfaces, user contract, everything that we just have to create a
class which implements authentication provider and annotate the class with a direct component.
So inside this we have to write a method called authenticate, which accept authentication and returns
authentication.
So here I'm just taking the user name from the authentication object by taking Get Me, which is from
the principal interface and password.
Also I am taking from the get credentials.
So now what I'm doing is I'm just directly calling my repository with the user name and once I give
the customer details, I'm leveraging the password encoder to just check whether the match between what
I receive and what I stored in the database in terms of hash is matching.
If everything is matching, then I'm just trying to populate the authorities based upon the role that
I have in my customer object and forming a username password authentication.
So if you remember in our previous videos we have discussed this.
This is an implementation of authentication interface.
So when I found this object of this class and returned to the method authenticate framework will happily
accept that and will not throw any issues.
So here are the same time.
We also have a support method which are hard to implement.
And here I'm saying to framework whenever I get input, which of username password authentication token
from the user then only use this provider.
But if we have a scenario where I am receiving some fingerprint or tip, then I don't want to use this
provider.
So in such scenarios we can create any number of providers with the same approach.
But the only difference will be to support Summitville.
How different authentication implementation along with this type.
So maybe in such an we have to write our own authentication implementation with the name like fingerprint
authentication to conduct class.
So once we are done that, then any number of authentication providers we can configure and automatically
spring security checks.
Since we are annotating this with the component and implementing authentication provider as being security
smart enough to detect all this progress.
And as I was saying previously, we can have any number of providers implemented inside our security.
And our provider manager, which implements the authentication manager, will take the responsibility
of calling each and every authentication provider built that we get bad credentials until we get a successful
authentication of all the providers has been tried.
But still there is no authentication happening.
So I just keep a breakpoint point here also.
And I'm also keeping a breakpoint here to show you that my code will not stop here because I'm not going
to use the user details, service, all those things.
My code will directly call from my provider manager to this provider.
We call with the authentication parameter here.
I just wanted to show you I'm not following any user details are user details service and even I can
ignore this password and so I can have my own implementation.
I'm just showing this code so that we can have a mix of all.
The things also like in that I'm just delivering password and quarter, but not leveraging any user
details or user details.
It's up to us to write inside this method, whatever we want.
But at the end of the authentication happens.
We have to return the object of authentication.
So now let's try to go to browser and call my account service with the credentials or credentials are
happy.
A great example.
Dot com password is one, two, three, four, five.
Now you can see, as I was saying, the filters will intercept my request and they will call the authentication
manager.
So in this scenario, provider manager is the implementation of authentication manager.
So they will call provider manager dot authenticate and this will internally call authenticate matter.
Here you can see it is inside for Loop.
And let's try to see what all providers available is only one provider available, which is of easy
bank username, password authentication provider, which we have implemented.
But if you have implemented three or four, all of them will be shown here and the provider manager
will make sure it is covering all the providers it reaches a successful authentication or invalid authentication.
So in this scenario, since my test is of data type username password authentication token, which is
supported by my provider, is a bank user name ID up to the Atlantic Ocean provider, it is allowing
to pass for the end once I release this breakpoint.
This will call my authenticate implementation method.
And here you can see all the code will be executed successfully and you will get a response, whatever
we have mentioned in the back.
This is how we should implement authentication provider and already a method of authentication as part
of our requirement.
So we have seen all the details about authentication provider, how to create them, and how authentication
manager implementations like provider manager will call all the providers that we have defined one by
one and how the authentication happens by citing security as part of logic written inside the authenticate
method.
I hope this is making sense to you.
And I'll also attach this code to this video.
Please download it and do Hanton and if you have any questions, please post in the Q&A section.
Thank you.
And I will see you in the next section, but.