1 00:00:00,210 --> 00:00:06,540 Similar to add filter before women have certain requirements where we want to exclude our own custom 2 00:00:06,540 --> 00:00:11,350 filter after certain inbuilt filters of spring security. 3 00:00:11,730 --> 00:00:18,990 One such scenario that we can try here now is I want to add a logger about a successful authentication 4 00:00:18,990 --> 00:00:23,850 of the user along with his user name and authorities associated to it. 5 00:00:24,060 --> 00:00:31,010 Obviously, I can get the authorities of the user only after the ID is successful. 6 00:00:31,260 --> 00:00:39,720 So due to that reason, I am going to inject my custom filter, which is a logging filter after basic 7 00:00:39,720 --> 00:00:40,950 authentication filter. 8 00:00:41,220 --> 00:00:47,760 So with that configuration, the order that I will get is just after the basic authentication filter, 9 00:00:47,910 --> 00:00:53,850 which is for let's try to go to the back and quote and try to implement this custom filter. 10 00:00:54,030 --> 00:00:59,790 So you can see here I have implemented a pretty simple filter this time, which with the name authorities 11 00:00:59,790 --> 00:01:06,280 logging after filter and it implements filter interface and already the method to filter. 12 00:01:06,420 --> 00:01:14,010 So what I am trying to do here is we know after successful authentication Spring Security Framework 13 00:01:14,290 --> 00:01:21,360 Store that user details inside security context holda, which is specific to that thread. 14 00:01:21,600 --> 00:01:27,660 With that assumption, I'm trying to get the authentication details of the current and logged in user 15 00:01:27,930 --> 00:01:35,290 by calling the method get authentication from the get context matter of security context holda. 16 00:01:35,460 --> 00:01:42,420 So this will give me the authentication details of the user and if this authentication object is not 17 00:01:42,420 --> 00:01:46,070 null, which means successful authentication happen. 18 00:01:46,350 --> 00:01:53,520 So in such scenarios, I am going to log a statement saying that user with the user name is successfully 19 00:01:53,520 --> 00:01:59,600 authenticated and has authorities by fetching that authorities from the authentication object. 20 00:01:59,850 --> 00:02:07,020 So once I'm done with this logger statement, I'm simply going to let my filter handle the responsibility 21 00:02:07,020 --> 00:02:13,570 to the next filter in the chain by leveraging the Thada parameter that we receive to do that. 22 00:02:13,650 --> 00:02:16,590 So now we have created a custom login filter. 23 00:02:16,800 --> 00:02:20,940 Let's try to go to project security config and configure this filter. 24 00:02:21,060 --> 00:02:27,570 And you can see here just below the line of the metadata filter before I have added a new line with 25 00:02:27,570 --> 00:02:35,040 the method invocation of our filter after we're passing my one custom filter, followed by the basic 26 00:02:35,040 --> 00:02:36,160 authentication filter. 27 00:02:36,480 --> 00:02:44,910 So this line will create a space for my own custom filter in the security filter chain just after the 28 00:02:44,910 --> 00:02:49,890 basic authentication filter by giving it order number after it. 29 00:02:49,920 --> 00:02:52,050 So now we made the changes. 30 00:02:52,200 --> 00:02:53,710 Let's try to start this over. 31 00:02:53,970 --> 00:02:55,700 I started the server successfully. 32 00:02:56,040 --> 00:03:02,100 Now I clear the console to see whether our filter comes in the security filter chain or not. 33 00:03:02,220 --> 00:03:04,950 I'll go to the UI and try to login. 34 00:03:05,230 --> 00:03:09,600 I'm trying to login with the valedictorian Charles Happy Aderet example. 35 00:03:09,600 --> 00:03:12,660 Dot com and password is one, two, three, four, five. 36 00:03:12,870 --> 00:03:15,540 And you can see here post authentication. 37 00:03:15,900 --> 00:03:23,160 Our filter will be called in the filtered chain and I'm trying to fetch the current authentication details. 38 00:03:23,310 --> 00:03:30,120 And since this is a not an old value, I will log all his authorities along with the details of the 39 00:03:30,120 --> 00:03:31,800 user, which is happy. 40 00:03:31,800 --> 00:03:36,120 Aderet example that I'm trying to invoke the logger statement here. 41 00:03:36,420 --> 00:03:45,390 And you can see in the console there is a log that we try to print is present inside the console. 42 00:03:45,390 --> 00:03:53,820 And similarly, our custom filter, which is authorities logging after filter, is changed just after 43 00:03:53,820 --> 00:03:55,600 the basic authentication filter. 44 00:03:55,890 --> 00:04:04,920 So with this, we also created a custom filter and injector just after a spring security filter and 45 00:04:04,920 --> 00:04:07,090 executed it completely. 46 00:04:07,290 --> 00:04:16,079 So now in the next video, let's try to explore one more method that we have where we can add a filter 47 00:04:16,110 --> 00:04:19,360 in the exact place of the screen security filter. 48 00:04:19,380 --> 00:04:19,950 Thank you. 49 00:04:19,950 --> 00:04:20,519 And by.