Similar to add filter before women have certain requirements where we want to exclude our own custom
filter after certain inbuilt filters of spring security.
One such scenario that we can try here now is I want to add a logger about a successful authentication
of the user along with his user name and authorities associated to it.
Obviously, I can get the authorities of the user only after the ID is successful.
So due to that reason, I am going to inject my custom filter, which is a logging filter after basic
authentication filter.
So with that configuration, the order that I will get is just after the basic authentication filter,
which is for let's try to go to the back and quote and try to implement this custom filter.
So you can see here I have implemented a pretty simple filter this time, which with the name authorities
logging after filter and it implements filter interface and already the method to filter.
So what I am trying to do here is we know after successful authentication Spring Security Framework
Store that user details inside security context holda, which is specific to that thread.
With that assumption, I'm trying to get the authentication details of the current and logged in user
by calling the method get authentication from the get context matter of security context holda.
So this will give me the authentication details of the user and if this authentication object is not
null, which means successful authentication happen.
So in such scenarios, I am going to log a statement saying that user with the user name is successfully
authenticated and has authorities by fetching that authorities from the authentication object.
So once I'm done with this logger statement, I'm simply going to let my filter handle the responsibility
to the next filter in the chain by leveraging the Thada parameter that we receive to do that.
So now we have created a custom login filter.
Let's try to go to project security config and configure this filter.
And you can see here just below the line of the metadata filter before I have added a new line with
the method invocation of our filter after we're passing my one custom filter, followed by the basic
authentication filter.
So this line will create a space for my own custom filter in the security filter chain just after the
basic authentication filter by giving it order number after it.
So now we made the changes.
Let's try to start this over.
I started the server successfully.
Now I clear the console to see whether our filter comes in the security filter chain or not.
I'll go to the UI and try to login.
I'm trying to login with the valedictorian Charles Happy Aderet example.
Dot com and password is one, two, three, four, five.
And you can see here post authentication.
Our filter will be called in the filtered chain and I'm trying to fetch the current authentication details.
And since this is a not an old value, I will log all his authorities along with the details of the
user, which is happy.
Aderet example that I'm trying to invoke the logger statement here.
And you can see in the console there is a log that we try to print is present inside the console.
And similarly, our custom filter, which is authorities logging after filter, is changed just after
the basic authentication filter.
So with this, we also created a custom filter and injector just after a spring security filter and
executed it completely.
So now in the next video, let's try to explore one more method that we have where we can add a filter
in the exact place of the screen security filter.
Thank you.
And by.