diff --git a/.gitattributes b/.gitattributes index 6e07d7cf72f94c7b442f215089ec944b165f1661..12f9eb40d906cd131a363ce532a046ebfb79517b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -119,3 +119,18 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text 03[[:space:]]-[[:space:]]Advance[[:space:]]Java/021[[:space:]]What[[:space:]]is[[:space:]]Exception.mp4 filter=lfs diff=lfs merge=lfs -text 03[[:space:]]-[[:space:]]Advance[[:space:]]Java/022[[:space:]]Exception[[:space:]]Handling[[:space:]]using[[:space:]]try[[:space:]]catch.mp4 filter=lfs diff=lfs merge=lfs -text 03[[:space:]]-[[:space:]]Advance[[:space:]]Java/024[[:space:]]Exception[[:space:]]Hierarchy.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/026[[:space:]]Custom[[:space:]]Exception.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/023[[:space:]]Try[[:space:]]with[[:space:]]multiple[[:space:]]catch.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/025[[:space:]]Exception[[:space:]]throw[[:space:]]keyword.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/027[[:space:]]Ducking[[:space:]]Exception[[:space:]]using[[:space:]]throws.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/030[[:space:]]Threads.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/028[[:space:]]User[[:space:]]Input[[:space:]]using[[:space:]]BufferedReader[[:space:]]and[[:space:]]Scanner.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/029[[:space:]]Try[[:space:]]with[[:space:]]resources.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/033[[:space:]]Runnable[[:space:]]vs[[:space:]]Thread.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/032[[:space:]]Thread[[:space:]]Priority[[:space:]]and[[:space:]]Sleep.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/031[[:space:]]Multiple[[:space:]]Threads.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/035[[:space:]]Thread[[:space:]]states.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/036[[:space:]]Collection[[:space:]]API.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/034[[:space:]]Race[[:space:]]Condition.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/038[[:space:]]Set.mp4 filter=lfs diff=lfs merge=lfs -text +03[[:space:]]-[[:space:]]Advance[[:space:]]Java/039[[:space:]]Map.mp4 filter=lfs diff=lfs merge=lfs -text diff --git a/03 - Advance Java/023 Try with multiple catch.mp4 b/03 - Advance Java/023 Try with multiple catch.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..774fb07366d3d253ee6789bc7389ff3fe98b8e86 --- /dev/null +++ b/03 - Advance Java/023 Try with multiple catch.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f988a6ed0ec641c94c27b4b6252b496c111eea476c39b18d38309631d4148dc +size 167982266 diff --git a/03 - Advance Java/025 Exception throw keyword.mp4 b/03 - Advance Java/025 Exception throw keyword.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8f79f640173ae68f788b9f3ff171a8a9efd7b739 --- /dev/null +++ b/03 - Advance Java/025 Exception throw keyword.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2fb6e52386d60ca774bb779ff2f7d6c822661b049ed490e2a1a908d74368a907 +size 85661874 diff --git a/03 - Advance Java/026 Custom Exception.mp4 b/03 - Advance Java/026 Custom Exception.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8c94616c2cfa8a05fbf92708bfed190226aa07c0 --- /dev/null +++ b/03 - Advance Java/026 Custom Exception.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:360c76766d1d27936c3129d0805bb7d9602c74e98e8c6ac15a2eb6fb70d8a5c4 +size 57640187 diff --git a/03 - Advance Java/027 Ducking Exception using throws.mp4 b/03 - Advance Java/027 Ducking Exception using throws.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2201fa913502977aef29c8ab234fce4670a353e9 --- /dev/null +++ b/03 - Advance Java/027 Ducking Exception using throws.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b41a418266b17b878378aae5c107356f66f495f411402d3ab287887ab58a2908 +size 153426306 diff --git a/03 - Advance Java/028 User Input using BufferedReader and Scanner.mp4 b/03 - Advance Java/028 User Input using BufferedReader and Scanner.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6fd31dc49f449f003e3ba41ef689541b08882e00 --- /dev/null +++ b/03 - Advance Java/028 User Input using BufferedReader and Scanner.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9a89d35133823d2aa7e3f0718acab9b2fc4fb7d18014a1419101054682b9004 +size 192332565 diff --git a/03 - Advance Java/029 Try with resources.mp4 b/03 - Advance Java/029 Try with resources.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ef72fc960602557a3b1e5517c7d732b523f32510 --- /dev/null +++ b/03 - Advance Java/029 Try with resources.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:916f1b53ebfbf8196b3b1b4019ad984d67d366e9a002e7eff6e5ef8a99cdd6dc +size 183484229 diff --git a/03 - Advance Java/030 Threads.mp4 b/03 - Advance Java/030 Threads.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f263ad2d9e9f22ff872b9d181cb51c49b9821418 --- /dev/null +++ b/03 - Advance Java/030 Threads.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:547e1e1f623c078c26fff840f6b93263b67031219c1dbaa13b14d6bc321f8d53 +size 98408255 diff --git a/03 - Advance Java/031 Multiple Threads.mp4 b/03 - Advance Java/031 Multiple Threads.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..54fb99528379da4f12d045a5a6da479cddab4d1a --- /dev/null +++ b/03 - Advance Java/031 Multiple Threads.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:146c9d273f172eb2a3e90bacbb4414b9d4afafe150522218caa158c8632e20fd +size 184310750 diff --git a/03 - Advance Java/032 Thread Priority and Sleep.mp4 b/03 - Advance Java/032 Thread Priority and Sleep.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5d1c169e999763c5386e4c53b4475249ec5ec6fb --- /dev/null +++ b/03 - Advance Java/032 Thread Priority and Sleep.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:777c1df34bd13a9eda84126f9dceb0d025ef7ba0f73680b176d657f91ba4b243 +size 173252287 diff --git a/03 - Advance Java/033 Runnable vs Thread.mp4 b/03 - Advance Java/033 Runnable vs Thread.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2fcd5cad5519db69243389a4784e5a95e3475c59 --- /dev/null +++ b/03 - Advance Java/033 Runnable vs Thread.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:024169cd5ed6385f02b7a12458bb3cc1472248e21c856b3a652e07f9e8be24d1 +size 136365684 diff --git a/03 - Advance Java/034 Race Condition.mp4 b/03 - Advance Java/034 Race Condition.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0ef7635ca1e45277b0c9c70b68dfbdd82ab18150 --- /dev/null +++ b/03 - Advance Java/034 Race Condition.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:63673eee1ab38f427338e0eec3392ab330cd608bc1e07799c6912c0ce59b93d2 +size 204923140 diff --git a/03 - Advance Java/035 Thread states.mp4 b/03 - Advance Java/035 Thread states.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..33cfefd7884da4b8dabaa671ec6970bc79c8a8a8 --- /dev/null +++ b/03 - Advance Java/035 Thread states.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:61f6ded70f9bb617444ed59a714cfacd5626a8e7d3b225854e2933648a7f1715 +size 61505378 diff --git a/03 - Advance Java/036 Collection API.mp4 b/03 - Advance Java/036 Collection API.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3cf70ab67a900d2f0e20cd2ea1b127c4dab52be4 --- /dev/null +++ b/03 - Advance Java/036 Collection API.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b4ab013c00446a8e21d822a627d8fe674e1605481a990e48180f65bb593deae +size 67323726 diff --git a/03 - Advance Java/038 Set.mp4 b/03 - Advance Java/038 Set.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f18fe54b29c8f63d288d6a72e5fac3a4d63b99d3 --- /dev/null +++ b/03 - Advance Java/038 Set.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52618955195ec0401c5e99c35c450c78d31c94cc6246d0c5866743a36766c929 +size 115117359 diff --git a/03 - Advance Java/039 Map.mp4 b/03 - Advance Java/039 Map.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8bb4f73352728a056523b3bec95e1235edd30b55 --- /dev/null +++ b/03 - Advance Java/039 Map.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d030d7af0240655a75ca7176f71ac243a6c610503d0f7d158f1c94cb550d8495 +size 161660550 diff --git a/20 - Spring Security/014 Source-Code.url b/20 - Spring Security/014 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..332c137061f3a961a8d0c3d5175df82a257b6272 --- /dev/null +++ b/20 - Spring Security/014 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.14%20Disabling%20Csrf%20Token/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/015 without lambda_en.srt b/20 - Spring Security/015 without lambda_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..bc30bf937c6baca995501b4d6c991dadeef7a118 --- /dev/null +++ b/20 - Spring Security/015 without lambda_en.srt @@ -0,0 +1,508 @@ +1 +00:00:00,000 --> 00:00:02,000 +(upbeat music) + +2 +00:00:02,000 --> 00:00:06,000 +-: So now let's convert this code into imperial style. + +3 +00:00:06,000 --> 00:00:08,000 +So I mean normal style. + +4 +00:00:08,000 --> 00:00:10,000 +So what I'm going to do is let's comment + +5 +00:00:10,000 --> 00:00:13,000 +this entire section, in fact till here, okay? + +6 +00:00:13,000 --> 00:00:15,000 +And now let's work one by one. + +7 +00:00:15,000 --> 00:00:19,000 +So first thing we have to address is http.csrf. + +8 +00:00:20,000 --> 00:00:24,000 +Now if you can see, if we talk about CSRF, + +9 +00:00:24,000 --> 00:00:25,000 +it takes a parameter of customizer + +10 +00:00:25,000 --> 00:00:30,000 +and of type CSRF, configure of type HttpSecurity. + +11 +00:00:30,000 --> 00:00:32,000 +So basically we have some types there. + +12 +00:00:32,000 --> 00:00:35,000 +So customizer takes a type of CsrfConfigurer, + +13 +00:00:35,000 --> 00:00:38,000 +which takes a type of HttpSecurity. + +14 +00:00:38,000 --> 00:00:40,000 +So what I'm going to do is we create an object of that + +15 +00:00:40,000 --> 00:00:42,000 +and let's pause it here. + +16 +00:00:42,000 --> 00:00:46,000 +So we have to get object of customizer of type + +17 +00:00:46,000 --> 00:00:51,000 +CsrfConfigurerr of type HttpSecurity + +18 +00:00:52,000 --> 00:00:57,000 +and then I will say this is customized CSRF equal to new. + +19 +00:00:58,000 --> 00:01:00,000 +And we get the object for the same thing. + +20 +00:01:00,000 --> 00:01:03,000 +And you can see we got the object. + +21 +00:01:03,000 --> 00:01:05,000 +But the thing is customize itself is interface, right? + +22 +00:01:05,000 --> 00:01:07,000 +So if you want to get the object of it, + +23 +00:01:07,000 --> 00:01:09,000 +we have to use anonymous in a class. + +24 +00:01:09,000 --> 00:01:10,000 +That's what I'm doing here. + +25 +00:01:10,000 --> 00:01:13,000 +And in this interface we only have one method. + +26 +00:01:13,000 --> 00:01:15,000 +If you can see the method name is customize. + +27 +00:01:15,000 --> 00:01:18,000 +So what I can do is I can just define that meta customize + +28 +00:01:18,000 --> 00:01:21,000 +and okay, that's the object we have created. + +29 +00:01:21,000 --> 00:01:25,000 +So the object name is, or the reference name is custCsrf + +30 +00:01:25,000 --> 00:01:29,000 +and we have to pass that custCsrf here. + +31 +00:01:29,000 --> 00:01:31,000 +Okay, so CSRF says you can configure me + +32 +00:01:31,000 --> 00:01:33,000 +by passing the object of this + +33 +00:01:33,000 --> 00:01:36,000 +and this the object type is customizer + +34 +00:01:36,000 --> 00:01:40,000 +with the type CsrfConfigurer HttpSecurity. + +35 +00:01:40,000 --> 00:01:42,000 +Now if you're thinking, do we have to remember this? + +36 +00:01:42,000 --> 00:01:44,000 +Not exactly anywhere when you are actually going to build, + +37 +00:01:44,000 --> 00:01:47,000 +we will be using the lambda expression, + +38 +00:01:47,000 --> 00:01:48,000 +not this type of code. + +39 +00:01:48,000 --> 00:01:49,000 +I'm just showing you this + +40 +00:01:49,000 --> 00:01:51,000 +so that you can understand what is happening. + +41 +00:01:51,000 --> 00:01:53,000 +So in this particular object creation, + +42 +00:01:53,000 --> 00:01:54,000 +we are using a method called customize + +43 +00:01:54,000 --> 00:01:56,000 +because that's the only method you have, + +44 +00:01:56,000 --> 00:01:59,000 +which takes a parameter of CsrfConfigurer, + +45 +00:01:59,000 --> 00:02:02,000 +which we are passing here of type HTTP. + +46 +00:02:02,000 --> 00:02:04,000 +And in this you can do the configuration. + +47 +00:02:04,000 --> 00:02:07,000 +So what I will do is instead of saying this big name, + +48 +00:02:07,000 --> 00:02:09,000 +I will say configure. + +49 +00:02:09,000 --> 00:02:12,000 +And then using that object of configure, + +50 +00:02:12,000 --> 00:02:13,000 +I can simply say disable. + +51 +00:02:13,000 --> 00:02:15,000 +Okay, so whatever you want to do + +52 +00:02:15,000 --> 00:02:16,000 +with this configure, you can do that. + +53 +00:02:16,000 --> 00:02:19,000 +You can set different properties or settings. + +54 +00:02:19,000 --> 00:02:22,000 +I'm just saying disable and the same thing I'm passing here. + +55 +00:02:22,000 --> 00:02:25,000 +So this one line of code is actually doing all this. + +56 +00:02:25,000 --> 00:02:27,000 +Again, if you are good with lambda, you don't even have + +57 +00:02:27,000 --> 00:02:29,000 +to watch this video, you can skip it. + +58 +00:02:29,000 --> 00:02:32,000 +But I'm just trying to make you understand how we got this + +59 +00:02:32,000 --> 00:02:34,000 +because when I saw this for the first time, I was like "hey, + +60 +00:02:34,000 --> 00:02:36,000 +what's happening behind the scene". + +61 +00:02:36,000 --> 00:02:37,000 +And this is what is happening. + +62 +00:02:37,000 --> 00:02:41,000 +Okay, next let's talk about the authorized HTTP request. + +63 +00:02:41,000 --> 00:02:42,000 +The same thing we can do there. + +64 +00:02:42,000 --> 00:02:46,000 +So we have to say HTTP dot authorize HTTP request, + +65 +00:02:46,000 --> 00:02:50,000 +and even this needs object of customizer. + +66 +00:02:50,000 --> 00:02:54,000 +So if you go there, you can see this particular method needs + +67 +00:02:54,000 --> 00:02:55,000 +object of customizer + +68 +00:02:55,000 --> 00:02:59,000 +but of type authorized HTTP request configure + +69 +00:02:59,000 --> 00:03:01,000 +of type HttpSecurity, big names, right? + +70 +00:03:01,000 --> 00:03:03,000 +And that's why we prefer to go with Lambda there. + +71 +00:03:03,000 --> 00:03:05,000 +So for this we have to create the object, + +72 +00:03:05,000 --> 00:03:10,000 +I will say customizer of type authorized HTTP request, + +73 +00:03:10,000 --> 00:03:13,000 +configure of type HttpSecurity. + +74 +00:03:13,000 --> 00:03:16,000 +And then again you have to say dot the actual object we have + +75 +00:03:16,000 --> 00:03:21,000 +create is authorized manager request manager registry. + +76 +00:03:21,000 --> 00:03:26,000 +And for this we'll create object of cost HTTP + +77 +00:03:26,000 --> 00:03:30,000 +equal to new, the same thing. + +78 +00:03:30,000 --> 00:03:32,000 +And you can see it's very lengthy name, right? + +79 +00:03:32,000 --> 00:03:34,000 +And no one want to remember this. + +80 +00:03:34,000 --> 00:03:36,000 +So you can see it's such a big name. + +81 +00:03:36,000 --> 00:03:38,000 +So you have to get object of customizer with this type + +82 +00:03:38,000 --> 00:03:41,000 +of authorized manager request match registry. + +83 +00:03:41,000 --> 00:03:43,000 +And once you have this object, + +84 +00:03:43,000 --> 00:03:45,000 +because that's the object we have to pass here, + +85 +00:03:45,000 --> 00:03:47,000 +which is custHttp. + +86 +00:03:47,000 --> 00:03:50,000 +And here in this bracket or in this definition, + +87 +00:03:50,000 --> 00:03:54,000 +I will say what was the method name registry. + +88 +00:03:54,000 --> 00:03:57,000 +So I'll say registry dot + +89 +00:03:57,000 --> 00:04:00,000 +for every request dot authenticated. + +90 +00:04:00,000 --> 00:04:03,000 +So basically I'm saying get authenticated + +91 +00:04:03,000 --> 00:04:05,000 +and that's how we can basically pass this. + +92 +00:04:05,000 --> 00:04:08,000 +Yeah, so this is basically your imperative style + +93 +00:04:08,000 --> 00:04:11,000 +of writing this, not a good idea, right? + +94 +00:04:11,000 --> 00:04:13,000 +But then now you know which classes are involved + +95 +00:04:13,000 --> 00:04:16,000 +behind the scene, okay? + +96 +00:04:16,000 --> 00:04:20,000 +And preferably we'll be using the lambda. + +97 +00:04:20,000 --> 00:04:23,000 +So this is only for showing you how things looks like, + +98 +00:04:23,000 --> 00:04:26,000 +but this is not the actual way of writing code here. + +99 +00:04:26,000 --> 00:04:28,000 +I would still prefer the lambda. + +100 +00:04:28,000 --> 00:04:30,000 +Let me just uncomment this. + +101 +00:04:30,000 --> 00:04:32,000 +For the session, also, we have the same thing. + +102 +00:04:32,000 --> 00:04:33,000 +You can explore session management. + +103 +00:04:33,000 --> 00:04:36,000 +We have to create object of customizer session management, + +104 +00:04:36,000 --> 00:04:39,000 +configure which of type HttpSecurity and this will work. + +105 +00:04:39,000 --> 00:04:42,000 +There's one more way of doing this since HTTP + +106 +00:04:42,000 --> 00:04:43,000 +follows a buildup pattern. + +107 +00:04:43,000 --> 00:04:44,000 +Instead of writing all this multiple times, + +108 +00:04:44,000 --> 00:04:48,000 +what you can do is you can HTTP dot CSRF + +109 +00:04:48,000 --> 00:04:50,000 +and then you can say DOT with the same thing. + +110 +00:04:50,000 --> 00:04:52,000 +So this is a buildup pattern so you don't have + +111 +00:04:52,000 --> 00:04:54,000 +to mention semicolon there at the end. + +112 +00:04:54,000 --> 00:04:57,000 +So we are confidentiality HTTP for CSRF first, + +113 +00:04:57,000 --> 00:05:01,000 +then authorized request and then dot HTTP + +114 +00:05:01,000 --> 00:05:03,000 +and then dot session management. + +115 +00:05:03,000 --> 00:05:06,000 +So you can see we are doing this in a line now + +116 +00:05:06,000 --> 00:05:07,000 +and that's what we can do. + +117 +00:05:07,000 --> 00:05:10,000 +In fact, if you want, you can see at the end dot build + +118 +00:05:10,000 --> 00:05:11,000 +and return that object itself + +119 +00:05:11,000 --> 00:05:12,000 +instead of doing all those things. + +120 +00:05:12,000 --> 00:05:14,000 +And that's why most of the time you will see this type + +121 +00:05:14,000 --> 00:05:16,000 +of configuration or somewhere you can also see this in a new + +122 +00:05:16,000 --> 00:05:20,000 +line if you want just to make things visible. + +123 +00:05:20,000 --> 00:05:23,000 +So yeah, that's how we can configure the CSRF disable + +124 +00:05:23,000 --> 00:05:24,000 +and other things here. + +125 +00:05:24,000 --> 00:05:27,000 +There are many, many configuration which we can do. + +126 +00:05:27,000 --> 00:05:29,000 +Maybe having multiple username password, not just one. + +127 +00:05:29,000 --> 00:05:32,000 +But how do you do that, let's say in the upcoming videos. + diff --git a/20 - Spring Security/017 Source-Code.url b/20 - Spring Security/017 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..df7e49ac3e1c9a4987dcf6c656a2270e52bf4232 --- /dev/null +++ b/20 - Spring Security/017 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.17%20Working%20With%20Multiple%20Users/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/017 Working with Multiple Users_en.srt b/20 - Spring Security/017 Working with Multiple Users_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f94749c9de7365dff2dd7a04cc29ea43fcdd01ed --- /dev/null +++ b/20 - Spring Security/017 Working with Multiple Users_en.srt @@ -0,0 +1,860 @@ +1 +00:00:00,000 --> 00:00:03,000 +(upbeat music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now let's write a code so + +3 +00:00:05,000 --> 00:00:06,000 +that we can have multiple users + +4 +00:00:06,000 --> 00:00:09,000 +and of course we want the users coming from database, + +5 +00:00:09,000 --> 00:00:11,000 +but then we'll go step by step so that it'll make sense. + +6 +00:00:11,000 --> 00:00:14,000 +The first thing we will do is let's hardcode the username + +7 +00:00:14,000 --> 00:00:17,000 +password values, not from database, + +8 +00:00:17,000 --> 00:00:19,000 +but let's hardcode them here in the code. + +9 +00:00:19,000 --> 00:00:21,000 +And I don't want to use this username password now. + +10 +00:00:21,000 --> 00:00:24,000 +The way you can achieve that, if you go back + +11 +00:00:24,000 --> 00:00:25,000 +to your security configuration, + +12 +00:00:25,000 --> 00:00:28,000 +and if you don't mention anything here by default, + +13 +00:00:28,000 --> 00:00:30,000 +the spring security uses something called + +14 +00:00:30,000 --> 00:00:32,000 +a UserDetailsService. + +15 +00:00:32,000 --> 00:00:35,000 +Now it uses this class to basically check + +16 +00:00:35,000 --> 00:00:38,000 +for your application properties + +17 +00:00:38,000 --> 00:00:39,000 +and see do you have username password there? + +18 +00:00:39,000 --> 00:00:42,000 +If yes, it will use it, but I don't want to use that. + +19 +00:00:42,000 --> 00:00:46,000 +I want to define my own UserDetailsService. + +20 +00:00:46,000 --> 00:00:50,000 +So what we will do is let's create a bean which will + +21 +00:00:50,000 --> 00:00:53,000 +return a UserDetailsService object. + +22 +00:00:53,000 --> 00:00:57,000 +And I can say this is UserDetailsService method, okay? + +23 +00:00:57,000 --> 00:00:59,000 +So basically I want to return the object + +24 +00:00:59,000 --> 00:01:02,000 +of user detail service and I want this to be bean. + +25 +00:01:02,000 --> 00:01:05,000 +Now what will happen is your spring security will look at + +26 +00:01:05,000 --> 00:01:08,000 +this particular object to get the data for the user. + +27 +00:01:08,000 --> 00:01:10,000 +So whatever data you're going to return on this, + +28 +00:01:10,000 --> 00:01:13,000 +will be your actual data where a spring security will check. + +29 +00:01:13,000 --> 00:01:15,000 +Okay, makes sense, but how do you return the data? + +30 +00:01:15,000 --> 00:01:17,000 +How do I specify the users? + +31 +00:01:17,000 --> 00:01:20,000 +And basically how can I return this user detail service? + +32 +00:01:20,000 --> 00:01:23,000 +Now if you go back to user detail service, + +33 +00:01:23,000 --> 00:01:24,000 +it's an interface, right? + +34 +00:01:24,000 --> 00:01:27,000 +And it has a method called load user by the username, okay? + +35 +00:01:27,000 --> 00:01:29,000 +So this is a method which is getting used behind the scene. + +36 +00:01:29,000 --> 00:01:34,000 +Our concern is this particular interface, it's an interface. + +37 +00:01:34,000 --> 00:01:36,000 +First of all, it has a method which returns the object + +38 +00:01:36,000 --> 00:01:37,000 +of user detail service. + +39 +00:01:37,000 --> 00:01:40,000 +So for us, the two important thing is this interface name + +40 +00:01:40,000 --> 00:01:43,000 +and it returns return type, which is user details. + +41 +00:01:43,000 --> 00:01:44,000 +If I go back here + +42 +00:01:44,000 --> 00:01:47,000 +and if I try to understand how do I create an object + +43 +00:01:47,000 --> 00:01:52,000 +for this, of course we don't want to get our own class now. + +44 +00:01:52,000 --> 00:01:54,000 +There is an inbuilt class which we can use. + +45 +00:01:54,000 --> 00:01:55,000 +So I can simply say return. + +46 +00:01:55,000 --> 00:01:57,000 +And the name of the inbuilt class is, if you can see, + +47 +00:01:57,000 --> 00:02:02,000 +we have something called InMemoryUserDetailsManager. + +48 +00:02:02,000 --> 00:02:05,000 +So we are going to use this because this particular object, + +49 +00:02:05,000 --> 00:02:09,000 +and if you see this implements, user details manager, + +50 +00:02:09,000 --> 00:02:11,000 +which extends user detail service. + +51 +00:02:11,000 --> 00:02:14,000 +So indirectly, this particular class, + +52 +00:02:14,000 --> 00:02:16,000 +which is InMemoryUserDetailsManager + +53 +00:02:16,000 --> 00:02:17,000 +implements the user detail service. + +54 +00:02:17,000 --> 00:02:19,000 +So our job is done because if you can return the object + +55 +00:02:19,000 --> 00:02:21,000 +of this indirectly hitting the object + +56 +00:02:21,000 --> 00:02:23,000 +of user detail service, okay, + +57 +00:02:23,000 --> 00:02:26,000 +but then we don't want to return the empty object, right? + +58 +00:02:26,000 --> 00:02:28,000 +We have to specify some values. + +59 +00:02:28,000 --> 00:02:31,000 +Let's try, and I want to really check if this works. + +60 +00:02:31,000 --> 00:02:35,000 +If I do this, it should not even accept the username + +61 +00:02:35,000 --> 00:02:36,000 +and password which is + +62 +00:02:36,000 --> 00:02:38,000 +specified in the application properties. + +63 +00:02:38,000 --> 00:02:41,000 +Let's try it out, so I hope this will, okay, + +64 +00:02:41,000 --> 00:02:43,000 +I will just manually restart sometime. + +65 +00:02:43,000 --> 00:02:45,000 +We don't trust the hard reload, + +66 +00:02:45,000 --> 00:02:46,000 +especially when you're experimenting. + +67 +00:02:46,000 --> 00:02:48,000 +Okay, reload, done, let me go back to the browser + +68 +00:02:48,000 --> 00:02:50,000 +and again, I will go for the incognito mode. + +69 +00:02:52,000 --> 00:02:55,000 +And here let's specify localized 8080. + +70 +00:02:55,000 --> 00:02:58,000 +It'll give you a prompt and now I will specify Telusko. + +71 +00:02:58,000 --> 00:03:01,000 +And 1, 2, 3, 4, enter, it's not working. + +72 +00:03:01,000 --> 00:03:02,000 +So basically the username, password, + +73 +00:03:02,000 --> 00:03:04,000 +which you have mentioned in the application properties + +74 +00:03:04,000 --> 00:03:06,000 +is not getting read. + +75 +00:03:06,000 --> 00:03:08,000 +Okay, so now let's specify the values here. + +76 +00:03:08,000 --> 00:03:10,000 +Now how do I specify the values? + +77 +00:03:10,000 --> 00:03:13,000 +See in this constructor of InMemoryUserDetailsManager, + +78 +00:03:13,000 --> 00:03:16,000 +if you click here, you can see it has multiple constructors. + +79 +00:03:16,000 --> 00:03:20,000 +One of the constructor is this, a collection of users. + +80 +00:03:20,000 --> 00:03:22,000 +If you don't want to pass a collection, + +81 +00:03:22,000 --> 00:03:25,000 +you can also pass a variable length arguments, + +82 +00:03:25,000 --> 00:03:26,000 +which is with Varargs, + +83 +00:03:26,000 --> 00:03:28,000 +and you can pass multiple users. + +84 +00:03:28,000 --> 00:03:29,000 +So I want to use this particular constructor + +85 +00:03:29,000 --> 00:03:33,000 +and I can pass any number of users I want. + +86 +00:03:33,000 --> 00:03:34,000 +And what object, what type of object + +87 +00:03:34,000 --> 00:03:37,000 +you wanna return, the user details object. + +88 +00:03:37,000 --> 00:03:39,000 +So that means here, even if you want + +89 +00:03:39,000 --> 00:03:41,000 +to create one particular user, you have + +90 +00:03:41,000 --> 00:03:42,000 +to create object of user details. + +91 +00:03:42,000 --> 00:03:44,000 +Here I would say user is equal to, + +92 +00:03:44,000 --> 00:03:46,000 +I should be importing this package. + +93 +00:03:46,000 --> 00:03:48,000 +I think it's already done. + +94 +00:03:48,000 --> 00:03:50,000 +And now how do I create the object of this? + +95 +00:03:50,000 --> 00:03:54,000 +If you go back to user details, oh, this is also interface. + +96 +00:03:54,000 --> 00:03:56,000 +I mean it's all coded for the interface, no problem. + +97 +00:03:56,000 --> 00:04:00,000 +We'll go back here and there's a class called user. + +98 +00:04:00,000 --> 00:04:03,000 +Okay, so the way we have user details, we also have user, + +99 +00:04:03,000 --> 00:04:07,000 +and this particular class, it has multiple methods. + +100 +00:04:07,000 --> 00:04:09,000 +It returns object of user builder, + +101 +00:04:09,000 --> 00:04:11,000 +if you can see, and user builder. + +102 +00:04:11,000 --> 00:04:14,000 +So it has a method called build, which returns user details. + +103 +00:04:14,000 --> 00:04:17,000 +And of course you don't have to remember every flow here, + +104 +00:04:17,000 --> 00:04:22,000 +just imagine the user has a method called user builder, + +105 +00:04:22,000 --> 00:04:24,000 +I mean a builder.build. + +106 +00:04:24,000 --> 00:04:26,000 +So basically user.builder.build will give + +107 +00:04:26,000 --> 00:04:27,000 +you the object of user details. + +108 +00:04:27,000 --> 00:04:28,000 +But then this is empty, right? + +109 +00:04:28,000 --> 00:04:30,000 +We don't want to go with empty one. + +110 +00:04:30,000 --> 00:04:31,000 +I want to specify some values to it. + +111 +00:04:31,000 --> 00:04:33,000 +So the first thing we have to mention + +112 +00:04:33,000 --> 00:04:34,000 +is the password encoder. + +113 +00:04:34,000 --> 00:04:36,000 +As I mentioned before, you don't want + +114 +00:04:36,000 --> 00:04:38,000 +to store your password in a plain text. + +115 +00:04:38,000 --> 00:04:40,000 +You want to store that in a encoded format. + +116 +00:04:40,000 --> 00:04:42,000 +So this, to start with, let's not do any encoding. + +117 +00:04:42,000 --> 00:04:45,000 +I will go for a default password encoder, this one. + +118 +00:04:45,000 --> 00:04:48,000 +So what I'm doing is I'm saying, "Hey, I'm going + +119 +00:04:48,000 --> 00:04:49,000 +for a default password encoder + +120 +00:04:49,000 --> 00:04:51,000 +and now I want to specify my username". + +121 +00:04:51,000 --> 00:04:54,000 +So you can say .username and you can mention the username. + +122 +00:04:54,000 --> 00:04:55,000 +So let's say I want to go for username + +123 +00:04:55,000 --> 00:04:57,000 +as navin and then you can say dot. + +124 +00:04:57,000 --> 00:04:58,000 +You can also set the password. + +125 +00:04:58,000 --> 00:05:01,000 +I want to set the password as n@123. + +126 +00:05:01,000 --> 00:05:03,000 +That's a password, and then if you want, + +127 +00:05:03,000 --> 00:05:05,000 +you can also specify the roles + +128 +00:05:05,000 --> 00:05:07,000 +and you can specify multiple roles here. + +129 +00:05:07,000 --> 00:05:11,000 +So if navin, I wanted to be admin as well as I want navin + +130 +00:05:11,000 --> 00:05:15,000 +to be user, I can specify both, + +131 +00:05:15,000 --> 00:05:16,000 +but at this point, let's say I want + +132 +00:05:16,000 --> 00:05:17,000 +to specify only one thing, + +133 +00:05:17,000 --> 00:05:18,000 +and that too makes sense if you have + +134 +00:05:18,000 --> 00:05:20,000 +in the capital, so it's a user. + +135 +00:05:20,000 --> 00:05:22,000 +And then at the end you can say build + +136 +00:05:22,000 --> 00:05:24,000 +because build returns object of user details. + +137 +00:05:24,000 --> 00:05:26,000 +And that's what we want, right? + +138 +00:05:26,000 --> 00:05:27,000 +Now if you want this to look good, + +139 +00:05:27,000 --> 00:05:29,000 +what you can also do is at this point you can say enter just + +140 +00:05:29,000 --> 00:05:32,000 +like a buildup pattern and you can say enter here. + +141 +00:05:32,000 --> 00:05:35,000 +So at least it'll make it much more readable + +142 +00:05:35,000 --> 00:05:37,000 +compared to the earlier one. + +143 +00:05:37,000 --> 00:05:40,000 +And if you try to read now what it says is, + +144 +00:05:40,000 --> 00:05:44,000 +we got a user in which we have a chain of methods. + +145 +00:05:44,000 --> 00:05:45,000 +We are going for a default password encoder. + +146 +00:05:45,000 --> 00:05:47,000 +Then we are going for the username, password, + +147 +00:05:47,000 --> 00:05:49,000 +roles and build. + +148 +00:05:49,000 --> 00:05:51,000 +And this particular object, the user object, + +149 +00:05:51,000 --> 00:05:52,000 +I can simply pass here + +150 +00:05:52,000 --> 00:05:56,000 +because that's how we can pass the object of user details + +151 +00:05:56,000 --> 00:05:58,000 +to the constructor here. + +152 +00:05:58,000 --> 00:06:00,000 +And you can pass multiple objects, not just one user. + +153 +00:06:00,000 --> 00:06:02,000 +You can create multiple user details, object, + +154 +00:06:02,000 --> 00:06:04,000 +and you can pass it here for multiple users. + +155 +00:06:04,000 --> 00:06:06,000 +And now let's talk about this. + +156 +00:06:06,000 --> 00:06:08,000 +What is this with default password encoder? + +157 +00:06:08,000 --> 00:06:11,000 +Now basically this is a method which says I don't want to go + +158 +00:06:11,000 --> 00:06:13,000 +for any encoder at this point. + +159 +00:06:13,000 --> 00:06:15,000 +Later on we'll move to different encoders, + +160 +00:06:15,000 --> 00:06:16,000 +but at this point we're not using it. + +161 +00:06:16,000 --> 00:06:18,000 +But again, not recommended. + +162 +00:06:18,000 --> 00:06:20,000 +And that's why it says it is deprecated. + +163 +00:06:20,000 --> 00:06:22,000 +We should not be using this in the production + +164 +00:06:22,000 --> 00:06:23,000 +or I mean it, + +165 +00:06:23,000 --> 00:06:25,000 +you should be only using it while learning purpose. + +166 +00:06:25,000 --> 00:06:27,000 +Never use it for even for experiments. + +167 +00:06:27,000 --> 00:06:29,000 +Sometime yes, you want + +168 +00:06:29,000 --> 00:06:30,000 +to check if everything is flowing well + +169 +00:06:30,000 --> 00:06:33,000 +and you have a pathway in database. + +170 +00:06:33,000 --> 00:06:35,000 +And if you want to verify, that's when you can do it, + +171 +00:06:35,000 --> 00:06:37,000 +but not in a normal scenario. + +172 +00:06:37,000 --> 00:06:39,000 +Okay, so we got a user and I can do the same thing. + +173 +00:06:39,000 --> 00:06:43,000 +Let's say I want to have one more user here, which is this. + +174 +00:06:43,000 --> 00:06:46,000 +And I can say this is user, maybe I want + +175 +00:06:46,000 --> 00:06:48,000 +to create an admin now with default encoder. + +176 +00:06:48,000 --> 00:06:50,000 +And I will say, let's execute in, + +177 +00:06:50,000 --> 00:06:52,000 +or maybe I can have admin itself. + +178 +00:06:52,000 --> 00:06:56,000 +And the password I want to go for is admin@789. + +179 +00:06:58,000 --> 00:07:00,000 +And the goal I want to give here is admin, okay? + +180 +00:07:00,000 --> 00:07:02,000 +That's how we can create another user here. + +181 +00:07:02,000 --> 00:07:05,000 +And here we can pass admin as well. + +182 +00:07:05,000 --> 00:07:06,000 +So depending upon how many objects you have, + +183 +00:07:06,000 --> 00:07:08,000 +we can pass those objects here. + +184 +00:07:08,000 --> 00:07:11,000 +Now I hope this will work, let me relaunch again. + +185 +00:07:11,000 --> 00:07:16,000 +I don't trust the hot reloads, so it's launching. + +186 +00:07:16,000 --> 00:07:18,000 +I will go back to my browser, refresh. + +187 +00:07:18,000 --> 00:07:20,000 +Okay, so we're not started yet. + +188 +00:07:20,000 --> 00:07:22,000 +So I was started and now here I will say, + +189 +00:07:22,000 --> 00:07:24,000 +let me try Telusko once again. + +190 +00:07:24,000 --> 00:07:26,000 +And I would say 1234, not working. + +191 +00:07:26,000 --> 00:07:28,000 +Let me say Navin and the password, + +192 +00:07:28,000 --> 00:07:30,000 +I will just enter a random password, which is incorrect. + +193 +00:07:30,000 --> 00:07:32,000 +Sign in not working. + +194 +00:07:32,000 --> 00:07:37,000 +Again, I will send Navin and the password is n@123, + +195 +00:07:37,000 --> 00:07:39,000 +sign in, worked, okay? + +196 +00:07:39,000 --> 00:07:42,000 +So whatever details + +197 +00:07:42,000 --> 00:07:43,000 +or whatever username password you mentioned, + +198 +00:07:43,000 --> 00:07:45,000 +you can use that here. + +199 +00:07:45,000 --> 00:07:47,000 +What if I wanna try with admin, let's try. + +200 +00:07:47,000 --> 00:07:49,000 +So that's the new incognito mode. + +201 +00:07:49,000 --> 00:07:53,000 +I would say admin, password is admin@789. + +202 +00:07:53,000 --> 00:07:55,000 +Enter, it worked. + +203 +00:07:55,000 --> 00:07:56,000 +So basically whatever username password + +204 +00:07:56,000 --> 00:07:58,000 +you mentioned here, it will work. + +205 +00:07:58,000 --> 00:07:59,000 +But again, this is not coming from database. + +206 +00:07:59,000 --> 00:08:01,000 +Let's see, how do you make it work + +207 +00:08:01,000 --> 00:08:03,000 +with the database in the upcoming videos. + +208 +00:08:03,000 --> 00:08:05,000 +But at this point, what we are doing is we are not going + +209 +00:08:05,000 --> 00:08:07,000 +for a default implementation of user detail service. + +210 +00:08:07,000 --> 00:08:09,000 +We are defining it by ourself. + +211 +00:08:09,000 --> 00:08:11,000 +And the way you can do that is by returning the object + +212 +00:08:11,000 --> 00:08:14,000 +of InMemoryUserDetailsManager. + +213 +00:08:14,000 --> 00:08:16,000 +And we are returning these two objects. + +214 +00:08:16,000 --> 00:08:18,000 +So yeah, that's it from this video. + +215 +00:08:18,000 --> 00:08:19,000 +See you in the upcoming videos. + diff --git a/20 - Spring Security/018 Source-Code.url b/20 - Spring Security/018 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..d21ea815a83b0ea8ef525704ef30d979978e3459 --- /dev/null +++ b/20 - Spring Security/018 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.18%20Creating%20User%20Table%20And%20Db%20Properties/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/019 AuthenticationProvider_en.srt b/20 - Spring Security/019 AuthenticationProvider_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..0dbc186f80a50b59cb536694ac7f03ac8f530586 --- /dev/null +++ b/20 - Spring Security/019 AuthenticationProvider_en.srt @@ -0,0 +1,536 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: And now let's try to create that provider. + +3 +00:00:05,000 --> 00:00:09,000 +So basically behind the scene, you are the Spring config. + +4 +00:00:09,000 --> 00:00:11,000 +Whenever you implement Spring security, + +5 +00:00:11,000 --> 00:00:14,000 +it'll provide you a authentication provider. + +6 +00:00:14,000 --> 00:00:16,000 +But I don't want to use default one + +7 +00:00:16,000 --> 00:00:17,000 +because I want to create my own now, + +8 +00:00:17,000 --> 00:00:18,000 +which we will link with database. + +9 +00:00:18,000 --> 00:00:21,000 +So what I will do is I will create a bean + +10 +00:00:21,000 --> 00:00:26,000 +which returns an authentication provider, okay? + +11 +00:00:26,000 --> 00:00:28,000 +And of course you can have multiple providers + +12 +00:00:28,000 --> 00:00:29,000 +in the same application. + +13 +00:00:29,000 --> 00:00:31,000 +At this point, I'm just working with DAO. + +14 +00:00:31,000 --> 00:00:33,000 +So I will say public authentication provider + +15 +00:00:33,000 --> 00:00:35,000 +and I will have the method name as auth provider. + +16 +00:00:35,000 --> 00:00:38,000 +Now what we to do is first of all, we have to make this + +17 +00:00:38,000 --> 00:00:40,000 +as a bean and then we have to return the object + +18 +00:00:40,000 --> 00:00:42,000 +of authentication provider. + +19 +00:00:42,000 --> 00:00:44,000 +Now if you jump to authentication provider, + +20 +00:00:44,000 --> 00:00:45,000 +you can see it's the interface. + +21 +00:00:45,000 --> 00:00:46,000 +Basically we have to search + +22 +00:00:46,000 --> 00:00:49,000 +for the class which implements this interface, right? + +23 +00:00:49,000 --> 00:00:52,000 +And it has only one method visit, which is authenticate. + +24 +00:00:52,000 --> 00:00:55,000 +So basically whenever you pass a username password, + +25 +00:00:55,000 --> 00:00:58,000 +that becomes a part of authentication object. + +26 +00:00:58,000 --> 00:00:59,000 +You can see we have an object here + +27 +00:00:59,000 --> 00:01:02,000 +and then this using this object, it will authenticate it. + +28 +00:01:02,000 --> 00:01:03,000 +If the user is there, + +29 +00:01:03,000 --> 00:01:05,000 +of course you will get the object of authentication. + +30 +00:01:05,000 --> 00:01:08,000 +If the user is not there, if the authentication fails, + +31 +00:01:08,000 --> 00:01:09,000 +it'll throw an exception. + +32 +00:01:09,000 --> 00:01:11,000 +Okay? So that's what we want. + +33 +00:01:11,000 --> 00:01:12,000 +But then I don't have a class, right? + +34 +00:01:12,000 --> 00:01:14,000 +So do we have a class in build? + +35 +00:01:14,000 --> 00:01:16,000 +Now for different type of authentication, + +36 +00:01:16,000 --> 00:01:18,000 +we have different type of authentication provider. + +37 +00:01:18,000 --> 00:01:20,000 +Now since we are working with database, + +38 +00:01:20,000 --> 00:01:23,000 +we have something called a DAO authentication provider. + +39 +00:01:23,000 --> 00:01:25,000 +So if you jump here, + +40 +00:01:25,000 --> 00:01:26,000 +you can see this is a class which + +41 +00:01:26,000 --> 00:01:27,000 +is DAO authentication provider. + +42 +00:01:27,000 --> 00:01:30,000 +It's a class that means we can create an object + +43 +00:01:30,000 --> 00:01:31,000 +and this extends the + +44 +00:01:31,000 --> 00:01:34,000 +abstract user details authentication provider. + +45 +00:01:34,000 --> 00:01:37,000 +If I click on this, it implements authentication provider + +46 +00:01:37,000 --> 00:01:39,000 +so that it'll do our job, right? + +47 +00:01:39,000 --> 00:01:41,000 +So that means if I create object + +48 +00:01:41,000 --> 00:01:44,000 +of DAO authentication provider, I would say provider + +49 +00:01:44,000 --> 00:01:47,000 +equal to new authentication provider. + +50 +00:01:47,000 --> 00:01:50,000 +And then if I return the same object provider, + +51 +00:01:50,000 --> 00:01:51,000 +our job is done, right? + +52 +00:01:51,000 --> 00:01:53,000 +See, we wanted the object of authentication provider + +53 +00:01:53,000 --> 00:01:56,000 +and we got the object of authentication provider + +54 +00:01:56,000 --> 00:01:58,000 +and we are returning it, right? + +55 +00:01:58,000 --> 00:02:00,000 +Now, this basically connects with the database + +56 +00:02:00,000 --> 00:02:02,000 +and it'll return the object. + +57 +00:02:02,000 --> 00:02:04,000 +And that's the thing we want, right? + +58 +00:02:04,000 --> 00:02:05,000 +Now will this work? + +59 +00:02:05,000 --> 00:02:07,000 +See, we have done the job, + +60 +00:02:07,000 --> 00:02:09,000 +we have created the authentication provider, + +61 +00:02:09,000 --> 00:02:11,000 +but then this will not work + +62 +00:02:11,000 --> 00:02:14,000 +because this particular class object has no idea + +63 +00:02:14,000 --> 00:02:16,000 +which DBMS you're working with. + +64 +00:02:16,000 --> 00:02:18,000 +How do I represent a user class? + +65 +00:02:18,000 --> 00:02:20,000 +And what is the user table name? + +66 +00:02:20,000 --> 00:02:22,000 +So we have to mention all those things. + +67 +00:02:22,000 --> 00:02:24,000 +And the way you can do that is we have + +68 +00:02:24,000 --> 00:02:26,000 +to create a user detail service. + +69 +00:02:26,000 --> 00:02:27,000 +We do have it here, + +70 +00:02:27,000 --> 00:02:29,000 +but then this user detail service + +71 +00:02:29,000 --> 00:02:31,000 +works with the static values. + +72 +00:02:31,000 --> 00:02:33,000 +We don't want to work with static values. + +73 +00:02:33,000 --> 00:02:35,000 +So what I will do is I will just commend this part. + +74 +00:02:35,000 --> 00:02:37,000 +I don't want to work with static values now. + +75 +00:02:37,000 --> 00:02:40,000 +I want to have a dynamic database connectivity. + +76 +00:02:40,000 --> 00:02:44,000 +And to achieve this, to create a user digital service, + +77 +00:02:44,000 --> 00:02:45,000 +first of all, why do we need it? + +78 +00:02:45,000 --> 00:02:47,000 +So if you say provider here, + +79 +00:02:47,000 --> 00:02:49,000 +and when you say dot, + +80 +00:02:49,000 --> 00:02:53,000 +there's a option of set user details service, + +81 +00:02:53,000 --> 00:02:55,000 +because authentication provider will say, I will check, + +82 +00:02:55,000 --> 00:02:57,000 +but tell me how do I connect with the user table? + +83 +00:02:57,000 --> 00:02:59,000 +What's the process I have follow? + +84 +00:02:59,000 --> 00:03:02,000 +And that is taken care by the user detailed service. + +85 +00:03:02,000 --> 00:03:04,000 +And that's what we have done before that as well. + +86 +00:03:04,000 --> 00:03:06,000 +But then that was study values. + +87 +00:03:06,000 --> 00:03:08,000 +I want to use this service for the database connectivity. + +88 +00:03:08,000 --> 00:03:10,000 +And for that we have to create a separate class + +89 +00:03:10,000 --> 00:03:12,000 +to achieve that. + +90 +00:03:12,000 --> 00:03:15,000 +And that class object need to specify here. + +91 +00:03:15,000 --> 00:03:18,000 +Now this particular method needs object + +92 +00:03:18,000 --> 00:03:19,000 +of user detail service. + +93 +00:03:19,000 --> 00:03:21,000 +So what I can do is + +94 +00:03:21,000 --> 00:03:24,000 +before we create a class, I want to create a bean here. + +95 +00:03:24,000 --> 00:03:28,000 +So I would say private user details service, + +96 +00:03:28,000 --> 00:03:29,000 +and I would say user detailed service. + +97 +00:03:29,000 --> 00:03:32,000 +And I want this to be auto wired. + +98 +00:03:32,000 --> 00:03:34,000 +So basically I'm asking, Hey, Spring Framework, + +99 +00:03:34,000 --> 00:03:36,000 +I want the object of user digital service. + +100 +00:03:36,000 --> 00:03:37,000 +I don't know where, from where you get it, + +101 +00:03:37,000 --> 00:03:39,000 +give me this object so that I can pass it here. + +102 +00:03:39,000 --> 00:03:42,000 +Okay? But then even Spring has no idea how do we do that? + +103 +00:03:42,000 --> 00:03:45,000 +Okay, so we have to configure this. + +104 +00:03:45,000 --> 00:03:48,000 +We have to basically implement the user digital service + +105 +00:03:48,000 --> 00:03:49,000 +because that's an interface. + +106 +00:03:49,000 --> 00:03:50,000 +And that's why I mentioned, right? + +107 +00:03:50,000 --> 00:03:52,000 +We have multiple classes to implement. + +108 +00:03:52,000 --> 00:03:55,000 +So basically the provider here, okay, where? Where's that? + +109 +00:03:55,000 --> 00:03:57,000 +Let me just close all the extra files. + +110 +00:03:57,000 --> 00:03:59,000 +So yeah, so the provider here is dependent + +111 +00:03:59,000 --> 00:04:01,000 +on the user data service. + +112 +00:04:01,000 --> 00:04:04,000 +If we can pass the object of this, our job is done, + +113 +00:04:04,000 --> 00:04:06,000 +but then this is the interface, right? + +114 +00:04:06,000 --> 00:04:07,000 +We have to create a class which will implement + +115 +00:04:07,000 --> 00:04:09,000 +this interface where we have + +116 +00:04:09,000 --> 00:04:11,000 +the implementation and then this will work. + +117 +00:04:11,000 --> 00:04:12,000 +So that's the first thing you have to do. + +118 +00:04:12,000 --> 00:04:14,000 +The second one is we have + +119 +00:04:14,000 --> 00:04:16,000 +to also specify the password encoder. + +120 +00:04:16,000 --> 00:04:19,000 +Again, I don't want to use any password encoder here. + +121 +00:04:19,000 --> 00:04:21,000 +So I will say set password encoder. + +122 +00:04:21,000 --> 00:04:23,000 +If you don't want to have any password encoder, + +123 +00:04:23,000 --> 00:04:24,000 +you can simply opt + +124 +00:04:24,000 --> 00:04:28,000 +for NoOpPasswordEncoder.getinstance. + +125 +00:04:28,000 --> 00:04:31,000 +So we are getting the object of no op password encoder. + +126 +00:04:31,000 --> 00:04:33,000 +So basically I don't want to use any password encoder. + +127 +00:04:33,000 --> 00:04:36,000 +In future, if you want to use any password encoder, + +128 +00:04:36,000 --> 00:04:37,000 +just replace this and your job is done. + +129 +00:04:37,000 --> 00:04:39,000 +And we are saying set password encoder. That's it. + +130 +00:04:39,000 --> 00:04:41,000 +These are the two things you have to mention. + +131 +00:04:41,000 --> 00:04:42,000 +But again, this will not work + +132 +00:04:42,000 --> 00:04:44,000 +because we don't have the implementation + +133 +00:04:44,000 --> 00:04:45,000 +for the use detailed service. + +134 +00:04:45,000 --> 00:04:48,000 +Okay, how do I do that? Let's see in the next video. + diff --git a/20 - Spring Security/019 Source-Code.url b/20 - Spring Security/019 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..54ff549a3f08f246ca29e1b099530c3bf16bdf6a --- /dev/null +++ b/20 - Spring Security/019 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.19%20Authenticationprovider/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/020 Creating a UserDetailsService_en.srt b/20 - Spring Security/020 Creating a UserDetailsService_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..eb260f86186b266c60527222b907d1021a922fd4 --- /dev/null +++ b/20 - Spring Security/020 Creating a UserDetailsService_en.srt @@ -0,0 +1,384 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:07,000 +-: Okay, now it's time to implement the user details service. + +3 +00:00:07,000 --> 00:00:10,000 +So what I will do is I will create a new class. + +4 +00:00:10,000 --> 00:00:12,000 +And in fact, you know, before we do that, + +5 +00:00:12,000 --> 00:00:15,000 +I just want to put everything in a particular package + +6 +00:00:15,000 --> 00:00:17,000 +that should make sense. + +7 +00:00:17,000 --> 00:00:20,000 +So I will just create a new package + +8 +00:00:20,000 --> 00:00:22,000 +and I will say these are controllers. + +9 +00:00:22,000 --> 00:00:24,000 +So I will have all my controllers here. + +10 +00:00:24,000 --> 00:00:27,000 +So basically I want to move the hello controller, + +11 +00:00:27,000 --> 00:00:30,000 +string controller, and this particular controller. + +12 +00:00:30,000 --> 00:00:32,000 +Yes, refactor. + +13 +00:00:32,000 --> 00:00:36,000 +I want to have a model class somewhere else. + +14 +00:00:36,000 --> 00:00:38,000 +I will say model. + +15 +00:00:38,000 --> 00:00:40,000 +And the student basically is a model class. + +16 +00:00:40,000 --> 00:00:42,000 +I will move that to, oh, I've created a model class, + +17 +00:00:42,000 --> 00:00:43,000 +not the package. + +18 +00:00:48,000 --> 00:00:51,000 +So model package, the student will go there. + +19 +00:00:51,000 --> 00:00:54,000 +And then I want to have a service layer as well. + +20 +00:00:54,000 --> 00:00:56,000 +Maybe in your project you want + +21 +00:00:56,000 --> 00:00:57,000 +to have multiple service classes. + +22 +00:00:57,000 --> 00:00:59,000 +I will say this is service layer. + +23 +00:00:59,000 --> 00:01:00,000 +And in this service layer, + +24 +00:01:00,000 --> 00:01:03,000 +I want to create a class for the user details service + +25 +00:01:03,000 --> 00:01:05,000 +because that's a service, right? + +26 +00:01:05,000 --> 00:01:07,000 +So I'll just right click here and say New Java class + +27 +00:01:07,000 --> 00:01:11,000 +and I will name this as my user details service, + +28 +00:01:11,000 --> 00:01:14,000 +because this is going to implement user details service. + +29 +00:01:14,000 --> 00:01:15,000 +I will say create. + +30 +00:01:15,000 --> 00:01:20,000 +And this is going to implement the user details service. + +31 +00:01:20,000 --> 00:01:22,000 +Okay, so we've got a class now + +32 +00:01:22,000 --> 00:01:24,000 +and this class object, + +33 +00:01:24,000 --> 00:01:27,000 +basically I have to pass in here, okay? + +34 +00:01:27,000 --> 00:01:31,000 +And the way you can do that is by making this as a service. + +35 +00:01:31,000 --> 00:01:32,000 +Of course you can also say component, + +36 +00:01:32,000 --> 00:01:33,000 +but this is service layer. + +37 +00:01:33,000 --> 00:01:35,000 +So service make much more sense. + +38 +00:01:35,000 --> 00:01:37,000 +Now what we are doing is when you say auto wire, + +39 +00:01:37,000 --> 00:01:40,000 +this particular class object will be injected, + +40 +00:01:40,000 --> 00:01:42,000 +but then you are getting an error here. + +41 +00:01:42,000 --> 00:01:43,000 +We are getting the error + +42 +00:01:43,000 --> 00:01:46,000 +because if you go to user detail, user detailed service, + +43 +00:01:46,000 --> 00:01:49,000 +it has a method called load user by username. + +44 +00:01:49,000 --> 00:01:53,000 +That means we have to implement that particular method. + +45 +00:01:53,000 --> 00:01:55,000 +So what I will do here is I will go back here + +46 +00:01:55,000 --> 00:01:57,000 +and let's implement it. + +47 +00:01:57,000 --> 00:01:59,000 +I will simply ask my IDE say, + +48 +00:01:59,000 --> 00:02:02,000 +implement the methods and it will just implement the method. + +49 +00:02:02,000 --> 00:02:04,000 +Of course the logic you have to define, + +50 +00:02:04,000 --> 00:02:06,000 +but it'll give you a structure. + +51 +00:02:06,000 --> 00:02:08,000 +So the structure is this particular class + +52 +00:02:08,000 --> 00:02:11,000 +or this particular method, which is load user by username, + +53 +00:02:11,000 --> 00:02:14,000 +takes a string username as a parameter, + +54 +00:02:14,000 --> 00:02:16,000 +and then we can perform the operation there. + +55 +00:02:16,000 --> 00:02:18,000 +And after performing the operation, + +56 +00:02:18,000 --> 00:02:22,000 +we have to return the object of user details. + +57 +00:02:22,000 --> 00:02:24,000 +Now, this is some, this is something we have to do now. + +58 +00:02:24,000 --> 00:02:26,000 +Oh, okay, the code is not getting completed, right? + +59 +00:02:26,000 --> 00:02:28,000 +But yeah, we'll be there, don't worry. + +60 +00:02:28,000 --> 00:02:31,000 +So since we are going step by step, let's focus. + +61 +00:02:31,000 --> 00:02:33,000 +So we have lower the user by username. + +62 +00:02:33,000 --> 00:02:34,000 +So that's the method we have to implement. + +63 +00:02:34,000 --> 00:02:36,000 +Now if you observe, + +64 +00:02:36,000 --> 00:02:38,000 +basically we are saying load user by username. + +65 +00:02:38,000 --> 00:02:40,000 +That means when you get a username, + +66 +00:02:40,000 --> 00:02:45,000 +we have to basically hit the database to get this detail. + +67 +00:02:45,000 --> 00:02:48,000 +Now question guys, how will you hit the database + +68 +00:02:48,000 --> 00:02:49,000 +and how will you return this? + +69 +00:02:49,000 --> 00:02:51,000 +Now to hit the database, + +70 +00:02:51,000 --> 00:02:53,000 +we need to have a repo layer, right? + +71 +00:02:53,000 --> 00:02:55,000 +Database connectivity is done by the repo layer, + +72 +00:02:55,000 --> 00:02:56,000 +not the service class. + +73 +00:02:56,000 --> 00:03:00,000 +And for that, we have to create object of user repo. + +74 +00:03:00,000 --> 00:03:02,000 +Now, user repo is not a class. + +75 +00:03:02,000 --> 00:03:04,000 +This is something we have to create. + +76 +00:03:04,000 --> 00:03:05,000 +It can be a class or the interface. + +77 +00:03:05,000 --> 00:03:07,000 +If you're using normal JDPC, we have to get a class. + +78 +00:03:07,000 --> 00:03:10,000 +If you're using JPA, we can do that + +79 +00:03:10,000 --> 00:03:11,000 +with the help of interface. + +80 +00:03:11,000 --> 00:03:13,000 +So I'm saying user repo, repo, + +81 +00:03:13,000 --> 00:03:15,000 +and I want this to be auto wired. + +82 +00:03:15,000 --> 00:03:20,000 +I will say auto wired and I will make this as private. + +83 +00:03:20,000 --> 00:03:22,000 +Now, basically using this particular class + +84 +00:03:22,000 --> 00:03:25,000 +or the interface, we have to get the database connectivity + +85 +00:03:25,000 --> 00:03:27,000 +and implement the code here. + +86 +00:03:27,000 --> 00:03:28,000 +Now the code is actually very simple. + +87 +00:03:28,000 --> 00:03:31,000 +The code is you use this particular repository + +88 +00:03:31,000 --> 00:03:34,000 +and fire the query to the data database. + +89 +00:03:34,000 --> 00:03:38,000 +If you get a user in return, that's great, you got a login. + +90 +00:03:38,000 --> 00:03:41,000 +But what if you don't have a user that time you can say, + +91 +00:03:41,000 --> 00:03:44,000 +user not found, or user 404, some error, right? + +92 +00:03:44,000 --> 00:03:46,000 +And then at the end you have + +93 +00:03:46,000 --> 00:03:47,000 +to return the object of user details. + +94 +00:03:47,000 --> 00:03:50,000 +So how do we do that? And to do that, we have to first + +95 +00:03:50,000 --> 00:03:53,000 +of all create this interface called user repo. + +96 +00:03:53,000 --> 00:03:55,000 +How do we create that? Let's say in the next video. + diff --git a/20 - Spring Security/020 Source-Code.url b/20 - Spring Security/020 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..a76132168cca97ce1d424265ce2db4b5cf566154 --- /dev/null +++ b/20 - Spring Security/020 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.20%20Creating%20A%20Userdetailsservice/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/021 Source-Code.url b/20 - Spring Security/021 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..ebdc3b6031ef5f2bd4298c011a313730e480c4ae --- /dev/null +++ b/20 - Spring Security/021 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.21%20User%20Repository/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/021 User Repository_en.srt b/20 - Spring Security/021 User Repository_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..3190084a813f4290fc22d0973c1933aeee8f8ea2 --- /dev/null +++ b/20 - Spring Security/021 User Repository_en.srt @@ -0,0 +1,560 @@ +1 +00:00:00,000 --> 00:00:02,000 +(upbeat music) + +2 +00:00:02,000 --> 00:00:06,000 +-: Okay, so now it's time to create this interface + +3 +00:00:06,000 --> 00:00:07,000 +or a class called UserRepo. + +4 +00:00:07,000 --> 00:00:10,000 +Now, since we are using JPA, we are going + +5 +00:00:10,000 --> 00:00:12,000 +to create a interface and that's what we are seeing here. + +6 +00:00:12,000 --> 00:00:15,000 +So you can see it says, "Hey, we don't have this UserRepo". + +7 +00:00:15,000 --> 00:00:18,000 +I will say, "okay, create a interface + +8 +00:00:18,000 --> 00:00:20,000 +called UserRepo in the package". + +9 +00:00:20,000 --> 00:00:23,000 +I'll say Dao layer and click on, okay? + +10 +00:00:23,000 --> 00:00:25,000 +And you can see we've got a Dao layer + +11 +00:00:25,000 --> 00:00:26,000 +in which you have as UserRepo. + +12 +00:00:26,000 --> 00:00:30,000 +And this particular UserRepo is going to extend + +13 +00:00:30,000 --> 00:00:33,000 +the JPA repository. + +14 +00:00:33,000 --> 00:00:34,000 +Now since we are connecting with database, + +15 +00:00:34,000 --> 00:00:36,000 +we have to do that. + +16 +00:00:36,000 --> 00:00:39,000 +And then in this JPA repository we have to pass two things. + +17 +00:00:39,000 --> 00:00:41,000 +The first one is the table name, + +18 +00:00:41,000 --> 00:00:43,000 +which we'll connect with the class. + +19 +00:00:43,000 --> 00:00:44,000 +Basically we have to mention the class name, + +20 +00:00:44,000 --> 00:00:47,000 +which we'll connect with the table, the ORM entity. + +21 +00:00:47,000 --> 00:00:49,000 +And then we have to mention the primary key. + +22 +00:00:49,000 --> 00:00:51,000 +The primary key is of type integer, okay? + +23 +00:00:51,000 --> 00:00:54,000 +So basically we need to get this interface called UserRepo, + +24 +00:00:54,000 --> 00:00:56,000 +which extends our JPA repository. + +25 +00:00:56,000 --> 00:01:00,000 +But the problem is we don't have this class yet. + +26 +00:01:00,000 --> 00:01:04,000 +So what I will do is I will create this class called user + +27 +00:01:04,000 --> 00:01:06,000 +and I will do that in my own package. + +28 +00:01:06,000 --> 00:01:09,000 +So I will go back to model and that's what, right? + +29 +00:01:09,000 --> 00:01:12,000 +So you have to remember which file goes where. + +30 +00:01:12,000 --> 00:01:15,000 +So in the model we'll create a user + +31 +00:01:15,000 --> 00:01:17,000 +which will represent a table. + +32 +00:01:17,000 --> 00:01:21,000 +And this particular class will have the same number + +33 +00:01:21,000 --> 00:01:23,000 +of properties as you have in the table, right? + +34 +00:01:23,000 --> 00:01:26,000 +So I will say private int id. + +35 +00:01:26,000 --> 00:01:31,000 +Then I want a private string username + +36 +00:01:31,000 --> 00:01:35,000 +and a private string password. + +37 +00:01:35,000 --> 00:01:37,000 +Now this is the same thing we have in the table. + +38 +00:01:37,000 --> 00:01:38,000 +So if you go back to your table, + +39 +00:01:38,000 --> 00:01:40,000 +you can see we have the same properties, Id, + +40 +00:01:40,000 --> 00:01:42,000 +username and password. + +41 +00:01:42,000 --> 00:01:46,000 +Now the problem here is I don't want to use dataset, + +42 +00:01:46,000 --> 00:01:47,000 +I just want to use Lombok. + +43 +00:01:47,000 --> 00:01:51,000 +And I think we don't have a Lombok here. + +44 +00:01:51,000 --> 00:01:55,000 +So we'll go back to my pom.xml and Lombok, + +45 +00:01:55,000 --> 00:01:56,000 +okay Lombok is there. + +46 +00:01:56,000 --> 00:01:58,000 +So what I can do is I can simply use Lombok here + +47 +00:01:58,000 --> 00:02:02,000 +for the users and I want to close all the extra files. + +48 +00:02:02,000 --> 00:02:04,000 +So I don't want to use controller anymore. + +49 +00:02:04,000 --> 00:02:05,000 +Showing control are done, + +50 +00:02:05,000 --> 00:02:08,000 +student we are not going to use now. + +51 +00:02:08,000 --> 00:02:09,000 +Xml, we are not going to touch. + +52 +00:02:09,000 --> 00:02:11,000 +So these are the files we have + +53 +00:02:11,000 --> 00:02:14,000 +and okay, let's go back to the user. + +54 +00:02:14,000 --> 00:02:17,000 +And I want to have a data, okay? + +55 +00:02:17,000 --> 00:02:21,000 +Now since this is also a entity for the OLM, + +56 +00:02:21,000 --> 00:02:23,000 +so I will say this is entity + +57 +00:02:23,000 --> 00:02:26,000 +and then this will also have a primary key, + +58 +00:02:26,000 --> 00:02:27,000 +which is Id here. + +59 +00:02:27,000 --> 00:02:29,000 +And we have talked about all those things in data JP, right? + +60 +00:02:29,000 --> 00:02:32,000 +So let's not waste our time more on explanation, + +61 +00:02:32,000 --> 00:02:33,000 +but we have done this. + +62 +00:02:33,000 --> 00:02:36,000 +So we got entity, we got Id and okay, + +63 +00:02:36,000 --> 00:02:37,000 +there's one little problem. + +64 +00:02:37,000 --> 00:02:39,000 +The class name I'm using as user, + +65 +00:02:39,000 --> 00:02:42,000 +but the table name is users, okay? + +66 +00:02:42,000 --> 00:02:44,000 +We have a different table name. + +67 +00:02:44,000 --> 00:02:46,000 +So if you want to have a different table name, + +68 +00:02:46,000 --> 00:02:49,000 +we have to basically mention at table, + +69 +00:02:49,000 --> 00:02:51,000 +and you can mention the table name here. + +70 +00:02:51,000 --> 00:02:54,000 +So you can say name is equal to users. + +71 +00:02:54,000 --> 00:02:56,000 +Since we have a different table name, + +72 +00:02:56,000 --> 00:02:57,000 +that's the only reason here. + +73 +00:02:57,000 --> 00:02:59,000 +And that's it, you got user, user table created. + +74 +00:02:59,000 --> 00:03:01,000 +I can go back here + +75 +00:03:01,000 --> 00:03:03,000 +and import the package from our, remember that we have + +76 +00:03:03,000 --> 00:03:07,000 +to import from our own package, not from the spring. + +77 +00:03:07,000 --> 00:03:10,000 +And that's it, we got this UserRepo, okay? + +78 +00:03:10,000 --> 00:03:13,000 +Now there's one little thing we have to do here + +79 +00:03:13,000 --> 00:03:16,000 +because if you say load user by username, + +80 +00:03:16,000 --> 00:03:17,000 +we are passing a username, right? + +81 +00:03:17,000 --> 00:03:21,000 +And that means the repo has to search by username. + +82 +00:03:21,000 --> 00:03:23,000 +And to achieve that, we have to use a method + +83 +00:03:23,000 --> 00:03:26,000 +which will return me a user. + +84 +00:03:26,000 --> 00:03:28,000 +I will say find by username. + +85 +00:03:28,000 --> 00:03:31,000 +And in the bracket I will say string username. + +86 +00:03:31,000 --> 00:03:34,000 +Technically we have one mistake in our code, not the code, + +87 +00:03:34,000 --> 00:03:35,000 +but the design of this database. + +88 +00:03:35,000 --> 00:03:38,000 +Don't you think the username should be unique? + +89 +00:03:38,000 --> 00:03:40,000 +Or maybe you can make your username as primary key, + +90 +00:03:40,000 --> 00:03:42,000 +not the Id, because we want to have it unique. + +91 +00:03:42,000 --> 00:03:45,000 +So when you create it, make sure that you have + +92 +00:03:45,000 --> 00:03:47,000 +not null or unique + +93 +00:03:47,000 --> 00:03:50,000 +or maybe just make your username as your primary key. + +94 +00:03:50,000 --> 00:03:51,000 +That will make much more sense. + +95 +00:03:51,000 --> 00:03:54,000 +But even this works, I will just make sure + +96 +00:03:54,000 --> 00:03:56,000 +that I have a use unique username there. + +97 +00:03:56,000 --> 00:03:59,000 +Okay, so we got this repo ready + +98 +00:03:59,000 --> 00:04:03,000 +and now I can use this repo in this particular method. + +99 +00:04:03,000 --> 00:04:05,000 +How do I do that, so one thing is for sure, + +100 +00:04:05,000 --> 00:04:08,000 +when we use this repo, so if I say repo dot, + +101 +00:04:08,000 --> 00:04:11,000 +you can see we have a method called find by username + +102 +00:04:11,000 --> 00:04:13,000 +in which you can pass the same username. + +103 +00:04:13,000 --> 00:04:18,000 +So now this repo is responsible to connect with database, + +104 +00:04:18,000 --> 00:04:20,000 +get the data, and we are getting this data here. + +105 +00:04:20,000 --> 00:04:23,000 +So when you say repo dot find user by username, + +106 +00:04:23,000 --> 00:04:24,000 +it'll fire the query behind the scene. + +107 +00:04:24,000 --> 00:04:27,000 +And what you will get in return is the type + +108 +00:04:27,000 --> 00:04:28,000 +which you have mentioned here. + +109 +00:04:28,000 --> 00:04:30,000 +The type is user. + +110 +00:04:30,000 --> 00:04:34,000 +So you can simply go back here and say user user equal to. + +111 +00:04:34,000 --> 00:04:36,000 +So what we are getting in return is user, + +112 +00:04:36,000 --> 00:04:39,000 +and this user is from my own package, not spring. + +113 +00:04:39,000 --> 00:04:41,000 +Now, once you got the user, you have + +114 +00:04:41,000 --> 00:04:44,000 +to check if the user is null or not, right? + +115 +00:04:44,000 --> 00:04:47,000 +So we'll check, if the user is null. + +116 +00:04:47,000 --> 00:04:50,000 +In that case, we have to throw an exception. + +117 +00:04:50,000 --> 00:04:52,000 +Remember, you can see we have this particular method, + +118 +00:04:52,000 --> 00:04:56,000 +load user by username, throws a user not found exception. + +119 +00:04:56,000 --> 00:04:58,000 +So I can simply throw the same exception. + +120 +00:04:58,000 --> 00:05:01,000 +So I can say, in fact, I want to print on console as well. + +121 +00:05:01,000 --> 00:05:06,000 +First of all, I will say user 404, user not found. + +122 +00:05:06,000 --> 00:05:07,000 +And I will throw the same exception. + +123 +00:05:07,000 --> 00:05:12,000 +So I will say, throw new user, not found exception. + +124 +00:05:12,000 --> 00:05:15,000 +I can print a message as well. + +125 +00:05:15,000 --> 00:05:19,000 +Maybe the same message if you want, yeah, that's it. + +126 +00:05:19,000 --> 00:05:21,000 +So if the user is not found, + +127 +00:05:21,000 --> 00:05:23,000 +we can simply throw the exception. + +128 +00:05:23,000 --> 00:05:25,000 +But what if you found the user? + +129 +00:05:25,000 --> 00:05:29,000 +In that case, we have to return the user, + +130 +00:05:29,000 --> 00:05:31,000 +but you can't simply return a user. + +131 +00:05:31,000 --> 00:05:35,000 +You can see what you are expecting here is not just a user, + +132 +00:05:35,000 --> 00:05:37,000 +but a user details. + +133 +00:05:37,000 --> 00:05:40,000 +Okay, so we need to make this work. + +134 +00:05:40,000 --> 00:05:41,000 +So instead of return a user, + +135 +00:05:41,000 --> 00:05:43,000 +we have to return a user, user details. + +136 +00:05:43,000 --> 00:05:48,000 +But if you click on user details, it's an interface. + +137 +00:05:48,000 --> 00:05:49,000 +So now you know what's the next video? + +138 +00:05:49,000 --> 00:05:53,000 +The next video is about let's implement this user details + +139 +00:05:53,000 --> 00:05:55,000 +so that we can return it from here. + +140 +00:05:55,000 --> 00:05:57,000 +How to do that, let's see in the next video. + diff --git a/20 - Spring Security/022 Source-Code.url b/20 - Spring Security/022 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..272651fd70b2e4dcd11880b4e60db025a0ee50b5 --- /dev/null +++ b/20 - Spring Security/022 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.22%20Userdetails%20And%20Userprincipal/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/022 UserDetails and UserPrincipal_en.srt b/20 - Spring Security/022 UserDetails and UserPrincipal_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..93a3c803698a24f1dd551255db8254e36dcee0b9 --- /dev/null +++ b/20 - Spring Security/022 UserDetails and UserPrincipal_en.srt @@ -0,0 +1,732 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:07,000 +-: So now let's try to return the UserDetails object. + +3 +00:00:07,000 --> 00:00:10,000 +But again, we have interface, we have to be a class. + +4 +00:00:10,000 --> 00:00:13,000 +So the next step is create a UserDetails. + +5 +00:00:13,000 --> 00:00:16,000 +I will do that in the model package, not in dao. + +6 +00:00:16,000 --> 00:00:19,000 +I'll just go back here and say new class. + +7 +00:00:19,000 --> 00:00:24,000 +And this class nickname as UserDetailsImplementation + +8 +00:00:25,000 --> 00:00:26,000 +because we are implementing + +9 +00:00:26,000 --> 00:00:28,000 +this particular interface. + +10 +00:00:28,000 --> 00:00:33,000 +Or a better name for that is UserPrincipal. + +11 +00:00:33,000 --> 00:00:35,000 +Because in the terms of Spring Security, + +12 +00:00:35,000 --> 00:00:38,000 +when you are representing a current user, + +13 +00:00:38,000 --> 00:00:39,000 +we call them as principals. + +14 +00:00:39,000 --> 00:00:41,000 +So the current user, which we are authenticating, + +15 +00:00:41,000 --> 00:00:42,000 +is a principal, okay? + +16 +00:00:42,000 --> 00:00:45,000 +So UserPrincipal makes much more sense, + +17 +00:00:45,000 --> 00:00:47,000 +but again, you can name your class + +18 +00:00:47,000 --> 00:00:50,000 +as UserDetailsImplementation, your choice. + +19 +00:00:50,000 --> 00:00:55,000 +So this UserPrincipal implements the UserDetails, okay? + +20 +00:00:56,000 --> 00:00:58,000 +And now let's provide the methods. + +21 +00:00:58,000 --> 00:00:59,000 +We have multiple methods there, + +22 +00:00:59,000 --> 00:01:00,000 +so we have to implement them. + +23 +00:01:00,000 --> 00:01:02,000 +So what I will do is I will just go back here + +24 +00:01:02,000 --> 00:01:04,000 +and say implement methods. + +25 +00:01:04,000 --> 00:01:06,000 +And you can see we have multiple methods + +26 +00:01:06,000 --> 00:01:07,000 +and we don't have a choice. + +27 +00:01:07,000 --> 00:01:08,000 +We have to implement everything. + +28 +00:01:08,000 --> 00:01:10,000 +And I will click on OK. + +29 +00:01:10,000 --> 00:01:12,000 +Now all these methods provides different purpose. + +30 +00:01:12,000 --> 00:01:16,000 +Example, let's go from bottom to up. So first is it enabled? + +31 +00:01:16,000 --> 00:01:18,000 +We have to check if the account is enabled. + +32 +00:01:18,000 --> 00:01:20,000 +Then is the credentials not expired? + +33 +00:01:20,000 --> 00:01:22,000 +We have to check for the expiry. + +34 +00:01:22,000 --> 00:01:24,000 +Is the account logged? We have to check for that. + +35 +00:01:24,000 --> 00:01:27,000 +Is the account expired? We have to check for that. + +36 +00:01:27,000 --> 00:01:28,000 +And then we have to check + +37 +00:01:28,000 --> 00:01:30,000 +for the username, password, + +38 +00:01:30,000 --> 00:01:32,000 +and then the authority. + +39 +00:01:32,000 --> 00:01:35,000 +Also, right, because every user will have different access, + +40 +00:01:35,000 --> 00:01:39,000 +admin access, user access, trainer access. + +41 +00:01:39,000 --> 00:01:41,000 +So whatever access you can mention, you can do that here. + +42 +00:01:41,000 --> 00:01:42,000 +Now since in this particular example, + +43 +00:01:42,000 --> 00:01:44,000 +we are not focusing on all these things. + +44 +00:01:44,000 --> 00:01:47,000 +So we'll keep it true. We don't want to check them. + +45 +00:01:47,000 --> 00:01:50,000 +So we are not even checking if it is expired. + +46 +00:01:50,000 --> 00:01:52,000 +Is it enabled, is it logged? + +47 +00:01:52,000 --> 00:01:53,000 +We are saying everything is good, + +48 +00:01:53,000 --> 00:01:55,000 +no need to worry about the expiry and logging. + +49 +00:01:55,000 --> 00:01:57,000 +So we'll keep it as true. + +50 +00:01:57,000 --> 00:01:58,000 +We have to work with this three methods. + +51 +00:01:58,000 --> 00:02:02,000 +First one is getAuthorities, getPassword and getUsername. + +52 +00:02:02,000 --> 00:02:05,000 +Now (indistinct) where you will get it. + +53 +00:02:05,000 --> 00:02:08,000 +See if you go back to your UserDetails service, + +54 +00:02:08,000 --> 00:02:10,000 +we do have this in user object + +55 +00:02:10,000 --> 00:02:12,000 +because user object will have everything. + +56 +00:02:12,000 --> 00:02:14,000 +The username, the password, also authority. + +57 +00:02:14,000 --> 00:02:17,000 +I'm not doing that so I will just hardcode that. + +58 +00:02:17,000 --> 00:02:19,000 +But if in your table we can have one more column + +59 +00:02:19,000 --> 00:02:23,000 +for the authority and you can return that in the user class. + +60 +00:02:23,000 --> 00:02:24,000 +So the user class will have username, password, + +61 +00:02:24,000 --> 00:02:26,000 +and authority as well. + +62 +00:02:26,000 --> 00:02:27,000 +At this point, I don't wanna do that. + +63 +00:02:27,000 --> 00:02:30,000 +Okay, so we'll keep it, we'll hardcode this part, + +64 +00:02:30,000 --> 00:02:32,000 +but the question arise, how will I get the access + +65 +00:02:32,000 --> 00:02:36,000 +of this particular user object in here? + +66 +00:02:36,000 --> 00:02:38,000 +So what we can do is here, + +67 +00:02:38,000 --> 00:02:39,000 +when you are returning the object, + +68 +00:02:39,000 --> 00:02:41,000 +so instead of saying return User, + +69 +00:02:41,000 --> 00:02:45,000 +I can say return new UserPrincipal + +70 +00:02:45,000 --> 00:02:48,000 +because UserPrincipal implements the UserDetails, right? + +71 +00:02:48,000 --> 00:02:50,000 +So you can see there's no error now, + +72 +00:02:50,000 --> 00:02:52,000 +but what if you can actually pass this object here? + +73 +00:02:52,000 --> 00:02:54,000 +Now when you can pass this object here, + +74 +00:02:54,000 --> 00:02:58,000 +we can accept the object here and then we can work with it. + +75 +00:02:58,000 --> 00:03:00,000 +And to accept it, we need a constructor. + +76 +00:03:00,000 --> 00:03:03,000 +So I'll say public UserPrincipal, + +77 +00:03:03,000 --> 00:03:05,000 +which will accept the user objects. + +78 +00:03:05,000 --> 00:03:10,000 +I will say User user. And I will, okay. + +79 +00:03:10,000 --> 00:03:12,000 +So I will also create object here + +80 +00:03:12,000 --> 00:03:15,000 +so that I can get the instance access + +81 +00:03:15,000 --> 00:03:19,000 +and this.user is equal to user. + +82 +00:03:19,000 --> 00:03:22,000 +So whatever object I'm receiving from the UserDetails, + +83 +00:03:22,000 --> 00:03:23,000 +I will just assign this to here + +84 +00:03:23,000 --> 00:03:25,000 +so that I can access in other methods. + +85 +00:03:25,000 --> 00:03:28,000 +Okay, now how do I get the username? It's very simple. + +86 +00:03:28,000 --> 00:03:31,000 +You can simply say user.getUsername. + +87 +00:03:31,000 --> 00:03:32,000 +How will I get the password? + +88 +00:03:32,000 --> 00:03:35,000 +It's very simple. You can say user.getPassword. + +89 +00:03:35,000 --> 00:03:36,000 +How will you get the authority? + +90 +00:03:36,000 --> 00:03:38,000 +We don't have it in the user. + +91 +00:03:38,000 --> 00:03:40,000 +So I can just return a hardcoded value, + +92 +00:03:40,000 --> 00:03:43,000 +and you can see it is expecting you + +93 +00:03:43,000 --> 00:03:45,000 +to return a Collection. + +94 +00:03:45,000 --> 00:03:48,000 +So I will say Collections. + +95 +00:03:48,000 --> 00:03:49,000 +And then we just want + +96 +00:03:49,000 --> 00:03:51,000 +to return one particular object, right? + +97 +00:03:51,000 --> 00:03:52,000 +So I can say singleton. + +98 +00:03:52,000 --> 00:03:53,000 +Now I can specify the type here. + +99 +00:03:53,000 --> 00:03:55,000 +Now you can see this singleton, + +100 +00:03:55,000 --> 00:03:59,000 +which returns one object, is expecting you to have an object + +101 +00:03:59,000 --> 00:04:03,000 +of GrantedAuthority because that's the type we are using. + +102 +00:04:03,000 --> 00:04:06,000 +So collection of type GrantedAuthority. + +103 +00:04:06,000 --> 00:04:07,000 +So we have to get object here. + +104 +00:04:07,000 --> 00:04:10,000 +So it's a new, so the class which implements + +105 +00:04:10,000 --> 00:04:12,000 +because if you say GrantedAuthority, it's interface. + +106 +00:04:12,000 --> 00:04:16,000 +So the class which implements this is simple, + +107 +00:04:18,000 --> 00:04:21,000 +GrantedAuthority, and this you can pass the authority. + +108 +00:04:21,000 --> 00:04:22,000 +So let's say, I will say this is user. + +109 +00:04:22,000 --> 00:04:25,000 +So by default, every user which I have in database + +110 +00:04:25,000 --> 00:04:27,000 +will be by default user. + +111 +00:04:27,000 --> 00:04:28,000 +But again, it will make much more sense when + +112 +00:04:28,000 --> 00:04:31,000 +to have extra column of authorities there. + +113 +00:04:31,000 --> 00:04:34,000 +And that's it, this is your UserPrincipal class, + +114 +00:04:34,000 --> 00:04:38,000 +which I'm using it here, and looks like everything is there. + +115 +00:04:38,000 --> 00:04:40,000 +But the question is, will it work? + +116 +00:04:40,000 --> 00:04:43,000 +Now finally, we are going to run this project, + +117 +00:04:43,000 --> 00:04:46,000 +after four to five videos, we are running this project + +118 +00:04:46,000 --> 00:04:48,000 +and we can get the error, and we'll solve it + +119 +00:04:48,000 --> 00:04:49,000 +if you get the error. + +120 +00:04:49,000 --> 00:04:50,000 +Let's relaunch it. + +121 +00:04:50,000 --> 00:04:53,000 +Okay, there's still some problem here. + +122 +00:04:53,000 --> 00:04:55,000 +Maybe hard reloading is not working properly. + +123 +00:04:55,000 --> 00:04:57,000 +I will just restart the application manually. Let's restart. + +124 +00:04:57,000 --> 00:05:00,000 +Okay, so at this point there's no problem. + +125 +00:05:00,000 --> 00:05:03,000 +I will just open my browser, again in incognito mode. + +126 +00:05:03,000 --> 00:05:06,000 +So I will say new incognito mode. + +127 +00:05:06,000 --> 00:05:08,000 +And I will go to localhost:8080, + +128 +00:05:08,000 --> 00:05:10,000 +asking for the username password, + +129 +00:05:10,000 --> 00:05:12,000 +and we'll enter the username password, + +130 +00:05:12,000 --> 00:05:14,000 +which is mentioned in the database. + +131 +00:05:14,000 --> 00:05:15,000 +So let me go back to database to check. + +132 +00:05:15,000 --> 00:05:17,000 +So we have a username, kiran, + +133 +00:05:17,000 --> 00:05:20,000 +and the password is n@789. + +134 +00:05:20,000 --> 00:05:24,000 +So yeah, so kiran, it's n@789. + +135 +00:05:26,000 --> 00:05:28,000 +First of all, we'll give a wrong password to check. + +136 +00:05:28,000 --> 00:05:30,000 +So I will just enter some random data + +137 +00:05:30,000 --> 00:05:31,000 +and sign in, not working. + +138 +00:05:31,000 --> 00:05:35,000 +That's great. So the negative case is not working. + +139 +00:05:35,000 --> 00:05:37,000 +Let's try with positive values, + +140 +00:05:37,000 --> 00:05:41,000 +n@789, click on sign in, worked. + +141 +00:05:41,000 --> 00:05:41,000 +Okay. + +142 +00:05:41,000 --> 00:05:44,000 +So feeling better, when things works out, it feels better. + +143 +00:05:44,000 --> 00:05:46,000 +Okay, why we are getting error, + +144 +00:05:46,000 --> 00:05:48,000 +because we are not mapping for the homepage, + +145 +00:05:48,000 --> 00:05:49,000 +you can see we have it here. + +146 +00:05:49,000 --> 00:05:51,000 +Refresh, refresh, refresh. It works. + +147 +00:05:51,000 --> 00:05:53,000 +Now I will try a different user. + +148 +00:05:53,000 --> 00:05:56,000 +So again, incognito mode, localhost:8080, + +149 +00:05:56,000 --> 00:05:58,000 +again asking for username, password. + +150 +00:05:58,000 --> 00:06:02,000 +This time we'll go for harsh and h@123. + +151 +00:06:02,000 --> 00:06:07,000 +So I just go back here, I will say harsh, h@123, it worked. + +152 +00:06:09,000 --> 00:06:11,000 +Okay? So yeah. + +153 +00:06:11,000 --> 00:06:14,000 +So now we are actually connecting with database. + +154 +00:06:14,000 --> 00:06:16,000 +And you can see at one point when I was entering + +155 +00:06:16,000 --> 00:06:19,000 +a wrong username, we got the error which is User 404. + +156 +00:06:19,000 --> 00:06:22,000 +Okay, so if you don't have a proper username, + +157 +00:06:22,000 --> 00:06:24,000 +it will give you 404. + +158 +00:06:24,000 --> 00:06:25,000 +And that's the message we have, which you're printing. + +159 +00:06:25,000 --> 00:06:29,000 +And by doing this, we have implemented the entire stuff. + +160 +00:06:29,000 --> 00:06:31,000 +So what I'll do is in the next video, let's try + +161 +00:06:31,000 --> 00:06:34,000 +to go all the steps one by one, + +162 +00:06:34,000 --> 00:06:35,000 +which will make much more sense. + +163 +00:06:35,000 --> 00:06:38,000 +But what we have done in this video is we have created this + +164 +00:06:38,000 --> 00:06:41,000 +particular class called UserPrincipal, + +165 +00:06:41,000 --> 00:06:42,000 +which implements the UserDetails. + +166 +00:06:42,000 --> 00:06:43,000 +Of course, as I mentioned before, + +167 +00:06:43,000 --> 00:06:45,000 +you can have a different name, + +168 +00:06:45,000 --> 00:06:48,000 +but then principal simply means a current user, okay? + +169 +00:06:48,000 --> 00:06:50,000 +So whenever you are authenticating a user, + +170 +00:06:50,000 --> 00:06:52,000 +we call them as principals. + +171 +00:06:52,000 --> 00:06:54,000 +And in this principal, basically we have multiple methods + +172 +00:06:54,000 --> 00:06:57,000 +to work with, like getAuthorities, getUsername, + +173 +00:06:57,000 --> 00:07:00,000 +getPassword, and multiple settings here. + +174 +00:07:00,000 --> 00:07:02,000 +And if you want to have this feature in your project + +175 +00:07:02,000 --> 00:07:04,000 +where maybe after a few days, + +176 +00:07:04,000 --> 00:07:06,000 +you want to expire the credentials, + +177 +00:07:06,000 --> 00:07:08,000 +after a few days, maybe you want to, + +178 +00:07:08,000 --> 00:07:09,000 +if there's an inactive for a long time, + +179 +00:07:09,000 --> 00:07:11,000 +you can lock the account, you can expire the account, + +180 +00:07:11,000 --> 00:07:13,000 +you can do all those settings here. + +181 +00:07:13,000 --> 00:07:14,000 +But at this point I'm not doing it. + +182 +00:07:14,000 --> 00:07:16,000 +So yeah, in the next video, let's try + +183 +00:07:16,000 --> 00:07:17,000 +to understand the entire project. + diff --git a/20 - Spring Security/023 Summary till now_en.srt b/20 - Spring Security/023 Summary till now_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..9b3a0c48ffd90443e5284d98ce7a9eccf2e0d2b3 --- /dev/null +++ b/20 - Spring Security/023 Summary till now_en.srt @@ -0,0 +1,336 @@ +1 +00:00:00,000 --> 00:00:03,000 +(soft bright music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So let's try to understand the project here. + +3 +00:00:05,000 --> 00:00:08,000 +So what I'll do is let's start with the security config. + +4 +00:00:08,000 --> 00:00:09,000 +Now what we are trying to do is + +5 +00:00:09,000 --> 00:00:13,000 +basically we want to verify the user from a database. + +6 +00:00:13,000 --> 00:00:15,000 +The data database which I'm using is Postgres. + +7 +00:00:15,000 --> 00:00:16,000 +So in our date table, + +8 +00:00:16,000 --> 00:00:19,000 +basically we have the ID, username and password. + +9 +00:00:19,000 --> 00:00:20,000 +The table name is users. + +10 +00:00:20,000 --> 00:00:21,000 +And to achieve that, + +11 +00:00:21,000 --> 00:00:24,000 +we have to change our authentication provider + +12 +00:00:24,000 --> 00:00:26,000 +and we want to say that I want to connect with database. + +13 +00:00:26,000 --> 00:00:30,000 +And to do that we have to use DAO authentication provider + +14 +00:00:30,000 --> 00:00:32,000 +because DAO is your database access. + +15 +00:00:32,000 --> 00:00:33,000 +And then to achieve that, + +16 +00:00:33,000 --> 00:00:36,000 +we have to specify the user details service + +17 +00:00:36,000 --> 00:00:38,000 +because if authentication provider wants to work, + +18 +00:00:38,000 --> 00:00:41,000 +it will ask you for the service class. + +19 +00:00:41,000 --> 00:00:42,000 +Now this service is an interface + +20 +00:00:42,000 --> 00:00:43,000 +which is user detailed service. + +21 +00:00:43,000 --> 00:00:45,000 +We have to implement it. + +22 +00:00:45,000 --> 00:00:46,000 +And the way we have implemented + +23 +00:00:46,000 --> 00:00:50,000 +is by creating a class called My User Detailed Service, + +24 +00:00:50,000 --> 00:00:52,000 +which implements the user detailed service + +25 +00:00:52,000 --> 00:00:54,000 +in which you have only one method, + +26 +00:00:54,000 --> 00:00:55,000 +which is load user by username. + +27 +00:00:55,000 --> 00:00:58,000 +But then since the data is coming from database, + +28 +00:00:58,000 --> 00:01:00,000 +we also need a user repo layer. + +29 +00:01:00,000 --> 00:01:03,000 +Now using this repo, which has a method + +30 +00:01:03,000 --> 00:01:07,000 +called find user by username, and we are using JPA, + +31 +00:01:07,000 --> 00:01:08,000 +so we don't have to write their skill queries. + +32 +00:01:08,000 --> 00:01:11,000 +What we are doing is we are firing a method + +33 +00:01:11,000 --> 00:01:12,000 +or we are calling a method + +34 +00:01:12,000 --> 00:01:14,000 +which returns you the object of user. + +35 +00:01:14,000 --> 00:01:15,000 +If the user is there, + +36 +00:01:15,000 --> 00:01:18,000 +then of course you have to return the object of user. + +37 +00:01:18,000 --> 00:01:19,000 +But if the user is not there, + +38 +00:01:19,000 --> 00:01:22,000 +we have to throw an exception, + +39 +00:01:22,000 --> 00:01:24,000 +but (indistinct) how will we return this user? + +40 +00:01:24,000 --> 00:01:27,000 +So we have to basically wrap this user object + +41 +00:01:27,000 --> 00:01:29,000 +into a user principle, + +42 +00:01:29,000 --> 00:01:33,000 +for extra things like expiry and all those stuff. + +43 +00:01:33,000 --> 00:01:34,000 +And to achieve that, + +44 +00:01:34,000 --> 00:01:37,000 +we have to implement the user details interface. + +45 +00:01:37,000 --> 00:01:39,000 +And we have done that in this class, + +46 +00:01:39,000 --> 00:01:41,000 +which is user principle. + +47 +00:01:41,000 --> 00:01:42,000 +Now, user principle is a class + +48 +00:01:42,000 --> 00:01:43,000 +which implements the user details + +49 +00:01:43,000 --> 00:01:45,000 +and principle means current user. + +50 +00:01:45,000 --> 00:01:49,000 +And in this particular class we have multiple methods, + +51 +00:01:49,000 --> 00:01:50,000 +three methods, one for authority, + +52 +00:01:50,000 --> 00:01:52,000 +one for username, one for password. + +53 +00:01:52,000 --> 00:01:54,000 +Apart from this, others are for your expiry, + +54 +00:01:54,000 --> 00:01:57,000 +for your account, and for your credentials. + +55 +00:01:57,000 --> 00:02:00,000 +And then, you know, basically just to give you a summary, + +56 +00:02:00,000 --> 00:02:04,000 +in a bank account, you get a notification + +57 +00:02:04,000 --> 00:02:05,000 +after every one month or three months + +58 +00:02:05,000 --> 00:02:07,000 +that change your password. + +59 +00:02:07,000 --> 00:02:09,000 +That's how they basically set this up. Okay? + +60 +00:02:09,000 --> 00:02:11,000 +So yeah, but then we have not done that. + +61 +00:02:11,000 --> 00:02:14,000 +So we have only focused on this three. + +62 +00:02:14,000 --> 00:02:17,000 +And all this thing, basically you have your table + +63 +00:02:17,000 --> 00:02:19,000 +and your class, which represents that, + +64 +00:02:19,000 --> 00:02:20,000 +which is your user class, + +65 +00:02:20,000 --> 00:02:22,000 +which is an entity which has three columns. + +66 +00:02:22,000 --> 00:02:25,000 +Now depended upon your number of columns in the table, + +67 +00:02:25,000 --> 00:02:25,000 +this will change. + +68 +00:02:25,000 --> 00:02:28,000 +So if you have three columns, you'll have three properties. + +69 +00:02:28,000 --> 00:02:30,000 +If you have five columns, you'll have five properties here. + +70 +00:02:30,000 --> 00:02:32,000 +Which you want to map and that's how it is working. + +71 +00:02:32,000 --> 00:02:33,000 +A few other changes which you have made + +72 +00:02:33,000 --> 00:02:34,000 +in the operation properties, + +73 +00:02:34,000 --> 00:02:36,000 +we have specified the properties + +74 +00:02:36,000 --> 00:02:38,000 +to connect with the Postgres. + +75 +00:02:38,000 --> 00:02:40,000 +If you have a different DBMS, you will change this. + +76 +00:02:40,000 --> 00:02:43,000 +And also in the xml file, we have to mention two things, + +77 +00:02:43,000 --> 00:02:46,000 +the JP repository, the data JPA, + +78 +00:02:46,000 --> 00:02:48,000 +and we have to mention the post quiz. + +79 +00:02:48,000 --> 00:02:50,000 +And by doing this, the security is working. + +80 +00:02:50,000 --> 00:02:54,000 +Your application is secure with the help of database, + +81 +00:02:54,000 --> 00:02:56,000 +but it's not completely secure + +82 +00:02:56,000 --> 00:03:00,000 +because we are still storing data in the plain text. + +83 +00:03:00,000 --> 00:03:01,000 +We have to change this. + +84 +00:03:01,000 --> 00:03:03,000 +Let's do that in the upcoming videos. + diff --git a/20 - Spring Security/024 What is Bcrypt_en.srt b/20 - Spring Security/024 What is Bcrypt_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..73bfd53ee13e1cd2677d90fa47e84dc14cc7b897 --- /dev/null +++ b/20 - Spring Security/024 What is Bcrypt_en.srt @@ -0,0 +1,636 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:04,000 +-: So 'til this point, + +3 +00:00:04,000 --> 00:00:07,000 +we were able to store data in database. + +4 +00:00:07,000 --> 00:00:09,000 +Basically, when I say data, I'm talking about the users + +5 +00:00:09,000 --> 00:00:11,000 +and then we can validate them with the help + +6 +00:00:11,000 --> 00:00:12,000 +of username, password. + +7 +00:00:12,000 --> 00:00:16,000 +And we are securing this with the help of spring security. + +8 +00:00:16,000 --> 00:00:18,000 +And for that we have created multiple files. + +9 +00:00:18,000 --> 00:00:21,000 +If you look at the logic, logic files are very less. + +10 +00:00:21,000 --> 00:00:23,000 +If you compare here, we got hello controller, + +11 +00:00:23,000 --> 00:00:25,000 +student controller, student. + +12 +00:00:25,000 --> 00:00:26,000 +I think that's it. + +13 +00:00:26,000 --> 00:00:28,000 +Those are your actual logic files + +14 +00:00:28,000 --> 00:00:32,000 +and others are your configuration files for Spring Security. + +15 +00:00:32,000 --> 00:00:34,000 +And maybe you're thinking, security files + +16 +00:00:34,000 --> 00:00:36,000 +are more than the actual logic. + +17 +00:00:36,000 --> 00:00:38,000 +See, once you talk about the big application, + +18 +00:00:38,000 --> 00:00:41,000 +you'll be having more files, not just two. + +19 +00:00:41,000 --> 00:00:42,000 +You'll be having multiple there, + +20 +00:00:42,000 --> 00:00:44,000 +maybe 50, 60 doesn't matter. + +21 +00:00:44,000 --> 00:00:46,000 +But the configuration files are limited. + +22 +00:00:46,000 --> 00:00:48,000 +So you can see we have this number of config files. + +23 +00:00:48,000 --> 00:00:51,000 +It might increase and it will in the upcoming videos. + +24 +00:00:51,000 --> 00:00:53,000 +But then this is the basic layout out we have. + +25 +00:00:53,000 --> 00:00:55,000 +And everything is working at this point, right? + +26 +00:00:55,000 --> 00:00:57,000 +We were able to run this application, + +27 +00:00:57,000 --> 00:00:59,000 +we were able to log in as well. + +28 +00:00:59,000 --> 00:01:00,000 +But there's one little problem. + +29 +00:01:00,000 --> 00:01:02,000 +The problem is, not a little problem. + +30 +00:01:02,000 --> 00:01:04,000 +It's actually a big problem. + +31 +00:01:04,000 --> 00:01:05,000 +If we look at our database, + +32 +00:01:05,000 --> 00:01:07,000 +we are storing our password in a plain text, + +33 +00:01:07,000 --> 00:01:09,000 +not a good sight, right? + +34 +00:01:09,000 --> 00:01:13,000 +So not just we are storing data in a plain text, + +35 +00:01:13,000 --> 00:01:16,000 +but also we are transferring this data in a plain text. + +36 +00:01:16,000 --> 00:01:19,000 +So how do you secure this? + +37 +00:01:19,000 --> 00:01:20,000 +So one thing you can do + +38 +00:01:20,000 --> 00:01:22,000 +is you can use something called a cryptography here. + +39 +00:01:22,000 --> 00:01:25,000 +Now in cryptography, basically we have a concept + +40 +00:01:25,000 --> 00:01:29,000 +of cryptography where you encrypt a message + +41 +00:01:29,000 --> 00:01:31,000 +and then on the other hand, you decrypt it. + +42 +00:01:31,000 --> 00:01:34,000 +So basically what I'm saying is when you want + +43 +00:01:34,000 --> 00:01:37,000 +to store a password, you can encrypt a password with a key, + +44 +00:01:37,000 --> 00:01:39,000 +and then when you want to verify the password, + +45 +00:01:39,000 --> 00:01:42,000 +you can decrypt the data and you can verify the password. + +46 +00:01:42,000 --> 00:01:46,000 +Example, let's say we have storing h@123 here. + +47 +00:01:46,000 --> 00:01:49,000 +we can change this value, we can encrypt this value. + +48 +00:01:49,000 --> 00:01:51,000 +And maybe the new output you got is, + +49 +00:01:51,000 --> 00:01:54,000 +let me write the new output here, some random value here. + +50 +00:01:54,000 --> 00:01:56,000 +Let's say this is your output for this encryption. + +51 +00:01:56,000 --> 00:01:58,000 +And then this is what you will be storing + +52 +00:01:58,000 --> 00:02:00,000 +in a password field in the database. + +53 +00:02:00,000 --> 00:02:02,000 +When you want to verify, + +54 +00:02:02,000 --> 00:02:04,000 +of course a user will not enter this value. + +55 +00:02:04,000 --> 00:02:06,000 +User will enter h at rate 123. + +56 +00:02:06,000 --> 00:02:08,000 +Then you have to decrypt this + +57 +00:02:08,000 --> 00:02:10,000 +and you have to get the password + +58 +00:02:10,000 --> 00:02:12,000 +and you can verify that point. + +59 +00:02:12,000 --> 00:02:12,000 +That's one way. + +60 +00:02:12,000 --> 00:02:13,000 +The problem with this is + +61 +00:02:13,000 --> 00:02:15,000 +when you have the encryption technique + +62 +00:02:15,000 --> 00:02:17,000 +for cryptography, you have a key, right? + +63 +00:02:17,000 --> 00:02:19,000 +And some way it is not that safe + +64 +00:02:19,000 --> 00:02:21,000 +because if you get the key, you are gone. + +65 +00:02:21,000 --> 00:02:22,000 +All the passwords are linked. + +66 +00:02:22,000 --> 00:02:24,000 +The other way you can do + +67 +00:02:24,000 --> 00:02:25,000 +is you can have a one way encryption, + +68 +00:02:25,000 --> 00:02:27,000 +which is called hashing basically. + +69 +00:02:27,000 --> 00:02:30,000 +In hashing, you don't decrypt the message. + +70 +00:02:30,000 --> 00:02:34,000 +Example, if this is your, okay, this is not a valid one + +71 +00:02:34,000 --> 00:02:36,000 +because it will beg a decimal. + +72 +00:02:36,000 --> 00:02:39,000 +But for that matter, let's say we have this value. + +73 +00:02:39,000 --> 00:02:42,000 +Now let's say this is your hash, okay? + +74 +00:02:42,000 --> 00:02:44,000 +Now hash is basically one way. + +75 +00:02:44,000 --> 00:02:46,000 +So for every data, you will get only one hash. + +76 +00:02:46,000 --> 00:02:48,000 +There's no collision here. + +77 +00:02:48,000 --> 00:02:50,000 +Now what will happen is you can't get it back. + +78 +00:02:50,000 --> 00:02:53,000 +So you can't simply go from this particular output + +79 +00:02:53,000 --> 00:02:57,000 +to h at 123, because that's one way. + +80 +00:02:57,000 --> 00:02:58,000 +Now, how do we verify this? + +81 +00:02:58,000 --> 00:03:02,000 +So every time a user enters h at rate 123, + +82 +00:03:02,000 --> 00:03:04,000 +then you convert that into a hash again. + +83 +00:03:04,000 --> 00:03:06,000 +And then you compare this two hash. + +84 +00:03:06,000 --> 00:03:07,000 +If they are matching, that's good, right? + +85 +00:03:07,000 --> 00:03:09,000 +Now what are the algorithms we have? + +86 +00:03:09,000 --> 00:03:11,000 +So for hashing, we have different algorithms available. + +87 +00:03:11,000 --> 00:03:15,000 +We got MD5, we got SHA256, + +88 +00:03:15,000 --> 00:03:17,000 +but then we want a more secure way of doing it + +89 +00:03:17,000 --> 00:03:20,000 +and the secure way of doing it, what if you can use SHA, + +90 +00:03:20,000 --> 00:03:23,000 +but not just in one round, in multiple rounds? + +91 +00:03:23,000 --> 00:03:26,000 +So what hashing does is, basically it converts your text + +92 +00:03:26,000 --> 00:03:29,000 +into a numbers using hashing algorithm. + +93 +00:03:29,000 --> 00:03:31,000 +This is one of the algorithm we have. + +94 +00:03:31,000 --> 00:03:33,000 +And then it will do only once. + +95 +00:03:33,000 --> 00:03:35,000 +But what if you can repeat this process multiple times, + +96 +00:03:35,000 --> 00:03:38,000 +1, 2, times, 10 times, 100 times? + +97 +00:03:38,000 --> 00:03:41,000 +And if we can do that, it'll be difficult for anyone + +98 +00:03:41,000 --> 00:03:43,000 +to hack or to get the password. + +99 +00:03:43,000 --> 00:03:45,000 +And to achieve that, we are going + +100 +00:03:45,000 --> 00:03:46,000 +to use some other technique + +101 +00:03:46,000 --> 00:03:50,000 +and we have to use algorithm called bcrypt. + +102 +00:03:50,000 --> 00:03:52,000 +So how do we use bcrypt here? + +103 +00:03:52,000 --> 00:03:54,000 +And you will, it will make much more sense + +104 +00:03:54,000 --> 00:03:56,000 +once we start implementing it. + +105 +00:03:56,000 --> 00:04:00,000 +But bcrypt will help you to generate a password + +106 +00:04:00,000 --> 00:04:02,000 +or it will encode your password. + +107 +00:04:02,000 --> 00:04:04,000 +To show an example, I will open my browser, I'll search + +108 +00:04:04,000 --> 00:04:08,000 +for bcrypt password generator, you can see here. + +109 +00:04:08,000 --> 00:04:09,000 +And I will go through this website, + +110 +00:04:09,000 --> 00:04:12,000 +which I have used multiple times and it is not coming up. + +111 +00:04:12,000 --> 00:04:14,000 +All I can see is ads. + +112 +00:04:14,000 --> 00:04:16,000 +Okay, it's here, second only. + +113 +00:04:16,000 --> 00:04:19,000 +So you got this website, which is browser link. + +114 +00:04:19,000 --> 00:04:21,000 +And here basically you can get your password. + +115 +00:04:21,000 --> 00:04:24,000 +So let's say I want to get the bcrypt password. + +116 +00:04:24,000 --> 00:04:27,000 +So I will enter maybe h at the rate 123, + +117 +00:04:27,000 --> 00:04:29,000 +and I can click on bcrypt. + +118 +00:04:29,000 --> 00:04:32,000 +So you can see this is the encoded format. + +119 +00:04:32,000 --> 00:04:34,000 +Okay, so we are using a password encoder now, + +120 +00:04:34,000 --> 00:04:37,000 +which is bcrypt, and it, this is your encoded format. + +121 +00:04:37,000 --> 00:04:38,000 +And if you look at this format, + +122 +00:04:38,000 --> 00:04:40,000 +of course you have some weird values here, + +123 +00:04:40,000 --> 00:04:41,000 +but if you look at the first one, + +124 +00:04:41,000 --> 00:04:44,000 +so $2A is a version for bcrypt. + +125 +00:04:44,000 --> 00:04:46,000 +So that different versions are available. + +126 +00:04:46,000 --> 00:04:48,000 +We got 2A, 2Y also. + +127 +00:04:48,000 --> 00:04:50,000 +And then after this we have $10, + +128 +00:04:50,000 --> 00:04:52,000 +which is basically your number of rounds. + +129 +00:04:52,000 --> 00:04:54,000 +So you can see, I'm saying 10 rounds here. + +130 +00:04:54,000 --> 00:04:55,000 +So by default it will be 10 rounds, + +131 +00:04:55,000 --> 00:04:57,000 +but you can change the rounds here. + +132 +00:04:57,000 --> 00:04:59,000 +So I can say 12 rounds. + +133 +00:04:59,000 --> 00:05:00,000 +And now when I click on bcrypt, + +134 +00:05:00,000 --> 00:05:02,000 +you will get a different number. + +135 +00:05:02,000 --> 00:05:04,000 +Of course, you will get a different string here. + +136 +00:05:04,000 --> 00:05:07,000 +And we got $12, which is 12 rounds. + +137 +00:05:07,000 --> 00:05:08,000 +Okay. + +138 +00:05:08,000 --> 00:05:10,000 +Now when I say 12 rounds, + +139 +00:05:10,000 --> 00:05:12,000 +it is actually two raised to 12 rounds. + +140 +00:05:12,000 --> 00:05:14,000 +It's not just 12 rounds, okay? + +141 +00:05:14,000 --> 00:05:16,000 +So it's a huge number. + +142 +00:05:16,000 --> 00:05:17,000 +So if you divide in half to leave with 10 + +143 +00:05:17,000 --> 00:05:18,000 +so it is 2 raised to 10, + +144 +00:05:18,000 --> 00:05:20,000 +which is that number of rounds, + +145 +00:05:20,000 --> 00:05:21,000 +it will take 200, this text here + +146 +00:05:21,000 --> 00:05:24,000 +So it is very difficult for anyone to hack it. + +147 +00:05:24,000 --> 00:05:26,000 +So even if they want to do a brute force, + +148 +00:05:26,000 --> 00:05:28,000 +they have to do this multiple times. + +149 +00:05:28,000 --> 00:05:31,000 +And every time you do a hashing, it takes some time, + +150 +00:05:31,000 --> 00:05:33,000 +it takes some computation. + +151 +00:05:33,000 --> 00:05:34,000 +So that means if you do computation, + +152 +00:05:34,000 --> 00:05:37,000 +let's say if you do it only for once, one round, + +153 +00:05:37,000 --> 00:05:38,000 +let's say if it takes one milliseconds, + +154 +00:05:38,000 --> 00:05:41,000 +imagine if you do 4,000 rounds. + +155 +00:05:41,000 --> 00:05:43,000 +That's huge, right? + +156 +00:05:43,000 --> 00:05:46,000 +So it will make sure that it will be less vulnerable + +157 +00:05:46,000 --> 00:05:50,000 +compared to the plain text and even for SHA256. + +158 +00:05:50,000 --> 00:05:52,000 +Now how we are going to use this in our code? + +159 +00:05:52,000 --> 00:05:55,000 +That's what we are going to see on the upcoming videos. + diff --git a/20 - Spring Security/025 Source-Code.url b/20 - Spring Security/025 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..c21ae56a3d5122514a7c0332625d401a76a47897 --- /dev/null +++ b/20 - Spring Security/025 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.25%20User%20Registration/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/025 User Registration_en.srt b/20 - Spring Security/025 User Registration_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..4f90cb8bf5e37cf3dc7e2365409a76a273571d87 --- /dev/null +++ b/20 - Spring Security/025 User Registration_en.srt @@ -0,0 +1,524 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: So now, we have to implement the Password Encoder, + +3 +00:00:06,000 --> 00:00:07,000 +which is Bcrypt. + +4 +00:00:07,000 --> 00:00:09,000 +But before that, we want to also create a user option. + +5 +00:00:09,000 --> 00:00:12,000 +So basically, I want an option where a user can register, + +6 +00:00:12,000 --> 00:00:13,000 +right? + +7 +00:00:13,000 --> 00:00:16,000 +And to do that, what I will do is I will create a controller + +8 +00:00:16,000 --> 00:00:18,000 +where a user can send a request to register. + +9 +00:00:18,000 --> 00:00:21,000 +And I will save this as UserController + +10 +00:00:21,000 --> 00:00:23,000 +in the controller package. + +11 +00:00:23,000 --> 00:00:28,000 +And we need to make sure that this is RestController. + +12 +00:00:28,000 --> 00:00:30,000 +And then, we'll create an endpoint. + +13 +00:00:30,000 --> 00:00:32,000 +So the endpoint here will be "public", + +14 +00:00:32,000 --> 00:00:35,000 +and I want to return the same user, which I'm creating. + +15 +00:00:35,000 --> 00:00:37,000 +So I will say a "User". + +16 +00:00:37,000 --> 00:00:39,000 +But unfortunately, we don't have. + +17 +00:00:39,000 --> 00:00:40,000 +Okay, we do have a user, okay? + +18 +00:00:40,000 --> 00:00:41,000 +So we can say "User". + +19 +00:00:41,000 --> 00:00:44,000 +And then I will say "register" + +20 +00:00:44,000 --> 00:00:46,000 +in which I am going to accept the object of register. + +21 +00:00:46,000 --> 00:00:50,000 +So I will say "RequestBody User_user". + +22 +00:00:52,000 --> 00:00:55,000 +And then, let's return the same user. + +23 +00:00:55,000 --> 00:00:58,000 +Okay, and then this should be mapped with a "Get". + +24 +00:00:58,000 --> 00:00:59,000 +No, not GetMapping, PostMapping, + +25 +00:00:59,000 --> 00:01:01,000 +because we will be sending data. + +26 +00:01:01,000 --> 00:01:05,000 +And here I will say "register". + +27 +00:01:05,000 --> 00:01:07,000 +So now, using this particular URL, + +28 +00:01:07,000 --> 00:01:09,000 +we can send a request for the registration, + +29 +00:01:09,000 --> 00:01:10,000 +and then we are simply returning it. + +30 +00:01:10,000 --> 00:01:12,000 +Of course, I want to store that somewhere, right? + +31 +00:01:12,000 --> 00:01:14,000 +So where do you want to store it? + +32 +00:01:14,000 --> 00:01:15,000 +Of course, in the database. + +33 +00:01:15,000 --> 00:01:20,000 +And to do that, I will create a service class for the user. + +34 +00:01:20,000 --> 00:01:22,000 +And I will say, this is UserService. + +35 +00:01:24,000 --> 00:01:26,000 +I will integrate this with a Service. + +36 +00:01:26,000 --> 00:01:28,000 +And then, here we'll have a method. + +37 +00:01:28,000 --> 00:01:30,000 +So I will say "public_User", + +38 +00:01:30,000 --> 00:01:34,000 +and I will say, "Save", maybe "saveUser". + +39 +00:01:34,000 --> 00:01:36,000 +And here, let's import the package. + +40 +00:01:36,000 --> 00:01:37,000 +Okay, so from the controller, + +41 +00:01:37,000 --> 00:01:39,000 +basically I have to send the user object + +42 +00:01:39,000 --> 00:01:43,000 +and then I can save that object in database, okay? + +43 +00:01:43,000 --> 00:01:45,000 +But then to store that in database, + +44 +00:01:45,000 --> 00:01:47,000 +let me return the same object first. + +45 +00:01:47,000 --> 00:01:48,000 +At this point, we are doing that. + +46 +00:01:48,000 --> 00:01:50,000 +Let's send the same object. + +47 +00:01:50,000 --> 00:01:51,000 +But I want to store it in database, right? + +48 +00:01:51,000 --> 00:01:53,000 +So for database, we already have a Repo. + +49 +00:01:53,000 --> 00:01:56,000 +So if you see we do have a UserRepo, and we can use this. + +50 +00:01:56,000 --> 00:02:00,000 +So I can simply create object of UserRepo. + +51 +00:02:00,000 --> 00:02:01,000 +I will say Repo. + +52 +00:02:01,000 --> 00:02:04,000 +And then we have to AutoWire this. + +53 +00:02:04,000 --> 00:02:05,000 +Okay. + +54 +00:02:05,000 --> 00:02:06,000 +And now with this particular Repo, + +55 +00:02:06,000 --> 00:02:11,000 +I can call it save and I can pass the user. + +56 +00:02:11,000 --> 00:02:13,000 +So basically, whatever data I'm getting from the controller, + +57 +00:02:13,000 --> 00:02:15,000 +I'm just passing it to the repository layer. + +58 +00:02:15,000 --> 00:02:18,000 +And we are calling it the save method. + +59 +00:02:18,000 --> 00:02:19,000 +And of course, the repository layer will + +60 +00:02:19,000 --> 00:02:20,000 +take care of the method execution + +61 +00:02:20,000 --> 00:02:22,000 +because we are using .jpa here, okay? + +62 +00:02:22,000 --> 00:02:23,000 +And I hope this will work. + +63 +00:02:23,000 --> 00:02:25,000 +So basically, I'm passing the object, + +64 +00:02:25,000 --> 00:02:27,000 +I'm just asking service layer to add it. + +65 +00:02:27,000 --> 00:02:29,000 +And we have not implemented Bcrypt yet. + +66 +00:02:29,000 --> 00:02:30,000 +We'll do that. + +67 +00:02:30,000 --> 00:02:31,000 +I'm just going step by step. + +68 +00:02:31,000 --> 00:02:32,000 +I want to register the user first. + +69 +00:02:32,000 --> 00:02:35,000 +And this is looking good. + +70 +00:02:35,000 --> 00:02:38,000 +So let me see if the project is running, it's not. + +71 +00:02:38,000 --> 00:02:39,000 +So I will just run this. + +72 +00:02:39,000 --> 00:02:42,000 +And now, let's head back to our Postman. + +73 +00:02:42,000 --> 00:02:44,000 +The request which we are sending now is for register. + +74 +00:02:44,000 --> 00:02:46,000 +Let's verify the Headers + +75 +00:02:46,000 --> 00:02:48,000 +and in fact, authorization. + +76 +00:02:48,000 --> 00:02:49,000 +This should not be "telusko", + +77 +00:02:49,000 --> 00:02:51,000 +this should be, + +78 +00:02:51,000 --> 00:02:55,000 +let's go with harsh and h@123. + +79 +00:02:55,000 --> 00:02:58,000 +So I'll say this is harsh and h@123. + +80 +00:02:58,000 --> 00:03:01,000 +That's the Username-Password we have in table. + +81 +00:03:01,000 --> 00:03:04,000 +And now, we have to pass data as well. + +82 +00:03:04,000 --> 00:03:05,000 +So basically, we already have some data here. + +83 +00:03:05,000 --> 00:03:06,000 +I don't want to use this. + +84 +00:03:06,000 --> 00:03:08,000 +Let's say, I want to say "id" is 3. + +85 +00:03:08,000 --> 00:03:10,000 +Name, I will register myself + +86 +00:03:10,000 --> 00:03:12,000 +because in database, I don't have my name. + +87 +00:03:12,000 --> 00:03:13,000 +So I'll say Navin. + +88 +00:03:13,000 --> 00:03:15,000 +And the password, so it should be not Name, + +89 +00:03:15,000 --> 00:03:16,000 +it should be "username". + +90 +00:03:16,000 --> 00:03:20,000 +This should match with your class, the user class. + +91 +00:03:20,000 --> 00:03:22,000 +And this is your password. + +92 +00:03:22,000 --> 00:03:26,000 +And the password I want to store is "n@345". + +93 +00:03:28,000 --> 00:03:30,000 +Okay, that's the password I have. + +94 +00:03:30,000 --> 00:03:33,000 +And now, let's click on Send. + +95 +00:03:33,000 --> 00:03:35,000 +Usually, I will give a wrong password just to see. + +96 +00:03:35,000 --> 00:03:38,000 +Let's say I'm passing password four, + +97 +00:03:38,000 --> 00:03:39,000 +fourth authorization, + +98 +00:03:39,000 --> 00:03:41,000 +and I'm creating a new record with this data. + +99 +00:03:41,000 --> 00:03:42,000 +Click on Send, it failed. + +100 +00:03:42,000 --> 00:03:44,000 +You can see we got 401. + +101 +00:03:44,000 --> 00:03:47,000 +Let's pass the right password, which is 123. + +102 +00:03:47,000 --> 00:03:48,000 +Click on Send. + +103 +00:03:48,000 --> 00:03:51,000 +And you can see that this time we got 200. + +104 +00:03:51,000 --> 00:03:53,000 +And we also got the data in back, which is this data, + +105 +00:03:53,000 --> 00:03:54,000 +which is stored in database. + +106 +00:03:54,000 --> 00:03:56,000 +How do I verify this? + +107 +00:03:56,000 --> 00:03:59,000 +Let's go back to our database and say "select * from users" + +108 +00:03:59,000 --> 00:04:00,000 +Run. + +109 +00:04:00,000 --> 00:04:02,000 +Okay, it's not working. + +110 +00:04:02,000 --> 00:04:05,000 +So maybe I have to reload. + +111 +00:04:06,000 --> 00:04:08,000 +Okay, you know what? + +112 +00:04:08,000 --> 00:04:11,000 +I'm not calling anyone "Service service" layer, my bad. + +113 +00:04:11,000 --> 00:04:16,000 +So this should be object of "private UserService service". + +114 +00:04:18,000 --> 00:04:21,000 +And then we have to annotate this with "Autowired". + +115 +00:04:21,000 --> 00:04:23,000 +And now, I can use a service. + +116 +00:04:23,000 --> 00:04:26,000 +In fact, service returns this object, right? + +117 +00:04:26,000 --> 00:04:29,000 +So it's a "service.saveUser" and pass the user object. + +118 +00:04:29,000 --> 00:04:31,000 +This is the steps which was missing. + +119 +00:04:31,000 --> 00:04:32,000 +Let's restart. + +120 +00:04:32,000 --> 00:04:34,000 +I don't want to depend upon the hot reload. + +121 +00:04:34,000 --> 00:04:37,000 +Okay, let's go back to Postman, answer issue again. + +122 +00:04:37,000 --> 00:04:40,000 +Okay, this time also, we got this user here, + +123 +00:04:40,000 --> 00:04:42,000 +but the way you can verify that + +124 +00:04:42,000 --> 00:04:44,000 +is with the help of database and it worked. + +125 +00:04:44,000 --> 00:04:45,000 +You can see we got a new user. + +126 +00:04:45,000 --> 00:04:48,000 +So this is also layer, which we have done. + +127 +00:04:48,000 --> 00:04:49,000 +And now, in the next video, + +128 +00:04:49,000 --> 00:04:52,000 +let's try to implement Bcrypt here. + +129 +00:04:52,000 --> 00:04:54,000 +So basically, I want to create a user + +130 +00:04:54,000 --> 00:04:56,000 +with a Bcrypt implementation. + +131 +00:04:56,000 --> 00:04:58,000 +So see you in the next video. + diff --git a/20 - Spring Security/026 BCrypt Encoding for User Registration_en.srt b/20 - Spring Security/026 BCrypt Encoding for User Registration_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..d5cdb16f484721a2cafb83d2072a33126068ec95 --- /dev/null +++ b/20 - Spring Security/026 BCrypt Encoding for User Registration_en.srt @@ -0,0 +1,568 @@ +1 +00:00:00,000 --> 00:00:03,000 +(logo chimes) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now, let's implement the bcrypt. + +3 +00:00:05,000 --> 00:00:06,000 +But, you know, + +4 +00:00:06,000 --> 00:00:08,000 +even if you implement bcrypt for the registration + +5 +00:00:08,000 --> 00:00:10,000 +and for the searching, see, what happens is, + +6 +00:00:10,000 --> 00:00:12,000 +when you register a new user, + +7 +00:00:12,000 --> 00:00:14,000 +and even if you send request + +8 +00:00:14,000 --> 00:00:15,000 +with this data which you're sending. + +9 +00:00:15,000 --> 00:00:16,000 +Let's say we have a new user. + +10 +00:00:16,000 --> 00:00:18,000 +This time we'll say 4. + +11 +00:00:18,000 --> 00:00:20,000 +And I will say avni. + +12 +00:00:20,000 --> 00:00:24,000 +And this is let's say a@123. + +13 +00:00:24,000 --> 00:00:27,000 +Now, if this is my new user, it should work, right? + +14 +00:00:27,000 --> 00:00:28,000 +But then, the moment you implement bcrypt, + +15 +00:00:28,000 --> 00:00:31,000 +and if you go back to your authorization, + +16 +00:00:31,000 --> 00:00:33,000 +here you can see we are sending a plain password. + +17 +00:00:33,000 --> 00:00:36,000 +And, of course, you have to send plain password. + +18 +00:00:36,000 --> 00:00:38,000 +But in the database as well, we're having a plain password. + +19 +00:00:38,000 --> 00:00:40,000 +And the moment you implement bcrypt, + +20 +00:00:40,000 --> 00:00:41,000 +it'll try to encode that, right? + +21 +00:00:41,000 --> 00:00:43,000 +So, we have to also make sure + +22 +00:00:43,000 --> 00:00:44,000 +that you change one of the field here. + +23 +00:00:44,000 --> 00:00:46,000 +So, let's say I want to change + +24 +00:00:46,000 --> 00:00:47,000 +the navin's password to something else, + +25 +00:00:47,000 --> 00:00:49,000 +some new password here. + +26 +00:00:49,000 --> 00:00:50,000 +And the way you can do that is, + +27 +00:00:50,000 --> 00:00:52,000 +I will file the update query. + +28 +00:00:52,000 --> 00:00:54,000 +So, update users + +29 +00:00:54,000 --> 00:00:59,000 +and I will set the password is equal to + +30 +00:01:00,000 --> 00:01:03,000 +something where the id=3. + +31 +00:01:03,000 --> 00:01:05,000 +So, basically, I'm changing this particular record here, + +32 +00:01:05,000 --> 00:01:06,000 +the third record. + +33 +00:01:06,000 --> 00:01:08,000 +But then, what should be the password? + +34 +00:01:08,000 --> 00:01:11,000 +I want to find the bcrypt of this. How do I get that? + +35 +00:01:11,000 --> 00:01:12,000 +I will just simply copy this, + +36 +00:01:12,000 --> 00:01:16,000 +and, remember, this website where we created a password. + +37 +00:01:16,000 --> 00:01:18,000 +I will just enter that here. + +38 +00:01:18,000 --> 00:01:21,000 +And let me just go with the default one, which is 10. + +39 +00:01:21,000 --> 00:01:22,000 +Or, maybe I will just go with 12 + +40 +00:01:22,000 --> 00:01:24,000 +because I'm going to use that everywhere. + +41 +00:01:24,000 --> 00:01:26,000 +So, I'm going for Rounds 12. + +42 +00:01:26,000 --> 00:01:28,000 +And this is a password which I want to convert. + +43 +00:01:28,000 --> 00:01:29,000 +I will say Bcrypt. + +44 +00:01:29,000 --> 00:01:31,000 +Now, this is your new password, + +45 +00:01:31,000 --> 00:01:34,000 +so I'll just copy this and store that here, + +46 +00:01:34,000 --> 00:01:36,000 +so that at least I can authorize it. + +47 +00:01:36,000 --> 00:01:38,000 +And this is a new password, and the way we can verify... + +48 +00:01:38,000 --> 00:01:40,000 +Okay , we have to execute this as well. + +49 +00:01:40,000 --> 00:01:43,000 +Run. Okay. + +50 +00:01:45,000 --> 00:01:47,000 +Also run, okay, update done. + +51 +00:01:47,000 --> 00:01:49,000 +And now, I will file the query, + +52 +00:01:49,000 --> 00:01:51,000 +and you can see we got the new password there. + +53 +00:01:51,000 --> 00:01:54,000 +Now, once you have this new password, I can go back here, + +54 +00:01:54,000 --> 00:01:57,000 +and let me log in with navin, + +55 +00:01:57,000 --> 00:02:01,000 +and the password is n@345. + +56 +00:02:01,000 --> 00:02:03,000 +Now, of course, from the field you have to pass this. + +57 +00:02:03,000 --> 00:02:06,000 +Then, in between it'll convert that into bcrypt. + +58 +00:02:06,000 --> 00:02:08,000 +But how will you convert that? + +59 +00:02:08,000 --> 00:02:11,000 +So, let me go back to my code and let's do the actual work. + +60 +00:02:11,000 --> 00:02:14,000 +So, when I say register in this service layer, + +61 +00:02:14,000 --> 00:02:16,000 +before you register the user, + +62 +00:02:16,000 --> 00:02:18,000 +this is where we have to change it, right? + +63 +00:02:18,000 --> 00:02:20,000 +So, I want to change the user data. + +64 +00:02:20,000 --> 00:02:22,000 +I have to set the new password. + +65 +00:02:22,000 --> 00:02:24,000 +So, instead of storing the password, which is in plain text, + +66 +00:02:24,000 --> 00:02:26,000 +I want to store that in the bcrypt. + +67 +00:02:26,000 --> 00:02:28,000 +So, I want to convert the password, + +68 +00:02:28,000 --> 00:02:29,000 +which I'm getting from the user, + +69 +00:02:29,000 --> 00:02:31,000 +which is, by default, the plain text, + +70 +00:02:31,000 --> 00:02:33,000 +to our encrypted format. + +71 +00:02:33,000 --> 00:02:34,000 +And to achieve that, + +72 +00:02:34,000 --> 00:02:37,000 +we can use something called a bcrypt encoder. + +73 +00:02:37,000 --> 00:02:40,000 +And to use that, we have to get object of bcrypt. + +74 +00:02:40,000 --> 00:02:43,000 +I'll say private BcryptPasswordEncoder. + +75 +00:02:44,000 --> 00:02:46,000 +And if you're thinking we are using something else, + +76 +00:02:46,000 --> 00:02:49,000 +so we have to add a extra library, no, + +77 +00:02:49,000 --> 00:02:50,000 +it's a part of Spring Security. + +78 +00:02:50,000 --> 00:02:52,000 +So, you can see BcryptPasswordEncoder + +79 +00:02:52,000 --> 00:02:54,000 +is a part of Spring Security. + +80 +00:02:54,000 --> 00:02:56,000 +So, we don't have to add an extra library. + +81 +00:02:56,000 --> 00:03:00,000 +Let's create the variable as encoder, + +82 +00:03:00,000 --> 00:03:02,000 +and then I can create the object like this. + +83 +00:03:02,000 --> 00:03:03,000 +This is one way. + +84 +00:03:03,000 --> 00:03:06,000 +Or, you can basically create a bean + +85 +00:03:06,000 --> 00:03:09,000 +inside your configuration in the security config, + +86 +00:03:09,000 --> 00:03:11,000 +return a bean of PasswordEncoder. + +87 +00:03:11,000 --> 00:03:13,000 +Again, you have a choice. + +88 +00:03:13,000 --> 00:03:15,000 +I'm just going for this variable thing here. + +89 +00:03:15,000 --> 00:03:18,000 +And then, in this, you can pass a strength. + +90 +00:03:18,000 --> 00:03:20,000 +So, how many rounds you want to go with. + +91 +00:03:20,000 --> 00:03:22,000 +So, we have done that 12 in the, + +92 +00:03:22,000 --> 00:03:23,000 +when we are generating your password, + +93 +00:03:23,000 --> 00:03:25,000 +I'll also go for 12 here. + +94 +00:03:25,000 --> 00:03:28,000 +We can also set different versions here if you want. + +95 +00:03:28,000 --> 00:03:30,000 +So, example, if I go back here, + +96 +00:03:30,000 --> 00:03:31,000 +and say Control+Space, + +97 +00:03:31,000 --> 00:03:34,000 +you can see we can specify the BCryptVersion + +98 +00:03:34,000 --> 00:03:35,000 +as $2A, $2B, $2Y. + +99 +00:03:36,000 --> 00:03:37,000 +These are the options you have, + +100 +00:03:37,000 --> 00:03:38,000 +but I don't want to specify that. + +101 +00:03:38,000 --> 00:03:40,000 +Let me go with default one, which is two 12A. + +102 +00:03:40,000 --> 00:03:42,000 +And you can see we got the strength. + +103 +00:03:42,000 --> 00:03:46,000 +And now, with this encoder, so I can say encoder dot, + +104 +00:03:46,000 --> 00:03:49,000 +I can basically encode a password, encode. + +105 +00:03:49,000 --> 00:03:51,000 +And what do you want to encode? + +106 +00:03:51,000 --> 00:03:54,000 +So, whatever user has, so user.getPassword, + +107 +00:03:54,000 --> 00:03:55,000 +that's what I'm encoding. + +108 +00:03:55,000 --> 00:03:57,000 +And then, assign it to back to the password. + +109 +00:03:57,000 --> 00:04:00,000 +And time being, let me also print + +110 +00:04:00,000 --> 00:04:02,000 +the new password which we assigned, + +111 +00:04:02,000 --> 00:04:04,000 +so that we can see in the console what the new password is. + +112 +00:04:04,000 --> 00:04:05,000 +Not a good idea in the production, + +113 +00:04:05,000 --> 00:04:08,000 +but since we're learning, we can see what is happening. + +114 +00:04:08,000 --> 00:04:10,000 +So now, when you register a new user, + +115 +00:04:10,000 --> 00:04:13,000 +it'll be registered with the help of this password, okay? + +116 +00:04:13,000 --> 00:04:15,000 +And, but there's one issue, + +117 +00:04:15,000 --> 00:04:17,000 +we have not implemented for reading, + +118 +00:04:17,000 --> 00:04:21,000 +so we'll still go with the same harsh and harsh@123 + +119 +00:04:21,000 --> 00:04:22,000 +for verification. + +120 +00:04:22,000 --> 00:04:27,000 +So, let's go with harsh and h@123, + +121 +00:04:27,000 --> 00:04:32,000 +because we have not done the bcrypt encoder for retrieving + +122 +00:04:32,000 --> 00:04:34,000 +or to verify the user, okay? + +123 +00:04:34,000 --> 00:04:36,000 +Let's register a new user, which is this, + +124 +00:04:36,000 --> 00:04:39,000 +avni and a@123, that's a password. + +125 +00:04:39,000 --> 00:04:42,000 +I'll click on Send, and you can see it worked. + +126 +00:04:42,000 --> 00:04:44,000 +And this time, you can see we got a different password. + +127 +00:04:44,000 --> 00:04:46,000 +This is the encoded password. + +128 +00:04:46,000 --> 00:04:48,000 +Also, we can verify in the console + +129 +00:04:48,000 --> 00:04:49,000 +because we are printing it. + +130 +00:04:49,000 --> 00:04:51,000 +This is a new password. + +131 +00:04:51,000 --> 00:04:53,000 +Also, we can verify in database. + +132 +00:04:53,000 --> 00:04:55,000 +And I will just hit this particular query here. + +133 +00:04:55,000 --> 00:04:57,000 +So, we've got a new user with this password. + +134 +00:04:57,000 --> 00:04:59,000 +Now, this is the encoded password. + +135 +00:04:59,000 --> 00:05:02,000 +Of course, while sending it, you have sent a@123, + +136 +00:05:02,000 --> 00:05:05,000 +but we got encoded format here. + +137 +00:05:05,000 --> 00:05:08,000 +But now, let's implement this for retrieving. + +138 +00:05:08,000 --> 00:05:09,000 +So, what we have done is, + +139 +00:05:09,000 --> 00:05:13,000 +we used bcrypt for storing up a new user + +140 +00:05:13,000 --> 00:05:16,000 +or grasping a new user, right? + +141 +00:05:16,000 --> 00:05:19,000 +But how do I verify this for the authentication? + +142 +00:05:19,000 --> 00:05:21,000 +Let's try that in the next video. + diff --git a/20 - Spring Security/026 Source-Code.url b/20 - Spring Security/026 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..c1aa8ba1b50400011e9892a5b25d01ab09b6360e --- /dev/null +++ b/20 - Spring Security/026 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.26%20Bcrypt%20Encoding%20For%20User%20Registration/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/027 Setting Password Encoder_en.srt b/20 - Spring Security/027 Setting Password Encoder_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..75e0d789457abe3b101d924176276a46c8ebb9ad --- /dev/null +++ b/20 - Spring Security/027 Setting Password Encoder_en.srt @@ -0,0 +1,312 @@ +1 +00:00:00,000 --> 00:00:03,000 +(peaceful music) + +2 +00:00:03,000 --> 00:00:05,000 +-: And now let's do it for the authentication. + +3 +00:00:05,000 --> 00:00:07,000 +So basically the registration done + +4 +00:00:07,000 --> 00:00:10,000 +and we also got a password in the incorrect format. + +5 +00:00:10,000 --> 00:00:12,000 +And you can see even if someone hacks the database + +6 +00:00:12,000 --> 00:00:14,000 +for this two records, at least, + +7 +00:00:14,000 --> 00:00:15,000 +they can't see the passwords. + +8 +00:00:15,000 --> 00:00:17,000 +Here we can, but that's what we have done before. + +9 +00:00:17,000 --> 00:00:21,000 +But now I want to authenticate right? + +10 +00:00:21,000 --> 00:00:23,000 +Now you'll be thinking we have to get multiple classes, + +11 +00:00:23,000 --> 00:00:25,000 +multiple files, not exactly. + +12 +00:00:25,000 --> 00:00:29,000 +If you go back to your security config, remember this line, + +13 +00:00:29,000 --> 00:00:32,000 +this line is the only thing we have to change. + +14 +00:00:32,000 --> 00:00:34,000 +Everything else remains same. + +15 +00:00:34,000 --> 00:00:35,000 +Let me show you how. + +16 +00:00:35,000 --> 00:00:36,000 +Now when you say set password encode, + +17 +00:00:36,000 --> 00:00:38,000 +remember when we talked about + +18 +00:00:38,000 --> 00:00:39,000 +authentication provider, + +19 +00:00:39,000 --> 00:00:42,000 +which was using a DAO authentication provider, + +20 +00:00:42,000 --> 00:00:44,000 +and we have set the password encoder + +21 +00:00:44,000 --> 00:00:45,000 +as no OP password encoder, + +22 +00:00:45,000 --> 00:00:47,000 +and that's why it was checking with a plain text. + +23 +00:00:47,000 --> 00:00:50,000 +Now let's replace this with our library. + +24 +00:00:50,000 --> 00:00:55,000 +So we'll simply say new Bcrypt password encoder + +25 +00:00:55,000 --> 00:00:58,000 +and just say 12, done. + +26 +00:00:58,000 --> 00:01:00,000 +That's the only change which which have to do + +27 +00:01:00,000 --> 00:01:01,000 +for the authentication + +28 +00:01:01,000 --> 00:01:03,000 +because other things we have already done, + +29 +00:01:03,000 --> 00:01:05,000 +right, now will this work, let's try. + +30 +00:01:05,000 --> 00:01:07,000 +Now, the way I can try + +31 +00:01:07,000 --> 00:01:08,000 +that is let me restart the application first + +32 +00:01:08,000 --> 00:01:13,000 +and let's try with the new account, which is Omni, right? + +33 +00:01:13,000 --> 00:01:15,000 +So let me go back to my postman + +34 +00:01:15,000 --> 00:01:18,000 +and let's hit the query for the hello, right? + +35 +00:01:18,000 --> 00:01:19,000 +So let's try to send hello. + +36 +00:01:19,000 --> 00:01:21,000 +And this time I will set the code, + +37 +00:01:21,000 --> 00:01:23,000 +one, two, three, four, send. + +38 +00:01:23,000 --> 00:01:24,000 +Of course you will get error + +39 +00:01:24,000 --> 00:01:25,000 +because we don't have this user. + +40 +00:01:25,000 --> 00:01:29,000 +And now I will try with something which is in plain text, + +41 +00:01:29,000 --> 00:01:32,000 +which is Hirsch and Hirsch at the rate one, two, three. + +42 +00:01:32,000 --> 00:01:36,000 +So it's a Hirsch and H at the data one, two, three. + +43 +00:01:36,000 --> 00:01:39,000 +Click on send. Even this is not working. + +44 +00:01:39,000 --> 00:01:41,000 +We do have this user, it's not working, + +45 +00:01:41,000 --> 00:01:44,000 +because we are searching for a plain text. + +46 +00:01:44,000 --> 00:01:45,000 +And in the coding we have said + +47 +00:01:45,000 --> 00:01:47,000 +that you have to use a Bcrypt. + +48 +00:01:47,000 --> 00:01:50,000 +And in the database it's not Bcrypt, it's a plain text. + +49 +00:01:50,000 --> 00:01:53,000 +So now I will go with Avni because it's already Bcrypt. + +50 +00:01:53,000 --> 00:01:58,000 +So I will say A at the rate one, two, three, run, it worked. + +51 +00:01:59,000 --> 00:02:00,000 +You can see that? + +52 +00:02:00,000 --> 00:02:02,000 +So this is how basically we can implement + +53 +00:02:02,000 --> 00:02:05,000 +Bcrypt for password encoding. + +54 +00:02:05,000 --> 00:02:07,000 +So you these two users you can delete now. + +55 +00:02:07,000 --> 00:02:09,000 +You can only focus on this two Navin and Avni, + +56 +00:02:09,000 --> 00:02:12,000 +and that's the passwords which are in encrypted format. + +57 +00:02:12,000 --> 00:02:15,000 +So it's so easy to implement a password encoder + +58 +00:02:15,000 --> 00:02:18,000 +to secure your database, not database, + +59 +00:02:18,000 --> 00:02:19,000 +but then the password fields. + +60 +00:02:19,000 --> 00:02:23,000 +So that's it from this video where we talked about Bcrypt. + +61 +00:02:23,000 --> 00:02:24,000 +How do we implement this? + +62 +00:02:24,000 --> 00:02:28,000 +So just to give you a overall picture for registration, + +63 +00:02:28,000 --> 00:02:30,000 +this particular controller has no idea + +64 +00:02:30,000 --> 00:02:31,000 +that we have to do encoding. + +65 +00:02:31,000 --> 00:02:34,000 +The actual encoding is happening in the user service, + +66 +00:02:34,000 --> 00:02:35,000 +which is service layer, + +67 +00:02:35,000 --> 00:02:38,000 +and whatever user you're getting from the controller, + +68 +00:02:38,000 --> 00:02:40,000 +you are basically encoding the password, + +69 +00:02:40,000 --> 00:02:42,000 +then you are saving the user, + +70 +00:02:42,000 --> 00:02:43,000 +and then when you are authenticating, + +71 +00:02:43,000 --> 00:02:47,000 +basically here we have used a password encoder, + +72 +00:02:47,000 --> 00:02:49,000 +set password encoder with Bcrypt, + +73 +00:02:49,000 --> 00:02:50,000 +you have to make sure that you use the same strength, + +74 +00:02:50,000 --> 00:02:51,000 +otherwise it'll not work. + +75 +00:02:51,000 --> 00:02:52,000 +If you specify 10 here, + +76 +00:02:52,000 --> 00:02:56,000 +if you specify 12 in the creation, this will not work. + +77 +00:02:56,000 --> 00:02:59,000 +So you have to make sure that you specify the same strength. + +78 +00:02:59,000 --> 00:03:02,000 +So that's it from this video. See you in the next topic. + diff --git a/20 - Spring Security/027 Source-Code.url b/20 - Spring Security/027 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..c8a872d7d4ebf710adf071712ddfd9a343bd3566 --- /dev/null +++ b/20 - Spring Security/027 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.27%20Setting%20Password%20Encoder/spring-sec-demo \ No newline at end of file diff --git a/20 - Spring Security/external-links.txt b/20 - Spring Security/external-links.txt new file mode 100644 index 0000000000000000000000000000000000000000..be41521b824dd616cff2b9c0a08a48ab8a96a339 --- /dev/null +++ b/20 - Spring Security/external-links.txt @@ -0,0 +1,54 @@ + +003 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.3%20Creating%20A%20Spring%20Security%20Project/spring-sec-demo + +004 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.4%20Default%20Login%20Form/spring-sec-demo + +006 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.6%20Session%20Id/spring-sec-demo + +007 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.7%20Setting%20Username%20And%20Password/spring-sec-demo4 + +010 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.10%20Error%20Without%20Csrf%20Token/spring-sec-demo + +011 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.11%20Sending%20Csrf%20Token/spring-sec-demo + +013 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.13%20Security%20Configuration/spring-sec-demo + +014 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.14%20Disabling%20Csrf%20Token/spring-sec-demo + +015 Source-Code +https://github.com/navinreddy20/spring6-course/tree/86a7c9e9036f7665f50c28c243fed20247f180d7/12%20Spring%20security/12.15%20Without%20Lambda/spring-sec-demo + +017 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.17%20Working%20With%20Multiple%20Users/spring-sec-demo + +018 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.18%20Creating%20User%20Table%20And%20Db%20Properties/spring-sec-demo + +019 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.19%20Authenticationprovider/spring-sec-demo + +020 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.20%20Creating%20A%20Userdetailsservice/spring-sec-demo + +021 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.21%20User%20Repository/spring-sec-demo + +022 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.22%20Userdetails%20And%20Userprincipal/spring-sec-demo + +025 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.25%20User%20Registration/spring-sec-demo + +026 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.26%20Bcrypt%20Encoding%20For%20User%20Registration/spring-sec-demo + +027 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/12.27%20Setting%20Password%20Encoder/spring-sec-demo diff --git a/21 - Securing Job App/001 Plan to secure Job App Project_en.srt b/21 - Securing Job App/001 Plan to secure Job App Project_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..3ccb611ed2c02cd5d1d03fad29b16923475eeefe --- /dev/null +++ b/21 - Securing Job App/001 Plan to secure Job App Project_en.srt @@ -0,0 +1,444 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now it's time to secure our job application. + +3 +00:00:06,000 --> 00:00:08,000 +So to this point, basically we have worked + +4 +00:00:08,000 --> 00:00:09,000 +with a separate project + +5 +00:00:09,000 --> 00:00:10,000 +and you can see we have project name, + +6 +00:00:10,000 --> 00:00:13,000 +which is spring-security-demo, + +7 +00:00:13,000 --> 00:00:16,000 +in which basically we had two controllers, + +8 +00:00:16,000 --> 00:00:17,000 +one for the HelloController, + +9 +00:00:17,000 --> 00:00:21,000 +where we were simply printing returning Hello World, + +10 +00:00:21,000 --> 00:00:24,000 +and then we also have a string controller. + +11 +00:00:24,000 --> 00:00:26,000 +And then we tried to secure this application + +12 +00:00:26,000 --> 00:00:29,000 +with the help of Spring Security. + +13 +00:00:29,000 --> 00:00:31,000 +And for that, we have basically created + +14 +00:00:31,000 --> 00:00:33,000 +multiple classes, multiple interfaces. + +15 +00:00:33,000 --> 00:00:36,000 +Example, we got this interface which use repo, + +16 +00:00:36,000 --> 00:00:37,000 +which connects with the database. + +17 +00:00:37,000 --> 00:00:38,000 +So if you can see, + +18 +00:00:38,000 --> 00:00:40,000 +we also have our database here, which is this. + +19 +00:00:40,000 --> 00:00:43,000 +And in this, what we are doing is basically we have a table + +20 +00:00:43,000 --> 00:00:47,000 +called users in which you got three columns + +21 +00:00:47,000 --> 00:00:49,000 +and then out of this we have four records + +22 +00:00:49,000 --> 00:00:52,000 +in which two records are vacated, + +23 +00:00:52,000 --> 00:00:54,000 +and of course we are going to use this one, + +24 +00:00:54,000 --> 00:00:56,000 +not the other two because they're plain text, + +25 +00:00:56,000 --> 00:00:57,000 +it will not even work. + +26 +00:00:57,000 --> 00:01:00,000 +And now, apart from this, we also have a class + +27 +00:01:00,000 --> 00:01:02,000 +for the user to represent user, + +28 +00:01:02,000 --> 00:01:04,000 +and then we got user principle, + +29 +00:01:04,000 --> 00:01:06,000 +which deals with user details, + +30 +00:01:06,000 --> 00:01:08,000 +and then we got two classes for service. + +31 +00:01:08,000 --> 00:01:12,000 +Actually, we can add this part inside this service itself. + +32 +00:01:12,000 --> 00:01:16,000 +So you don't have to have two different classes. + +33 +00:01:16,000 --> 00:01:19,000 +We can basically copy this class, this method here, + +34 +00:01:19,000 --> 00:01:22,000 +and paste it in the user details, even that will work. + +35 +00:01:22,000 --> 00:01:26,000 +So, but then we have those two classes just to differentiate + +36 +00:01:26,000 --> 00:01:28,000 +what is compulsory and what we are doing + +37 +00:01:28,000 --> 00:01:30,000 +for the user service. + +38 +00:01:30,000 --> 00:01:32,000 +And we have done something, + +39 +00:01:32,000 --> 00:01:33,000 +some changes in the application properties. + +40 +00:01:33,000 --> 00:01:35,000 +We have a database connectivity. + +41 +00:01:35,000 --> 00:01:36,000 +I think it'll be same there. + +42 +00:01:36,000 --> 00:01:38,000 +And then to secure, we have to use + +43 +00:01:38,000 --> 00:01:41,000 +this particular dependency, which is Spring Security. + +44 +00:01:41,000 --> 00:01:43,000 +Now we need to implement this in the job portal. + +45 +00:01:43,000 --> 00:01:46,000 +Now if you go back to job portal, which we have done, + +46 +00:01:46,000 --> 00:01:47,000 +now, if you want to get this project, + +47 +00:01:47,000 --> 00:01:51,000 +this is from our lecture name Search by Keyword + +48 +00:01:51,000 --> 00:01:52,000 +from data JPA. + +49 +00:01:52,000 --> 00:01:54,000 +This is the project which we're going to change, + +50 +00:01:54,000 --> 00:01:57,000 +and this project works, but it is not secure. + +51 +00:01:57,000 --> 00:01:58,000 +Let me show you. + +52 +00:01:58,000 --> 00:01:58,000 +What I will do is, first of all, + +53 +00:01:58,000 --> 00:02:01,000 +I have to make sure that I'm not running the earlier one + +54 +00:02:01,000 --> 00:02:03,000 +because I want to use the same code number. + +55 +00:02:03,000 --> 00:02:06,000 +Let me run this, the project, which we have done way back, + +56 +00:02:06,000 --> 00:02:08,000 +and this should work, so let me run this. + +57 +00:02:08,000 --> 00:02:09,000 +So this job portal, + +58 +00:02:09,000 --> 00:02:12,000 +which is not secure at this point, I'm running it. + +59 +00:02:12,000 --> 00:02:17,000 +Let me go back to Postman and hit the URL for job portal. + +60 +00:02:17,000 --> 00:02:18,000 +So if you go back, + +61 +00:02:18,000 --> 00:02:20,000 +this is the job portal which we had, right, + +62 +00:02:20,000 --> 00:02:22,000 +so let me just pick up this. + +63 +00:02:22,000 --> 00:02:23,000 +So you can see this is a (indistinct), + +64 +00:02:23,000 --> 00:02:26,000 +localhost:8080/jobPost + +65 +00:02:26,000 --> 00:02:29,000 +and I know you might be watching this bit later + +66 +00:02:29,000 --> 00:02:32,000 +so I just want to show you the end points. + +67 +00:02:32,000 --> 00:02:34,000 +So if I go back to Job Rest Controller, + +68 +00:02:34,000 --> 00:02:37,000 +you can see we have a Git mapping for jobPost, + +69 +00:02:37,000 --> 00:02:38,000 +which will return all the posts. + +70 +00:02:38,000 --> 00:02:40,000 +From where we are getting this data, + +71 +00:02:40,000 --> 00:02:43,000 +if you'll see the table, we do have this job post table, + +72 +00:02:43,000 --> 00:02:45,000 +which if I say view all rows, + +73 +00:02:45,000 --> 00:02:47,000 +this is basically a job portal. + +74 +00:02:47,000 --> 00:02:49,000 +We only have two jobs, but that's fine. + +75 +00:02:49,000 --> 00:02:51,000 +We just want to secure whatever resource we have. + +76 +00:02:51,000 --> 00:02:54,000 +I want to secure this and the way you can do that + +77 +00:02:54,000 --> 00:02:55,000 +is by adding Spring Security, + +78 +00:02:55,000 --> 00:02:59,000 +but since we have not added them, let me send the request. + +79 +00:02:59,000 --> 00:03:01,000 +Of course, I have to say no auth + +80 +00:03:01,000 --> 00:03:03,000 +in the headers, nothing specified. + +81 +00:03:03,000 --> 00:03:05,000 +If I go back to no auth, + +82 +00:03:05,000 --> 00:03:07,000 +click on send and look at the response. + +83 +00:03:07,000 --> 00:03:10,000 +We got this data and this is not secured yet. + +84 +00:03:10,000 --> 00:03:12,000 +Now let's secure this. + +85 +00:03:12,000 --> 00:03:13,000 +So what are the things we have to do? + +86 +00:03:13,000 --> 00:03:14,000 +Of course, if you want to secure this, + +87 +00:03:14,000 --> 00:03:17,000 +the first thing you will do in this project + +88 +00:03:17,000 --> 00:03:19,000 +is you will go to your POM file + +89 +00:03:19,000 --> 00:03:21,000 +and add this Spring Security dependency, that's one. + +90 +00:03:21,000 --> 00:03:24,000 +Next, we have to config the security + +91 +00:03:24,000 --> 00:03:26,000 +because now we want to use a table + +92 +00:03:26,000 --> 00:03:29,000 +where you have your users and we already have a table + +93 +00:03:29,000 --> 00:03:31,000 +so we don't have to create one, we just have to map it, + +94 +00:03:31,000 --> 00:03:33,000 +and that's why our job will become easy now, + +95 +00:03:33,000 --> 00:03:34,000 +because we are already done a lot of things + +96 +00:03:34,000 --> 00:03:37,000 +in the earlier project, we can just use them here. + +97 +00:03:37,000 --> 00:03:38,000 +So if we have to do the configuration, + +98 +00:03:38,000 --> 00:03:41,000 +we have to create classes for the users, + +99 +00:03:41,000 --> 00:03:43,000 +class for the user detail service, + +100 +00:03:43,000 --> 00:03:46,000 +and then a repo, user repo interface. + +101 +00:03:46,000 --> 00:03:49,000 +So all these things will be added in this project, + +102 +00:03:49,000 --> 00:03:51,000 +and we'll do step by step. + +103 +00:03:51,000 --> 00:03:52,000 +I'm not going to code everything + +104 +00:03:52,000 --> 00:03:53,000 +because we already done that, + +105 +00:03:53,000 --> 00:03:54,000 +I can just use them in this project. + +106 +00:03:54,000 --> 00:03:56,000 +But we have one more thing + +107 +00:03:56,000 --> 00:03:58,000 +which we have not talked about in the previous project, + +108 +00:03:58,000 --> 00:04:00,000 +and that is your cross origin. + +109 +00:04:00,000 --> 00:04:02,000 +So in the next video, let's try to understand + +110 +00:04:02,000 --> 00:04:04,000 +what is cross origin and then we'll move towards + +111 +00:04:04,000 --> 00:04:05,000 +securing this application. + diff --git a/21 - Securing Job App/001 Source-Code.url b/21 - Securing Job App/001 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..78ebcbcd451f7b169b92483b4ee9ee754c4d82d7 --- /dev/null +++ b/21 - Securing Job App/001 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/13.1%20Plan%20To%20Secure%20Job%20App%20Project/spring-boot-rest \ No newline at end of file diff --git a/21 - Securing Job App/002 Cross Origin_en.srt b/21 - Securing Job App/002 Cross Origin_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..58cc81a274ebb36a380c5f28c77d781578a494aa --- /dev/null +++ b/21 - Securing Job App/002 Cross Origin_en.srt @@ -0,0 +1,236 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:07,000 +-: Now let's talk about this CrossOrigin, also called Cross. + +3 +00:00:07,000 --> 00:00:08,000 +So basically, what is happening here + +4 +00:00:08,000 --> 00:00:09,000 +is we have two different projects. + +5 +00:00:09,000 --> 00:00:12,000 +One for the front end and one for the back end. + +6 +00:00:12,000 --> 00:00:14,000 +We have built our front end using React, + +7 +00:00:14,000 --> 00:00:17,000 +and we have built our back end with Spring. + +8 +00:00:17,000 --> 00:00:19,000 +Now the back end, which is Spring, + +9 +00:00:19,000 --> 00:00:22,000 +we are running on a port number 8080, + +10 +00:00:22,000 --> 00:00:24,000 +which is localhost:8080. + +11 +00:00:24,000 --> 00:00:26,000 +And then the front end, which is your React application, + +12 +00:00:26,000 --> 00:00:28,000 +is running on a different port number, which is 3000. + +13 +00:00:28,000 --> 00:00:32,000 +Now by default, Spring says you can't actually access + +14 +00:00:32,000 --> 00:00:33,000 +a resource from a different origin + +15 +00:00:33,000 --> 00:00:35,000 +because we have a different port number there. + +16 +00:00:35,000 --> 00:00:37,000 +So it'll stop it. And why is important is because + +17 +00:00:37,000 --> 00:00:40,000 +what if someone else is trying to access your resource + +18 +00:00:40,000 --> 00:00:42,000 +from a different origin? + +19 +00:00:42,000 --> 00:00:43,000 +And that's a risky part, right? + +20 +00:00:43,000 --> 00:00:45,000 +So that's not security. + +21 +00:00:45,000 --> 00:00:46,000 +So if you want to make it more secure + +22 +00:00:46,000 --> 00:00:47,000 +by default, Spring says, + +23 +00:00:47,000 --> 00:00:50,000 +hey, you're not allowed to directly access a resource + +24 +00:00:50,000 --> 00:00:52,000 +from the outside origin. + +25 +00:00:52,000 --> 00:00:53,000 +And if you want to allow it, + +26 +00:00:53,000 --> 00:00:55,000 +of course we have to allow it in our application + +27 +00:00:55,000 --> 00:00:57,000 +because we are running on two different ports + +28 +00:00:57,000 --> 00:00:58,000 +or two different origins. + +29 +00:00:58,000 --> 00:01:01,000 +And to do that, we are using CrossOrigin + +30 +00:01:01,000 --> 00:01:03,000 +and we are specifying that from where + +31 +00:01:03,000 --> 00:01:05,000 +you will be getting this request. + +32 +00:01:05,000 --> 00:01:08,000 +So we are getting this request from localhost:3000. + +33 +00:01:08,000 --> 00:01:10,000 +Of course, once you deploy, you will get a different URL. + +34 +00:01:10,000 --> 00:01:12,000 +So you will allow it for that. + +35 +00:01:12,000 --> 00:01:14,000 +So you will say, okay, whenever you get requests from this, + +36 +00:01:14,000 --> 00:01:15,000 +you have to allow it. + +37 +00:01:15,000 --> 00:01:17,000 +And sometime maybe you want your APIs + +38 +00:01:17,000 --> 00:01:18,000 +to be public for the user. + +39 +00:01:18,000 --> 00:01:21,000 +In that case you have to simply say CrossOrigin, + +40 +00:01:21,000 --> 00:01:23,000 +then it will allow for all the request. + +41 +00:01:23,000 --> 00:01:25,000 +Okay, you can do that on each controller + +42 +00:01:25,000 --> 00:01:27,000 +the way I'm doing here. + +43 +00:01:27,000 --> 00:01:30,000 +Or you can do a configuration in the config file + +44 +00:01:30,000 --> 00:01:32,000 +to allow from everywhere. + +45 +00:01:32,000 --> 00:01:35,000 +At this point, I'm not doing it, but that's the idea. + +46 +00:01:35,000 --> 00:01:37,000 +So CrossOrigin simply means that if you are accessing + +47 +00:01:37,000 --> 00:01:39,000 +the data from the outside world + +48 +00:01:39,000 --> 00:01:41,000 +from this particular application, + +49 +00:01:41,000 --> 00:01:42,000 +then it'll by default blocked. + +50 +00:01:42,000 --> 00:01:46,000 +And if you want to allow it, you have to say CrossOrigin. + +51 +00:01:46,000 --> 00:01:49,000 +And also cause is basically one of these issues + +52 +00:01:49,000 --> 00:01:52,000 +in the OS top 10, which we have discussed. + +53 +00:01:52,000 --> 00:01:55,000 +And that's how basically, Spring takes care of it. + +54 +00:01:55,000 --> 00:01:56,000 +But yeah, we have used it by default. + +55 +00:01:56,000 --> 00:01:58,000 +I have not explained to you when we were doing this + +56 +00:01:58,000 --> 00:02:01,000 +or maybe you have done that, but not in terms of security. + +57 +00:02:01,000 --> 00:02:03,000 +But yes, you have to allow it from the outside world. + +58 +00:02:03,000 --> 00:02:05,000 +And if you don't do that, it will not allow. + +59 +00:02:05,000 --> 00:02:07,000 +Okay, so that's your CrossOrigin. + diff --git a/21 - Securing Job App/003 Adding Security Configuration_en.srt b/21 - Securing Job App/003 Adding Security Configuration_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..52f6006045dce494129683f60d8ceafb97b88c8b --- /dev/null +++ b/21 - Securing Job App/003 Adding Security Configuration_en.srt @@ -0,0 +1,336 @@ +1 +00:00:00,000 --> 00:00:03,000 +(pleasant, soft music) + +2 +00:00:03,000 --> 00:00:05,000 +-: And now, once we have talked about CrossOrigin, + +3 +00:00:05,000 --> 00:00:06,000 +it's time to secure it. + +4 +00:00:06,000 --> 00:00:09,000 +Now, in this project, we have less number of files, + +5 +00:00:09,000 --> 00:00:11,000 +but we'll add more for the security, + +6 +00:00:11,000 --> 00:00:13,000 +and we'll take it from our older project. + +7 +00:00:13,000 --> 00:00:14,000 +So, let me go back to my older project, + +8 +00:00:14,000 --> 00:00:16,000 +which is your spring security demo. + +9 +00:00:16,000 --> 00:00:19,000 +The first thing you will do is you will add spring security. + +10 +00:00:19,000 --> 00:00:22,000 +So I will just copy this dependency block here + +11 +00:00:22,000 --> 00:00:25,000 +from the XML file and go back to our project. + +12 +00:00:25,000 --> 00:00:27,000 +Let's open the XML for this particular project, + +13 +00:00:27,000 --> 00:00:30,000 +and now, let's add the dependency. + +14 +00:00:30,000 --> 00:00:32,000 +I will just do it above Lombok, + +15 +00:00:32,000 --> 00:00:35,000 +and now you can see I have added the spring security + +16 +00:00:35,000 --> 00:00:36,000 +dependency of Spring Boot. + +17 +00:00:36,000 --> 00:00:38,000 +Now once you add that in your project, you have + +18 +00:00:38,000 --> 00:00:40,000 +to reload your VIN for the changes + +19 +00:00:40,000 --> 00:00:43,000 +and you can say it says resolving dependency done. + +20 +00:00:43,000 --> 00:00:45,000 +Now, how do I verify if this is secure? + +21 +00:00:45,000 --> 00:00:46,000 +I have to restart the application + +22 +00:00:46,000 --> 00:00:48,000 +because we have changed the palm file. + +23 +00:00:48,000 --> 00:00:51,000 +Okay, so you can see we got this project running. + +24 +00:00:51,000 --> 00:00:53,000 +Let me go back to my postman + +25 +00:00:53,000 --> 00:00:55,000 +and let's try to hit the same U model. + +26 +00:00:55,000 --> 00:00:57,000 +The moment you add spring security, + +27 +00:00:57,000 --> 00:00:59,000 +and when you say send, it'll not work + +28 +00:00:59,000 --> 00:01:03,000 +because now your application is kind of secure. + +29 +00:01:03,000 --> 00:01:06,000 +So if you want to make it work, you have to say basic auth + +30 +00:01:06,000 --> 00:01:08,000 +and you have to specify the user. + +31 +00:01:08,000 --> 00:01:10,000 +Now, this username password will not work. + +32 +00:01:10,000 --> 00:01:12,000 +It's because we have not connected with database yet. + +33 +00:01:12,000 --> 00:01:14,000 +So even if you try with that, this will not work. + +34 +00:01:14,000 --> 00:01:16,000 +You can say we got 4 0 1. + +35 +00:01:16,000 --> 00:01:20,000 +So we have to use a default username, which is user + +36 +00:01:20,000 --> 00:01:21,000 +and the default password, + +37 +00:01:21,000 --> 00:01:24,000 +which you'll be getting in the console. + +38 +00:01:24,000 --> 00:01:26,000 +So if you go back to console here, not this project. + +39 +00:01:26,000 --> 00:01:28,000 +If you go back to console here, + +40 +00:01:28,000 --> 00:01:30,000 +and if you go up, you got a password. + +41 +00:01:30,000 --> 00:01:33,000 +And we have done this before, so we can just copy that + +42 +00:01:33,000 --> 00:01:37,000 +and paste it here and click on send, it worked. + +43 +00:01:37,000 --> 00:01:40,000 +So basically now it's kind of secured. + +44 +00:01:40,000 --> 00:01:41,000 +Now when I say kind of secure, + +45 +00:01:41,000 --> 00:01:43,000 +it's because you are generating a password + +46 +00:01:43,000 --> 00:01:46,000 +in the application, which is same for all the users. + +47 +00:01:46,000 --> 00:01:49,000 +Not exactly security, but yeah, we got something, right? + +48 +00:01:49,000 --> 00:01:50,000 +This is partially secured now. + +49 +00:01:50,000 --> 00:01:52,000 +Next, what's the next step? + +50 +00:01:52,000 --> 00:01:53,000 +We need to copy files. + +51 +00:01:53,000 --> 00:01:56,000 +I have to, I want to change the configuration. + +52 +00:01:56,000 --> 00:01:58,000 +So when you say you want to change the configuration from + +53 +00:01:58,000 --> 00:02:01,000 +your older project, we need to get the config file. + +54 +00:02:01,000 --> 00:02:03,000 +This one, in the config package. + +55 +00:02:03,000 --> 00:02:06,000 +So I'll just copy this, the secure config, + +56 +00:02:06,000 --> 00:02:07,000 +go back to my old project. + +57 +00:02:07,000 --> 00:02:11,000 +We don't have a config folder here in the job application. + +58 +00:02:11,000 --> 00:02:14,000 +So I will create a package for config. + +59 +00:02:14,000 --> 00:02:16,000 +And here, let's paste it. + +60 +00:02:16,000 --> 00:02:18,000 +So this particular file will be pasted, + +61 +00:02:18,000 --> 00:02:20,000 +of course this will not work, + +62 +00:02:20,000 --> 00:02:23,000 +is because we don't have these classes created yet. + +63 +00:02:23,000 --> 00:02:24,000 +But let me just try + +64 +00:02:24,000 --> 00:02:28,000 +to see if all the packages are imported. + +65 +00:02:28,000 --> 00:02:30,000 +We are saying that we want to go with user details, + +66 +00:02:30,000 --> 00:02:32,000 +but it'll pick up the inbuilt, + +67 +00:02:32,000 --> 00:02:35,000 +not the one which we created, but let's try. + +68 +00:02:35,000 --> 00:02:37,000 +Let's try, let's see what error you get. + +69 +00:02:37,000 --> 00:02:39,000 +Even we are using B crypt here. + +70 +00:02:39,000 --> 00:02:42,000 +So let me just try, if this works, + +71 +00:02:42,000 --> 00:02:43,000 +let me just start the application + +72 +00:02:43,000 --> 00:02:47,000 +and go back to our postman just by adding that config file + +73 +00:02:47,000 --> 00:02:50,000 +and I will click on send. + +74 +00:02:52,000 --> 00:02:55,000 +Okay, so it looks like we got error here. + +75 +00:02:55,000 --> 00:03:00,000 +It says the field user detail service is a bean, + +76 +00:03:00,000 --> 00:03:01,000 +but could not be found + +77 +00:03:01,000 --> 00:03:03,000 +because we have not created the class for it. + +78 +00:03:03,000 --> 00:03:06,000 +So let's try to add the classes for user details. + +79 +00:03:06,000 --> 00:03:07,000 +So next video we'll be bit lengthy + +80 +00:03:07,000 --> 00:03:09,000 +because we'll be adding all the files, + +81 +00:03:09,000 --> 00:03:10,000 +but we got the idea, right? + +82 +00:03:10,000 --> 00:03:13,000 +So we cannot simply add a security config, + +83 +00:03:13,000 --> 00:03:15,000 +we have to add other files as well. + +84 +00:03:15,000 --> 00:03:18,000 +So let's do that step by step in the next video. + diff --git a/21 - Securing Job App/003 Source-Code.url b/21 - Securing Job App/003 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..7bc74e449ce4ab12b9d39b54e11732554a378f9f --- /dev/null +++ b/21 - Securing Job App/003 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/13.3%20Adding%20Security%20Configuration/spring-boot-rest \ No newline at end of file diff --git a/21 - Securing Job App/004 Job App is secure now_en.srt b/21 - Securing Job App/004 Job App is secure now_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..9fa2a13c20818eb4eab87e1a1af697a14084a5ea --- /dev/null +++ b/21 - Securing Job App/004 Job App is secure now_en.srt @@ -0,0 +1,652 @@ +1 +00:00:00,000 --> 00:00:03,000 +(calm music) + +2 +00:00:03,000 --> 00:00:05,000 +-: And now let's copy those files. + +3 +00:00:05,000 --> 00:00:06,000 +So I will just go back to my project, + +4 +00:00:06,000 --> 00:00:08,000 +which we have done before for the security. + +5 +00:00:08,000 --> 00:00:09,000 +Now what are the files we need? + +6 +00:00:09,000 --> 00:00:10,000 +So we copied config, + +7 +00:00:10,000 --> 00:00:15,000 +SecurityConfig, but now we need to get this UserDetails. + +8 +00:00:15,000 --> 00:00:17,000 +So I will copy both the service classes, + +9 +00:00:17,000 --> 00:00:19,000 +UserDetails for the configuration + +10 +00:00:19,000 --> 00:00:22,000 +and UserService for your registration. + +11 +00:00:22,000 --> 00:00:24,000 +So we'll just copy these two files. + +12 +00:00:24,000 --> 00:00:25,000 +Go back to my project. + +13 +00:00:25,000 --> 00:00:27,000 +In the service package, + +14 +00:00:27,000 --> 00:00:28,000 +let's paste it, both the files. + +15 +00:00:28,000 --> 00:00:30,000 +And when you open the UserService, + +16 +00:00:30,000 --> 00:00:32,000 +of course it will give you some issues + +17 +00:00:32,000 --> 00:00:35,000 +that you don't have this particular UserRepo. + +18 +00:00:35,000 --> 00:00:36,000 +Let me remove the imports + +19 +00:00:36,000 --> 00:00:38,000 +because we have a different package name. + +20 +00:00:38,000 --> 00:00:40,000 +It'll create issues. And now we need to get the UserRepo. + +21 +00:00:40,000 --> 00:00:42,000 +We need to get the User, and that's it. + +22 +00:00:42,000 --> 00:00:44,000 +So we have to get these two files. + +23 +00:00:44,000 --> 00:00:46,000 +Also, if you open your UserDetailsService, + +24 +00:00:46,000 --> 00:00:50,000 +even this is dependent on the User and the UserRepo. + +25 +00:00:50,000 --> 00:00:52,000 +So we have to remove the invite packages. + +26 +00:00:52,000 --> 00:00:55,000 +So ultimately we are dependent upon two files now, + +27 +00:00:55,000 --> 00:00:58,000 +UserRepo and the User file, or User class. + +28 +00:00:58,000 --> 00:01:01,000 +So let me go back here. So it needs a user. + +29 +00:01:01,000 --> 00:01:04,000 +So we'll copy User, paste it in the model package. + +30 +00:01:04,000 --> 00:01:05,000 +Done. + +31 +00:01:05,000 --> 00:01:07,000 +Okay, so we got the User + +32 +00:01:07,000 --> 00:01:09,000 +and all the imports looks good. + +33 +00:01:09,000 --> 00:01:10,000 +Let me go back to my UserDetailsService. + +34 +00:01:10,000 --> 00:01:12,000 +Now we are able to get the user, + +35 +00:01:12,000 --> 00:01:14,000 +you can see there's no problem. + +36 +00:01:14,000 --> 00:01:15,000 +We can simply import it. + +37 +00:01:15,000 --> 00:01:18,000 +And for the UserService also + +38 +00:01:18,000 --> 00:01:20,000 +the User is resolved. + +39 +00:01:20,000 --> 00:01:22,000 +But then repo. Where's repo? + +40 +00:01:22,000 --> 00:01:24,000 +Let me go back to my older project + +41 +00:01:24,000 --> 00:01:28,000 +and get the UserRepo, which should be in dao. + +42 +00:01:28,000 --> 00:01:30,000 +So you can see we already have a repo layer. + +43 +00:01:30,000 --> 00:01:31,000 +So this time we are not using dao layer. + +44 +00:01:31,000 --> 00:01:33,000 +Here we have a repo layer, but that's fine. + +45 +00:01:33,000 --> 00:01:35,000 +The concept remains same. + +46 +00:01:35,000 --> 00:01:37,000 +Let me remove both this dependency + +47 +00:01:37,000 --> 00:01:41,000 +and say get the User from this + +48 +00:01:41,000 --> 00:01:45,000 +and also the JpaRepository, done. + +49 +00:01:45,000 --> 00:01:47,000 +And you can see there's no problem with repo now. + +50 +00:01:47,000 --> 00:01:49,000 +In the service, it should be happy + +51 +00:01:49,000 --> 00:01:50,000 +the moment you import UserRepo. + +52 +00:01:50,000 --> 00:01:54,000 +So you can see the entire class is ready, the service, + +53 +00:01:54,000 --> 00:01:56,000 +but much detailed service is still not ready + +54 +00:01:56,000 --> 00:02:01,000 +because it is dependent on the UserPrincipal. + +55 +00:02:01,000 --> 00:02:03,000 +Okay, there's still some issue with this, + +56 +00:02:03,000 --> 00:02:06,000 +cannot resolve findByUsername in repo + +57 +00:02:06,000 --> 00:02:08,000 +because we have not imported the package. + +58 +00:02:08,000 --> 00:02:09,000 +Let's import the package for UserRepo, + +59 +00:02:09,000 --> 00:02:12,000 +which we have added, and now it is resolved. + +60 +00:02:12,000 --> 00:02:15,000 +But then not UserPrincipal. So now you know what to do. + +61 +00:02:15,000 --> 00:02:16,000 +Let's copy the UserPrincipal, + +62 +00:02:16,000 --> 00:02:19,000 +which goes into the model package. + +63 +00:02:20,000 --> 00:02:23,000 +Okay, there's no invite import here. + +64 +00:02:23,000 --> 00:02:25,000 +Everything looks cool, + +65 +00:02:25,000 --> 00:02:26,000 +but why is not giving error for user, + +66 +00:02:26,000 --> 00:02:28,000 +okay, it's in the same package. + +67 +00:02:28,000 --> 00:02:29,000 +Okay, no problem here, let me go back + +68 +00:02:29,000 --> 00:02:30,000 +to my UserDetailsService + +69 +00:02:30,000 --> 00:02:34,000 +and let's import this UserPrincipal class basically. + +70 +00:02:34,000 --> 00:02:37,000 +So we are importing that. Okay, what next? + +71 +00:02:37,000 --> 00:02:39,000 +Any problem you see there? + +72 +00:02:39,000 --> 00:02:42,000 +No, looks good. Everything. + +73 +00:02:42,000 --> 00:02:45,000 +Let me just check once any file, which I'm missing here. No. + +74 +00:02:45,000 --> 00:02:46,000 +In the application properties, + +75 +00:02:46,000 --> 00:02:48,000 +we do have database configuration, + +76 +00:02:48,000 --> 00:02:51,000 +but I think we already have that in the project. + +77 +00:02:51,000 --> 00:02:53,000 +So if you go back application properties, + +78 +00:02:53,000 --> 00:02:55,000 +we do have the connectivity with the password, + +79 +00:02:55,000 --> 00:02:57,000 +with the database, and we are using the same database. + +80 +00:02:57,000 --> 00:02:59,000 +There's no problem. + +81 +00:02:59,000 --> 00:03:00,000 +And if you don't have this configuration, + +82 +00:03:00,000 --> 00:03:01,000 +make sure you have it. + +83 +00:03:01,000 --> 00:03:03,000 +And even if you're using some different DBMS, + +84 +00:03:03,000 --> 00:03:06,000 +you have to change these particular details. + +85 +00:03:06,000 --> 00:03:07,000 +Everything else will remain same. + +86 +00:03:07,000 --> 00:03:12,000 +And yeah, I think our project is ready. Let's verify. + +87 +00:03:12,000 --> 00:03:14,000 +How do I verify? Just restart the application. + +88 +00:03:14,000 --> 00:03:16,000 +Restart, taking some time. + +89 +00:03:16,000 --> 00:03:19,000 +And you can say it is not generating a password now + +90 +00:03:19,000 --> 00:03:21,000 +because now we are using database, right? + +91 +00:03:21,000 --> 00:03:22,000 +Now what's the password in database? + +92 +00:03:22,000 --> 00:03:25,000 +So we have avni, where is our table? + +93 +00:03:25,000 --> 00:03:29,000 +So if I say users, view all rows. + +94 +00:03:29,000 --> 00:03:32,000 +So we got avni as a username, if you can see. + +95 +00:03:32,000 --> 00:03:33,000 +Yeah, so we got avni, let's use avni. + +96 +00:03:33,000 --> 00:03:36,000 +And the password is a@123. + +97 +00:03:36,000 --> 00:03:38,000 +But this is Pcrypt, but let's try that. + +98 +00:03:38,000 --> 00:03:39,000 +So let me go back to Postman now + +99 +00:03:39,000 --> 00:03:41,000 +and let's try to hit the URL. + +100 +00:03:41,000 --> 00:03:42,000 +Of course this will not work, + +101 +00:03:42,000 --> 00:03:45,000 +is because we have a wrong username password. + +102 +00:03:45,000 --> 00:03:47,000 +So let me use avni here. + +103 +00:03:47,000 --> 00:03:52,000 +And the password is a@123. + +104 +00:03:52,000 --> 00:03:55,000 +I hope this will work. Let's click on send, and it worked. + +105 +00:03:55,000 --> 00:03:56,000 +Okay, so yeah. + +106 +00:03:56,000 --> 00:03:58,000 +So our application is secured now, + +107 +00:03:58,000 --> 00:04:02,000 +the job portal, right, or the job app, which we have done. + +108 +00:04:02,000 --> 00:04:05,000 +So everything was working but it was not secure. + +109 +00:04:05,000 --> 00:04:07,000 +So we have done this security part. + +110 +00:04:07,000 --> 00:04:08,000 +Now what are the things we have added? + +111 +00:04:08,000 --> 00:04:10,000 +So if you go back to your job application, + +112 +00:04:10,000 --> 00:04:11,000 +we have added certain files. + +113 +00:04:11,000 --> 00:04:14,000 +We got SecurityConfig file in which you are saying + +114 +00:04:14,000 --> 00:04:17,000 +that I want to provide my own AuthenticationProvider. + +115 +00:04:17,000 --> 00:04:20,000 +And in the security filter I'm disabling the csrf, + +116 +00:04:20,000 --> 00:04:22,000 +I'm saying all the requests going + +117 +00:04:22,000 --> 00:04:23,000 +to the application should be authenticated. + +118 +00:04:23,000 --> 00:04:25,000 +I want to have a login page as well, + +119 +00:04:25,000 --> 00:04:26,000 +which works with HTTP headers. + +120 +00:04:26,000 --> 00:04:29,000 +You get a prompt there, not the form. + +121 +00:04:29,000 --> 00:04:33,000 +And then we are saying this session is stateless. + +122 +00:04:33,000 --> 00:04:35,000 +That's the thing we are doing here. + +123 +00:04:35,000 --> 00:04:39,000 +And if you see this particular SecurityConfig, + +124 +00:04:39,000 --> 00:04:41,000 +it is dependent upon the UserDetailsService. + +125 +00:04:41,000 --> 00:04:45,000 +And we have basically implemented this particular interface, + +126 +00:04:45,000 --> 00:04:47,000 +UserDetailsService in which you have this method, + +127 +00:04:47,000 --> 00:04:50,000 +loadUserByUsername, we are passing a username. + +128 +00:04:50,000 --> 00:04:51,000 +It is verifying that. + +129 +00:04:51,000 --> 00:04:54,000 +And to achieve this, it is using a User plus, + +130 +00:04:54,000 --> 00:04:57,000 +and also it returns the object + +131 +00:04:57,000 --> 00:04:58,000 +of UserPrincipals. + +132 +00:04:58,000 --> 00:04:59,000 +And the UserPrincipal, + +133 +00:04:59,000 --> 00:05:02,000 +basically we are implementing UserDetails + +134 +00:05:02,000 --> 00:05:04,000 +and then it has multiple methods in which you're + +135 +00:05:04,000 --> 00:05:05,000 +also specifying the role. + +136 +00:05:05,000 --> 00:05:09,000 +Maybe we have to also update this rules + +137 +00:05:09,000 --> 00:05:12,000 +because when you say you have a job portal, + +138 +00:05:12,000 --> 00:05:14,000 +we don't want everyone to add a job. + +139 +00:05:14,000 --> 00:05:16,000 +Only employers can do that. + +140 +00:05:16,000 --> 00:05:19,000 +And we don't want anyone to delete the jobs. + +141 +00:05:19,000 --> 00:05:21,000 +Only employers can do that. + +142 +00:05:21,000 --> 00:05:24,000 +Or maybe we can have a admin who can add the jobs + +143 +00:05:24,000 --> 00:05:26,000 +and remove the jobs, edit the jobs, + +144 +00:05:26,000 --> 00:05:28,000 +but job searching can be done by all the users. + +145 +00:05:28,000 --> 00:05:30,000 +Now that's something we can add here. Okay? + +146 +00:05:30,000 --> 00:05:33,000 +And yeah, that's it. Those are the things we have done. + +147 +00:05:33,000 --> 00:05:35,000 +We also got UserRepo, which is connecting with this table, + +148 +00:05:35,000 --> 00:05:37,000 +which is User, I mean User plus, + +149 +00:05:37,000 --> 00:05:40,000 +which is representing the users table. + +150 +00:05:41,000 --> 00:05:43,000 +Oh, it was not that long. + +151 +00:05:43,000 --> 00:05:45,000 +We completed the stuff very fast + +152 +00:05:45,000 --> 00:05:49,000 +because we have already done this in the previous section. + +153 +00:05:49,000 --> 00:05:50,000 +Okay? + +154 +00:05:50,000 --> 00:05:54,000 +So yeah, that's how we can secure our job portal. + +155 +00:05:54,000 --> 00:05:56,000 +We can also try this with our React application. + +156 +00:05:56,000 --> 00:05:58,000 +Maybe you can just modify your React application + +157 +00:05:58,000 --> 00:06:02,000 +by adding a username, password, or maybe a login form, + +158 +00:06:02,000 --> 00:06:06,000 +and just hit this URL just by passing the basic auth, right? + +159 +00:06:06,000 --> 00:06:07,000 +And if you do that, it'll work. + +160 +00:06:07,000 --> 00:06:11,000 +So yeah, that's it from this video where we talked about + +161 +00:06:11,000 --> 00:06:14,000 +how do you secure your job portal application, + +162 +00:06:14,000 --> 00:06:16,000 +which is this, or job application. + +163 +00:06:16,000 --> 00:06:17,000 +See you in the next topic. + diff --git a/21 - Securing Job App/004 Source-Code.url b/21 - Securing Job App/004 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..691644a4dc962fd69145a2af4813cdf796a710c1 --- /dev/null +++ b/21 - Securing Job App/004 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/13.4%20Job%20App%20Is%20Secure%20Now/spring-boot-rest \ No newline at end of file diff --git a/21 - Securing Job App/external-links.txt b/21 - Securing Job App/external-links.txt new file mode 100644 index 0000000000000000000000000000000000000000..3f332eb2eaba6999f4365763127bbbed99eb59ee --- /dev/null +++ b/21 - Securing Job App/external-links.txt @@ -0,0 +1,9 @@ + +001 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/13.1%20Plan%20To%20Secure%20Job%20App%20Project/spring-boot-rest + +003 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/13.3%20Adding%20Security%20Configuration/spring-boot-rest + +004 Source-Code +https://github.com/navinreddy20/spring6-course/tree/c6690e4f2c70d8f530d70623f13d14ff0ffd7e7d/12%20Spring%20security/13.4%20Job%20App%20Is%20Secure%20Now/spring-boot-rest diff --git a/22 - JWT (JSON Web Token) and OAuth2/001 Encryption and Decryption_en.srt b/22 - JWT (JSON Web Token) and OAuth2/001 Encryption and Decryption_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..83da3649749d3d699767cdd06e11ba0e7b3f8556 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/001 Encryption and Decryption_en.srt @@ -0,0 +1,808 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, we'll talk about cryptography. + +3 +00:00:06,000 --> 00:00:07,000 +So what happens is, on the internet, + +4 +00:00:07,000 --> 00:00:10,000 +basically you send information between the servers, + +5 +00:00:10,000 --> 00:00:13,000 +or from client to server, or server to client, + +6 +00:00:13,000 --> 00:00:15,000 +and when you say you are + +7 +00:00:15,000 --> 00:00:17,000 +sending this data through the internet, + +8 +00:00:17,000 --> 00:00:19,000 +anyone in between can check this data. + +9 +00:00:19,000 --> 00:00:21,000 +Of course, right, we have something + +10 +00:00:21,000 --> 00:00:23,000 +called man in middle attack. + +11 +00:00:23,000 --> 00:00:26,000 +Basically, they can see your data, they can modify the data. + +12 +00:00:26,000 --> 00:00:29,000 +So let's say, if person A says to person B, + +13 +00:00:29,000 --> 00:00:33,000 +"Hey, let's meet at a cafe at 5:00 PM," + +14 +00:00:33,000 --> 00:00:36,000 +but then when the data is passing, the person C says, + +15 +00:00:36,000 --> 00:00:39,000 +"Okay, I want to make some modification here." + +16 +00:00:39,000 --> 00:00:42,000 +So they go there, they change this data from 5:00 to 6:00, + +17 +00:00:42,000 --> 00:00:44,000 +and they send it to B. + +18 +00:00:44,000 --> 00:00:48,000 +Now, here we are not just doing a passive attack + +19 +00:00:48,000 --> 00:00:49,000 +where you are able to read the data, + +20 +00:00:49,000 --> 00:00:51,000 +you are also doing a active attack. + +21 +00:00:51,000 --> 00:00:54,000 +Now, how do we secure ourself from this attack? + +22 +00:00:54,000 --> 00:00:56,000 +Now of course there have, we have to use some mechanism + +23 +00:00:56,000 --> 00:00:59,000 +where at least other person cannot see your data. + +24 +00:00:59,000 --> 00:01:01,000 +Even if they can see data, + +25 +00:01:01,000 --> 00:01:03,000 +they should not be able to read it. + +26 +00:01:03,000 --> 00:01:05,000 +And second, they should not be able to make the changes. + +27 +00:01:05,000 --> 00:01:06,000 +Even if they make the changes, + +28 +00:01:06,000 --> 00:01:10,000 +B should be able to know that something has changed, + +29 +00:01:10,000 --> 00:01:13,000 +and we can do that with the help of cryptography. + +30 +00:01:13,000 --> 00:01:14,000 +Now, in cryptography we use + +31 +00:01:14,000 --> 00:01:16,000 +a concept of encryption and decryption. + +32 +00:01:16,000 --> 00:01:20,000 +So let's say when A, is sending data to B now, + +33 +00:01:20,000 --> 00:01:22,000 +what you can do is you can do something + +34 +00:01:22,000 --> 00:01:23,000 +called an encryption here. + +35 +00:01:23,000 --> 00:01:25,000 +Now, to do the encryption, + +36 +00:01:25,000 --> 00:01:28,000 +what we basically do is we use a key, + +37 +00:01:28,000 --> 00:01:31,000 +and we change our data in such a way + +38 +00:01:31,000 --> 00:01:33,000 +that no one else can read it. + +39 +00:01:33,000 --> 00:01:35,000 +Of course, when you send the same data to B, + +40 +00:01:35,000 --> 00:01:37,000 +B cannot read that data now. + +41 +00:01:37,000 --> 00:01:40,000 +That's weird, I mean, we wanted B to know the data, right? + +42 +00:01:40,000 --> 00:01:43,000 +But then, what if you can share the key with B? + +43 +00:01:43,000 --> 00:01:46,000 +And now B should be able to decrypt it. + +44 +00:01:46,000 --> 00:01:48,000 +So, encryption simply means converting + +45 +00:01:48,000 --> 00:01:50,000 +your normal text to cipher text. + +46 +00:01:50,000 --> 00:01:52,000 +And decryption simply means + +47 +00:01:52,000 --> 00:01:55,000 +that you're converting your cipher text to a normal text. + +48 +00:01:55,000 --> 00:01:59,000 +So when A is writing the content, that will be your, + +49 +00:01:59,000 --> 00:02:00,000 +and that's your normal text, + +50 +00:02:00,000 --> 00:02:03,000 +but then you have to encrypt it so that no one can read it. + +51 +00:02:03,000 --> 00:02:05,000 +Even A cannot read that now. + +52 +00:02:05,000 --> 00:02:07,000 +Of course, A can read if A can decrypt it, + +53 +00:02:07,000 --> 00:02:11,000 +but now when you send this data to B, now B cannot read it, + +54 +00:02:11,000 --> 00:02:12,000 +so they have to decrypt it, + +55 +00:02:12,000 --> 00:02:14,000 +and they will simply, they can be able to read that. + +56 +00:02:14,000 --> 00:02:17,000 +Now, even if C is able to get the data, + +57 +00:02:17,000 --> 00:02:19,000 +C cannot read that because it is encrypted. + +58 +00:02:19,000 --> 00:02:21,000 +Now, how can B read it? + +59 +00:02:21,000 --> 00:02:25,000 +Because B has a key, C has no idea what that key is. + +60 +00:02:25,000 --> 00:02:27,000 +Now, this key is important here. + +61 +00:02:27,000 --> 00:02:29,000 +Now, this key can be of two different types. + +62 +00:02:29,000 --> 00:02:33,000 +A key can be a symmetric key or it can be a asymmetric key. + +63 +00:02:33,000 --> 00:02:35,000 +So in the symmetric key, what you have is, + +64 +00:02:35,000 --> 00:02:37,000 +you have keys which are same. + +65 +00:02:37,000 --> 00:02:40,000 +So A and B will have the same keys. + +66 +00:02:40,000 --> 00:02:44,000 +And of course, this can be any normal text or normal string, + +67 +00:02:44,000 --> 00:02:46,000 +which you can use as a key, + +68 +00:02:46,000 --> 00:02:47,000 +but they will be having the same key. + +69 +00:02:47,000 --> 00:02:51,000 +So if A is encrypting the data with this key, + +70 +00:02:51,000 --> 00:02:54,000 +the same key has to be there with B as well to decrypt it. + +71 +00:02:54,000 --> 00:02:57,000 +This is called a symmetric cryptography, + +72 +00:02:57,000 --> 00:02:59,000 +or symmetric key cryptography. + +73 +00:02:59,000 --> 00:03:01,000 +The problem with this is, + +74 +00:03:01,000 --> 00:03:04,000 +this key need to be shared before the communication. + +75 +00:03:04,000 --> 00:03:07,000 +Of course, they cannot share this key on the internet now + +76 +00:03:07,000 --> 00:03:09,000 +because if they share this key on the internet, + +77 +00:03:09,000 --> 00:03:12,000 +C can see the key and then C will say, + +78 +00:03:12,000 --> 00:03:13,000 +"Okay, I got the key, now. + +79 +00:03:13,000 --> 00:03:15,000 +Even if you encrypted, I can read that." + +80 +00:03:15,000 --> 00:03:17,000 +So that's one challenge here. + +81 +00:03:17,000 --> 00:03:19,000 +This key need to be shared beforehand. + +82 +00:03:19,000 --> 00:03:22,000 +Maybe they can meet somewhere in person, + +83 +00:03:22,000 --> 00:03:25,000 +share the key and go back and do the communication. + +84 +00:03:25,000 --> 00:03:27,000 +Again, a weird way of sharing a key, + +85 +00:03:27,000 --> 00:03:29,000 +but again, it's very famous, + +86 +00:03:29,000 --> 00:03:31,000 +the symmetric cryptography, it is faster, + +87 +00:03:31,000 --> 00:03:34,000 +and you can also have a large key size, + +88 +00:03:34,000 --> 00:03:36,000 +and the bigger the key size you have, it's more secure. + +89 +00:03:36,000 --> 00:03:39,000 +The challenge is of course the key sharing. + +90 +00:03:39,000 --> 00:03:42,000 +And also, what if you have multiple members in the network? + +91 +00:03:42,000 --> 00:03:43,000 +We have D, E, + +92 +00:03:43,000 --> 00:03:46,000 +and maybe A want to do communications with D now. + +93 +00:03:46,000 --> 00:03:47,000 +So of course, they have to use a different key. + +94 +00:03:47,000 --> 00:03:50,000 +They cannot use the same key which is shared by A and B. + +95 +00:03:50,000 --> 00:03:55,000 +Maybe if you say K1 is the key used by A and B, + +96 +00:03:55,000 --> 00:03:57,000 +now A cannot use the same key with D, + +97 +00:03:57,000 --> 00:04:00,000 +they need to have a different key, let's say K2. + +98 +00:04:00,000 --> 00:04:02,000 +What if D wants to communicate with E? + +99 +00:04:02,000 --> 00:04:05,000 +Of course, they have to have a different key, which is K3. + +100 +00:04:05,000 --> 00:04:08,000 +That's how you basically have multiple keys + +101 +00:04:08,000 --> 00:04:10,000 +in symmetric key cryptography. + +102 +00:04:10,000 --> 00:04:13,000 +And the problem is, managing this key becomes difficult. + +103 +00:04:13,000 --> 00:04:15,000 +What if A want to communicate with everyone? + +104 +00:04:15,000 --> 00:04:17,000 +So A has to manage those number of + +105 +00:04:17,000 --> 00:04:18,000 +those amount of keys with A. + +106 +00:04:18,000 --> 00:04:21,000 +The solution for this is asymmetric cryptography, + +107 +00:04:21,000 --> 00:04:23,000 +where you don't have a symmetric key. + +108 +00:04:23,000 --> 00:04:25,000 +Of course, that's what asymmetric means. + +109 +00:04:25,000 --> 00:04:26,000 +But what you basically have + +110 +00:04:26,000 --> 00:04:29,000 +is you have a concept of public and private key. + +111 +00:04:29,000 --> 00:04:30,000 +Now, how this works. + +112 +00:04:30,000 --> 00:04:33,000 +For the encryption and decryption, you'll be using two keys, + +113 +00:04:33,000 --> 00:04:35,000 +encrypt the public key and a private key. + +114 +00:04:35,000 --> 00:04:36,000 +And how that works is, + +115 +00:04:36,000 --> 00:04:40,000 +if you encrypt a data with a private key, + +116 +00:04:40,000 --> 00:04:42,000 +then you have to decrypt the data with a public key. + +117 +00:04:42,000 --> 00:04:44,000 +And when you encrypt the data with public key, + +118 +00:04:44,000 --> 00:04:46,000 +you have to decrypt the data with private key. + +119 +00:04:46,000 --> 00:04:47,000 +So basically, + +120 +00:04:47,000 --> 00:04:50,000 +you have to use the opposite keys + +121 +00:04:50,000 --> 00:04:50,000 +for encryption, decryption. + +122 +00:04:50,000 --> 00:04:53,000 +You can't use same key for both. + +123 +00:04:53,000 --> 00:04:54,000 +Now, what happens is, + +124 +00:04:54,000 --> 00:04:56,000 +now since private key is a private key, + +125 +00:04:56,000 --> 00:04:58,000 +only the owner knows the private key. + +126 +00:04:58,000 --> 00:04:59,000 +What about the public key? + +127 +00:04:59,000 --> 00:05:03,000 +Public key is known by everyone in the network. Okay? + +128 +00:05:03,000 --> 00:05:04,000 +Now, what's the advantage of this? + +129 +00:05:04,000 --> 00:05:05,000 +So, let's say there's + +130 +00:05:05,000 --> 00:05:08,000 +a public key and private key with the B, + +131 +00:05:08,000 --> 00:05:10,000 +and now A wants to send data. + +132 +00:05:10,000 --> 00:05:12,000 +Now, a question arise, what key A will use. + +133 +00:05:14,000 --> 00:05:17,000 +Can A use a private key of B? Of course not. + +134 +00:05:17,000 --> 00:05:20,000 +Private key is only known by B, + +135 +00:05:20,000 --> 00:05:22,000 +but then public key is known by everyone. + +136 +00:05:22,000 --> 00:05:23,000 +So let's say that we have a central repository + +137 +00:05:23,000 --> 00:05:25,000 +where you have all these keys, + +138 +00:05:25,000 --> 00:05:28,000 +public keys of all the members in the network, right? + +139 +00:05:28,000 --> 00:05:29,000 +So they don't have to basically + +140 +00:05:29,000 --> 00:05:31,000 +ask B to give you a public key. + +141 +00:05:31,000 --> 00:05:33,000 +Everyone knows what is a public key for B. + +142 +00:05:33,000 --> 00:05:35,000 +And since it's a public key, there's no risk here. + +143 +00:05:35,000 --> 00:05:38,000 +Now, when A wants to send this data, + +144 +00:05:38,000 --> 00:05:41,000 +what a will do is A will use B's public key to encrypt. + +145 +00:05:41,000 --> 00:05:43,000 +The beauty is, when B receives the message, + +146 +00:05:43,000 --> 00:05:48,000 +B can decrypt with its own private key. + +147 +00:05:48,000 --> 00:05:51,000 +And only B can do that, because B knows the private key. + +148 +00:05:51,000 --> 00:05:52,000 +What if C comes in between and say, + +149 +00:05:52,000 --> 00:05:55,000 +"Hey, I got the data, now I want to decrypt it." + +150 +00:05:55,000 --> 00:05:57,000 +C can't decrypt that with C's private key. + +151 +00:05:57,000 --> 00:06:01,000 +It can be only decrypt with the B's private key. + +152 +00:06:01,000 --> 00:06:03,000 +Okay, that's how you basically use + +153 +00:06:03,000 --> 00:06:04,000 +asymmetric cryptographic here. + +154 +00:06:04,000 --> 00:06:06,000 +Now, there are different algorithms available. + +155 +00:06:06,000 --> 00:06:08,000 +Now, the question arise, + +156 +00:06:08,000 --> 00:06:09,000 +everyone in the network will have their own + +157 +00:06:09,000 --> 00:06:11,000 +private key and public key, right? + +158 +00:06:11,000 --> 00:06:12,000 +And they know their private key, + +159 +00:06:12,000 --> 00:06:13,000 +and public key is known by everyone. + +160 +00:06:13,000 --> 00:06:16,000 +Now, when D want to send data to E, + +161 +00:06:16,000 --> 00:06:17,000 +basically they will be, + +162 +00:06:17,000 --> 00:06:19,000 +so let's say D want to send data to E. + +163 +00:06:19,000 --> 00:06:21,000 +So D will use E's public key to encrypt + +164 +00:06:21,000 --> 00:06:24,000 +and E will use E's private key to decrypt. + +165 +00:06:24,000 --> 00:06:27,000 +So that's basically the asymmetric box. + +166 +00:06:27,000 --> 00:06:29,000 +There are different algorithms available for symmetric key, + +167 +00:06:29,000 --> 00:06:32,000 +or we have algorithm like AES or DES. + +168 +00:06:32,000 --> 00:06:34,000 +There are multiple algorithms available. + +169 +00:06:34,000 --> 00:06:37,000 +Every algorithm have their own strength and weaknesses. + +170 +00:06:37,000 --> 00:06:41,000 +Same goes for asymmetric key, where you have RSA, ECC. + +171 +00:06:41,000 --> 00:06:44,000 +So we have multiple algorithms available to use. + +172 +00:06:44,000 --> 00:06:46,000 +Now, depending upon different situation, + +173 +00:06:46,000 --> 00:06:47,000 +we can use different algorithms, + +174 +00:06:47,000 --> 00:06:50,000 +and some are faster, some are more secure. + +175 +00:06:50,000 --> 00:06:54,000 +Some depending upon that, you can use any algorithm there. + +176 +00:06:54,000 --> 00:06:55,000 +But now question arise. + +177 +00:06:55,000 --> 00:06:59,000 +Let's say A is sending the data to B + +178 +00:06:59,000 --> 00:07:01,000 +and say, "Hey, I want to meet at 5:00 PM," + +179 +00:07:01,000 --> 00:07:04,000 +and maybe there's nothing secret here, okay? + +180 +00:07:04,000 --> 00:07:06,000 +So now let's say A want to send a message to B by saying, + +181 +00:07:06,000 --> 00:07:09,000 +"Hey, let's meet at 5:00 PM," + +182 +00:07:09,000 --> 00:07:11,000 +and the message is going on the network. + +183 +00:07:11,000 --> 00:07:12,000 +Now, C comes in between and say, + +184 +00:07:12,000 --> 00:07:14,000 +"Hey, let me just hack that packet, + +185 +00:07:14,000 --> 00:07:17,000 +and now let me send a new packet." + +186 +00:07:17,000 --> 00:07:20,000 +And C says, "Let's meet at 6:00 PM." + +187 +00:07:20,000 --> 00:07:23,000 +Now, how C will encrypt it? + +188 +00:07:23,000 --> 00:07:25,000 +So what C will do is, so C will use B's public key, + +189 +00:07:25,000 --> 00:07:29,000 +which A also did, and send that message to B, + +190 +00:07:29,000 --> 00:07:32,000 +and B says, okay, I got a message and it is encrypted. + +191 +00:07:32,000 --> 00:07:34,000 +Let me use my private key to decrypt. Decryption done. + +192 +00:07:34,000 --> 00:07:36,000 +So that means I have to meet at 6:00 PM. + +193 +00:07:36,000 --> 00:07:39,000 +Okay, so when you are using this cryptography, + +194 +00:07:39,000 --> 00:07:41,000 +you are basically encrypting data and decrypting it, + +195 +00:07:41,000 --> 00:07:43,000 +but there's no way to prove + +196 +00:07:43,000 --> 00:07:45,000 +that the sender is the actual sender. + +197 +00:07:45,000 --> 00:07:49,000 +So, maybe B is thinking the message is sent by A, + +198 +00:07:49,000 --> 00:07:51,000 +but it is actually sent by C. + +199 +00:07:51,000 --> 00:07:54,000 +How do you maintain that identity here? + +200 +00:07:54,000 --> 00:07:56,000 +Now, that's where you can use something + +201 +00:07:56,000 --> 00:07:58,000 +called a digital signature. + +202 +00:07:58,000 --> 00:08:00,000 +And let's talk about data signature in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/002 Digital Signature_en.srt b/22 - JWT (JSON Web Token) and OAuth2/002 Digital Signature_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..4b57d9b38636c2a2d9a29ffc68c04107f26c7292 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/002 Digital Signature_en.srt @@ -0,0 +1,384 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle bright music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now we know about encryption/decryption, + +3 +00:00:05,000 --> 00:00:07,000 +but then there was no way to prove that the person + +4 +00:00:07,000 --> 00:00:10,000 +who is sending the message is actually that person. + +5 +00:00:10,000 --> 00:00:11,000 +Now the way you can do that + +6 +00:00:11,000 --> 00:00:13,000 +is with the help of digital signature. + +7 +00:00:13,000 --> 00:00:15,000 +Now what we do here is + +8 +00:00:15,000 --> 00:00:17,000 +what if we don't want to secure the information, + +9 +00:00:17,000 --> 00:00:21,000 +but we want to prove that we are the real person? + +10 +00:00:21,000 --> 00:00:23,000 +Now in that case, what A will do is, + +11 +00:00:23,000 --> 00:00:26,000 +A will not encrypt the message with B's public key, + +12 +00:00:26,000 --> 00:00:28,000 +which he was doing before. + +13 +00:00:28,000 --> 00:00:31,000 +Now what A will do is A will encrypt a message + +14 +00:00:31,000 --> 00:00:35,000 +with its own private key. + +15 +00:00:35,000 --> 00:00:37,000 +Remember that we are not using B's public key here. + +16 +00:00:37,000 --> 00:00:39,000 +We're using A's private key. + +17 +00:00:39,000 --> 00:00:42,000 +Now after encryption, the packet goes to B. + +18 +00:00:42,000 --> 00:00:43,000 +Now how B can decrypt it? + +19 +00:00:43,000 --> 00:00:46,000 +Of course, can I use a B's private key? + +20 +00:00:46,000 --> 00:00:49,000 +No, because it is encrypted by A's private key. + +21 +00:00:49,000 --> 00:00:51,000 +And the only way you can decrypt that + +22 +00:00:51,000 --> 00:00:53,000 +is with the help of A's public key. + +23 +00:00:53,000 --> 00:00:55,000 +If the decryption is possible, + +24 +00:00:55,000 --> 00:00:59,000 +that means that there's a proof that only A has sent it. + +25 +00:00:59,000 --> 00:01:00,000 +What if, let's try to attack here? + +26 +00:01:00,000 --> 00:01:03,000 +What if C says, "I want to send this data." + +27 +00:01:03,000 --> 00:01:05,000 +So when the message is going from A to B, + +28 +00:01:05,000 --> 00:01:08,000 +C comes in between say, "Hey, let me hack this message." + +29 +00:01:08,000 --> 00:01:10,000 +And message is gone. + +30 +00:01:10,000 --> 00:01:12,000 +Now C is sending a new message + +31 +00:01:12,000 --> 00:01:14,000 +and which key C is going to use? + +32 +00:01:14,000 --> 00:01:17,000 +C is going to use C's private key. + +33 +00:01:17,000 --> 00:01:19,000 +And now when it goes to B, + +34 +00:01:19,000 --> 00:01:22,000 +B is not able to decrypt that with A's public key. + +35 +00:01:22,000 --> 00:01:24,000 +It is only possible with the help of C's public key. + +36 +00:01:24,000 --> 00:01:27,000 +And now B knows something is wrong. + +37 +00:01:27,000 --> 00:01:28,000 +It's not coming from A. + +38 +00:01:28,000 --> 00:01:31,000 +And that's how you prove that the data is not coming from A. + +39 +00:01:31,000 --> 00:01:33,000 +It came from someone else. + +40 +00:01:33,000 --> 00:01:35,000 +But what if it was possible to decrypt that + +41 +00:01:35,000 --> 00:01:36,000 +with A's public key? + +42 +00:01:36,000 --> 00:01:38,000 +That means we have a proof that A has sent it. + +43 +00:01:38,000 --> 00:01:42,000 +Now a cannot say later that, "Hey, I have not sent that." + +44 +00:01:42,000 --> 00:01:43,000 +It's a proof, right? + +45 +00:01:43,000 --> 00:01:45,000 +A has encrypted that with A's private key + +46 +00:01:45,000 --> 00:01:49,000 +and only A knows its own private key. + +47 +00:01:49,000 --> 00:01:50,000 +And that's important. + +48 +00:01:50,000 --> 00:01:52,000 +This is called digital signature. + +49 +00:01:52,000 --> 00:01:53,000 +But there's one problem here. + +50 +00:01:53,000 --> 00:01:55,000 +We don't have security. + +51 +00:01:55,000 --> 00:01:58,000 +We do have a proof that who has sent it, + +52 +00:01:58,000 --> 00:01:59,000 +but anyone can read data. + +53 +00:01:59,000 --> 00:02:02,000 +You know, C can also read that data. + +54 +00:02:02,000 --> 00:02:03,000 +Because when C gets this packet, + +55 +00:02:03,000 --> 00:02:06,000 +which says, "Let's meet at 5:00 PM," + +56 +00:02:06,000 --> 00:02:08,000 +C is able to decrypt it + +57 +00:02:08,000 --> 00:02:11,000 +because A has signed it with a private key, + +58 +00:02:11,000 --> 00:02:15,000 +and to decrypt it, C can use A's public key. + +59 +00:02:15,000 --> 00:02:16,000 +You're hacked. + +60 +00:02:16,000 --> 00:02:18,000 +So how do we secure it? + +61 +00:02:18,000 --> 00:02:20,000 +So that's why we have to do double encryption. + +62 +00:02:20,000 --> 00:02:21,000 +I mean think about this. + +63 +00:02:21,000 --> 00:02:24,000 +What if you are basically first, + +64 +00:02:24,000 --> 00:02:26,000 +so let's say A want to send message to B. + +65 +00:02:26,000 --> 00:02:30,000 +First, A will use B's public key to encrypt. + +66 +00:02:30,000 --> 00:02:31,000 +So first layer done. + +67 +00:02:31,000 --> 00:02:34,000 +Then the same packet will be encrypted again + +68 +00:02:34,000 --> 00:02:36,000 +with A's private key. + +69 +00:02:36,000 --> 00:02:38,000 +So double encryption. + +70 +00:02:38,000 --> 00:02:39,000 +Now when the packet goes to B, + +71 +00:02:39,000 --> 00:02:42,000 +B says, "Okay, it is encrypted with A's private key. + +72 +00:02:42,000 --> 00:02:45,000 +Let me decrypt it with A's public key." + +73 +00:02:45,000 --> 00:02:45,000 +Decryption done. + +74 +00:02:45,000 --> 00:02:48,000 +That means there's a proof that A has sent it, + +75 +00:02:48,000 --> 00:02:51,000 +and then again the package is still encrypted. + +76 +00:02:51,000 --> 00:02:54,000 +So again, it will do decryption again with B's private key + +77 +00:02:55,000 --> 00:02:57,000 +because no one knows the B's private key. + +78 +00:02:57,000 --> 00:02:59,000 +And now, B got the message + +79 +00:02:59,000 --> 00:03:03,000 +and we have achieved security as well as identity. + +80 +00:03:03,000 --> 00:03:05,000 +But what if, let's try to attack here? + +81 +00:03:05,000 --> 00:03:06,000 +So let's say C comes in between + +82 +00:03:06,000 --> 00:03:08,000 +and say, "Hey, I want to attack now." + +83 +00:03:08,000 --> 00:03:09,000 +C receives a package + +84 +00:03:09,000 --> 00:03:13,000 +and says, "Okay, it is encrypted with A's private key. + +85 +00:03:13,000 --> 00:03:15,000 +Let me decrypt it with A's public key." + +86 +00:03:15,000 --> 00:03:16,000 +Decryption done. + +87 +00:03:16,000 --> 00:03:19,000 +But now it is still encrypted with B's public key. + +88 +00:03:19,000 --> 00:03:20,000 +Can C decrypt it? + +89 +00:03:20,000 --> 00:03:24,000 +No, because C has no idea what's B's private key + +90 +00:03:24,000 --> 00:03:26,000 +and you're safe, right? + +91 +00:03:26,000 --> 00:03:28,000 +So this is how basically you achieve signature + +92 +00:03:28,000 --> 00:03:29,000 +with encryption. + +93 +00:03:29,000 --> 00:03:31,000 +And this will be used in the upcoming session, + +94 +00:03:31,000 --> 00:03:34,000 +so I hope you understood what is cryptography, + +95 +00:03:34,000 --> 00:03:38,000 +what is encryption/decryption, and what is signature. + +96 +00:03:38,000 --> 00:03:39,000 +See you in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/003 Why JWT_en.srt b/22 - JWT (JSON Web Token) and OAuth2/003 Why JWT_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..ddf77287b6ddbd0ad045354802692064e11131d5 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/003 Why JWT_en.srt @@ -0,0 +1,792 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, we'll talk about JWT, + +3 +00:00:06,000 --> 00:00:08,000 +which stands for JSON Web Tokens. + +4 +00:00:08,000 --> 00:00:10,000 +Now, before we go there, let's take an example. + +5 +00:00:10,000 --> 00:00:13,000 +So let's say every day when I go to my office, + +6 +00:00:13,000 --> 00:00:14,000 +so we have a cafe, + +7 +00:00:14,000 --> 00:00:16,000 +which is just between my home and my office. + +8 +00:00:16,000 --> 00:00:18,000 +I go there to have a coffee, + +9 +00:00:18,000 --> 00:00:20,000 +and then I go to my office. + +10 +00:00:20,000 --> 00:00:21,000 +Of course, for every coffee, + +11 +00:00:21,000 --> 00:00:22,000 +I have to pay some amount. + +12 +00:00:22,000 --> 00:00:25,000 +Let's say I'm paying $1 per coffee every day. + +13 +00:00:25,000 --> 00:00:27,000 +And then, I'm doing this every day, right? + +14 +00:00:27,000 --> 00:00:30,000 +Now, what if I can talk to them by saying, + +15 +00:00:30,000 --> 00:00:32,000 +"Hey, you know, I come there every day. + +16 +00:00:32,000 --> 00:00:35,000 +Let me take a membership of a month." + +17 +00:00:35,000 --> 00:00:36,000 +So maybe I can get a discount. + +18 +00:00:36,000 --> 00:00:38,000 +I mean, I'm going there every day. + +19 +00:00:38,000 --> 00:00:40,000 +So what if I can get, let's say in $20, + +20 +00:00:40,000 --> 00:00:42,000 +I can get for the entire month. + +21 +00:00:42,000 --> 00:00:44,000 +And now the person knows me. + +22 +00:00:44,000 --> 00:00:46,000 +So basically every day, we see each other. + +23 +00:00:46,000 --> 00:00:47,000 +So now he knows who I am. + +24 +00:00:47,000 --> 00:00:52,000 +And then he says, "Okay, now we got $20 from you. + +25 +00:00:52,000 --> 00:00:54,000 +You can come for the entire month and have a coffee." + +26 +00:00:54,000 --> 00:00:58,000 +Now, I went there for first day, second day, third day, + +27 +00:00:58,000 --> 00:00:59,000 +and then everything was working. + +28 +00:00:59,000 --> 00:01:01,000 +So he know my face. + +29 +00:01:01,000 --> 00:01:04,000 +And the problem is, what if the person changes? + +30 +00:01:04,000 --> 00:01:06,000 +Now, of course, we don't want that scenario, right? + +31 +00:01:06,000 --> 00:01:08,000 +Where I go to a cafe after one week + +32 +00:01:08,000 --> 00:01:09,000 +and I see another person, + +33 +00:01:09,000 --> 00:01:11,000 +and then this person says, "Who are you?" + +34 +00:01:11,000 --> 00:01:12,000 +We don't want that. + +35 +00:01:12,000 --> 00:01:14,000 +So maybe there should be some entry in a book + +36 +00:01:14,000 --> 00:01:17,000 +where they can make an entry by saying: + +37 +00:01:17,000 --> 00:01:19,000 +This person has taken a subscription of one month, + +38 +00:01:19,000 --> 00:01:22,000 +and every time this person comes, we can give them a coffee. + +39 +00:01:22,000 --> 00:01:25,000 +And now let's say we have an entry there. + +40 +00:01:25,000 --> 00:01:27,000 +So I can go there, I can tell, + +41 +00:01:27,000 --> 00:01:29,000 +"Hey, there should be my entry. + +42 +00:01:29,000 --> 00:01:32,000 +And for the entry, I also got id. So it's my ID is 102. + +43 +00:01:32,000 --> 00:01:34,000 +And every time I go there, I can say, + +44 +00:01:34,000 --> 00:01:36,000 +this is my id, which is 102. + +45 +00:01:36,000 --> 00:01:39,000 +And they can refer in that book. Yeah. + +46 +00:01:39,000 --> 00:01:40,000 +So we have an id, which is 102, + +47 +00:01:40,000 --> 00:01:43,000 +which says Navin Reddy subscription for one month. + +48 +00:01:43,000 --> 00:01:46,000 +That's great. So now I can get my coffee. + +49 +00:01:46,000 --> 00:01:49,000 +So this, this model works better, right? + +50 +00:01:49,000 --> 00:01:50,000 +Where I can get ID as well, + +51 +00:01:50,000 --> 00:01:52,000 +not just person knows me with my face. + +52 +00:01:52,000 --> 00:01:54,000 +Now there's one little problem. + +53 +00:01:54,000 --> 00:01:57,000 +So let's say with this cafe has multiple branches, okay? + +54 +00:01:57,000 --> 00:01:59,000 +So it is there in my area, + +55 +00:01:59,000 --> 00:02:01,000 +but let's say I go to go for some training + +56 +00:02:01,000 --> 00:02:02,000 +in different cities. + +57 +00:02:02,000 --> 00:02:06,000 +I go to let's say Delhi or Bangalore or Mumbai, + +58 +00:02:06,000 --> 00:02:08,000 +and maybe they have the branches. + +59 +00:02:08,000 --> 00:02:10,000 +Of course they have scaled out well + +60 +00:02:10,000 --> 00:02:12,000 +and now they have branches in different cities. + +61 +00:02:12,000 --> 00:02:15,000 +So let's say if one day I'm not in my hometown, + +62 +00:02:15,000 --> 00:02:17,000 +I'm going to a different city + +63 +00:02:17,000 --> 00:02:20,000 +and I see a cafe there, the question is, + +64 +00:02:20,000 --> 00:02:22,000 +will I get the free coffee? + +65 +00:02:22,000 --> 00:02:24,000 +I mean, not free, but I got the membership, right? + +66 +00:02:24,000 --> 00:02:26,000 +I should not, I don't have to pay for it. + +67 +00:02:26,000 --> 00:02:28,000 +But then when I go there by saying, + +68 +00:02:28,000 --> 00:02:30,000 +hey, you know, I have id, which is 102, + +69 +00:02:30,000 --> 00:02:31,000 +please check your book. + +70 +00:02:31,000 --> 00:02:32,000 +That should be my entry. + +71 +00:02:32,000 --> 00:02:34,000 +What do you think? + +72 +00:02:34,000 --> 00:02:36,000 +Will there be a coffee there or the entry there? + +73 +00:02:36,000 --> 00:02:39,000 +Of course not, because the book is there in my hometown. + +74 +00:02:39,000 --> 00:02:41,000 +It's not there in every city. + +75 +00:02:41,000 --> 00:02:43,000 +So that's the problem of scaling, which I'm talking about. + +76 +00:02:43,000 --> 00:02:45,000 +So what is the solution here? + +77 +00:02:45,000 --> 00:02:49,000 +The solution could be one where instead of them + +78 +00:02:49,000 --> 00:02:51,000 +making an entry on their book, + +79 +00:02:51,000 --> 00:02:54,000 +what if they can give me a pass, a monthly pass, which says, + +80 +00:02:54,000 --> 00:02:58,000 +Navin Reddy has already paid $20 for the entire month + +81 +00:02:58,000 --> 00:03:00,000 +and from this date to this date, + +82 +00:03:00,000 --> 00:03:04,000 +if the person has this pass, you can give them a coffee. + +83 +00:03:04,000 --> 00:03:05,000 +And that perfectly works, right? + +84 +00:03:05,000 --> 00:03:07,000 +I can just go there in any of the branch, + +85 +00:03:07,000 --> 00:03:10,000 +doesn't matter the location, I will get the coffee. + +86 +00:03:10,000 --> 00:03:12,000 +Okay, that sounds good, right? + +87 +00:03:12,000 --> 00:03:15,000 +And I shared this model with my friend + +88 +00:03:15,000 --> 00:03:17,000 +and now he knows that if you have that pass, + +89 +00:03:17,000 --> 00:03:18,000 +you will get a free coffee. + +90 +00:03:18,000 --> 00:03:22,000 +So what he did, he basically printed a coupon which says, + +91 +00:03:22,000 --> 00:03:26,000 +let's say Harsh now also paid $20 + +92 +00:03:26,000 --> 00:03:29,000 +and he will get a subscription for the entire month. + +93 +00:03:29,000 --> 00:03:31,000 +But Harsh has not done that. + +94 +00:03:31,000 --> 00:03:35,000 +He is creating his own coupon just by copying my coupon. + +95 +00:03:35,000 --> 00:03:36,000 +And anyone can do that, right? + +96 +00:03:36,000 --> 00:03:39,000 +So they will go to a coffee shop and say, "Hey, I'm Harsh. + +97 +00:03:39,000 --> 00:03:42,000 +I already paid for $20 for the coffee," + +98 +00:03:42,000 --> 00:03:44,000 +but that coupon is not valid. + +99 +00:03:44,000 --> 00:03:45,000 +How do I identify that? + +100 +00:03:45,000 --> 00:03:47,000 +So one way this cafes can improve is + +101 +00:03:47,000 --> 00:03:51,000 +what if with the coupon they also sign it, + +102 +00:03:51,000 --> 00:03:53,000 +maybe a manager sign or maybe a company stamp + +103 +00:03:53,000 --> 00:03:56,000 +or maybe a digital signature if it is online. + +104 +00:03:56,000 --> 00:03:59,000 +And now that is something which can prove + +105 +00:03:59,000 --> 00:04:00,000 +that this company coupon is valid. + +106 +00:04:00,000 --> 00:04:03,000 +So we moved from having an entry on a book + +107 +00:04:03,000 --> 00:04:07,000 +and giving you an ID instead of that give a pass itself, + +108 +00:04:07,000 --> 00:04:08,000 +which which mentions everything. + +109 +00:04:08,000 --> 00:04:11,000 +So in the pass itself, you have all the data, + +110 +00:04:11,000 --> 00:04:14,000 +the person name, at what time this coupon was issued, + +111 +00:04:14,000 --> 00:04:15,000 +what is the expression, + +112 +00:04:15,000 --> 00:04:17,000 +expression, time, and the rules as well. + +113 +00:04:17,000 --> 00:04:19,000 +So you are here just for the coffee, + +114 +00:04:19,000 --> 00:04:21,000 +not for the entire things on the cafe. + +115 +00:04:21,000 --> 00:04:23,000 +Maybe the cafe sells a lot of different stuff + +116 +00:04:23,000 --> 00:04:24,000 +apart from coffee. + +117 +00:04:24,000 --> 00:04:27,000 +What I will get is only coffee, right? + +118 +00:04:27,000 --> 00:04:29,000 +So those things will be mentioned on the pass. + +119 +00:04:29,000 --> 00:04:33,000 +So which is better having an ID or this pass? + +120 +00:04:33,000 --> 00:04:35,000 +Of course, both have their own advantages and drawbacks, + +121 +00:04:35,000 --> 00:04:39,000 +but this is another way of having that session. + +122 +00:04:39,000 --> 00:04:41,000 +Now, coming back to the IT world, + +123 +00:04:41,000 --> 00:04:43,000 +we have client and server + +124 +00:04:43,000 --> 00:04:45,000 +where client goes to the server by saying, + +125 +00:04:45,000 --> 00:04:46,000 +"Hey, I want a resource." + +126 +00:04:46,000 --> 00:04:48,000 +Now if it is static page, + +127 +00:04:48,000 --> 00:04:49,000 +we don't have to verify who the user is. + +128 +00:04:49,000 --> 00:04:51,000 +I can simply turn a static page. + +129 +00:04:51,000 --> 00:04:53,000 +But what if I want to change the data + +130 +00:04:53,000 --> 00:04:55,000 +based on the user request? + +131 +00:04:55,000 --> 00:04:57,000 +So let's say if I go to my social media app, + +132 +00:04:57,000 --> 00:04:58,000 +maybe it can be Instagram, YouTube, + +133 +00:04:58,000 --> 00:05:00,000 +and if I want to see the content, + +134 +00:05:00,000 --> 00:05:02,000 +or maybe I go to Facebook to see my friend list, + +135 +00:05:02,000 --> 00:05:05,000 +I want to see only my things, not others, + +136 +00:05:05,000 --> 00:05:08,000 +other, other settings right? + +137 +00:05:08,000 --> 00:05:10,000 +Now, in this case, the server should know who I am. + +138 +00:05:10,000 --> 00:05:12,000 +I can of, of course I can claim + +139 +00:05:12,000 --> 00:05:14,000 +I'm this person, but how do I verify that? + +140 +00:05:14,000 --> 00:05:16,000 +That's right. You're thinking about login. + +141 +00:05:16,000 --> 00:05:20,000 +But after doing a login, the server is basically stateless. + +142 +00:05:20,000 --> 00:05:22,000 +How do you manage there? + +143 +00:05:22,000 --> 00:05:24,000 +So one way you can do it on the server side, + +144 +00:05:24,000 --> 00:05:25,000 +you can make an entry. + +145 +00:05:25,000 --> 00:05:29,000 +So once I log in, the server says, okay, I know you, + +146 +00:05:29,000 --> 00:05:31,000 +you are real Navin Reddy. + +147 +00:05:31,000 --> 00:05:32,000 +This is your session id. + +148 +00:05:32,000 --> 00:05:34,000 +And that goes on with the session + +149 +00:05:34,000 --> 00:05:35,000 +with the cookies in my machine. + +150 +00:05:35,000 --> 00:05:38,000 +And every time I go to a server, I can use that cookie. + +151 +00:05:38,000 --> 00:05:41,000 +I don't have to basically log in every time for every page. + +152 +00:05:41,000 --> 00:05:43,000 +Now that will be weird, right? + +153 +00:05:43,000 --> 00:05:44,000 +You go to, let's say you go to Facebook, + +154 +00:05:44,000 --> 00:05:48,000 +and every time you log in, not a good idea. + +155 +00:05:48,000 --> 00:05:49,000 +So what do you do? + +156 +00:05:49,000 --> 00:05:50,000 +So basically you have a session ID, + +157 +00:05:50,000 --> 00:05:52,000 +which you can use every time. + +158 +00:05:52,000 --> 00:05:53,000 +And in fact we have seen that + +159 +00:05:53,000 --> 00:05:55,000 +when you were talking about this being security, + +160 +00:05:55,000 --> 00:05:58,000 +we call it JSessionId and things were working out. + +161 +00:05:58,000 --> 00:06:00,000 +But what if you want to scale now? + +162 +00:06:00,000 --> 00:06:01,000 +What if you don't have one server, + +163 +00:06:01,000 --> 00:06:03,000 +you have multiple servers. + +164 +00:06:03,000 --> 00:06:06,000 +In that case, JSessionID will not work. + +165 +00:06:06,000 --> 00:06:08,000 +It's because the session ID is there with one server. + +166 +00:06:08,000 --> 00:06:11,000 +When you are scaling up, when you have a horizontal scaling, + +167 +00:06:11,000 --> 00:06:13,000 +you have multiple machines now. + +168 +00:06:13,000 --> 00:06:15,000 +In this case, what we can do is we can, + +169 +00:06:15,000 --> 00:06:18,000 +all these servers can share the same database + +170 +00:06:18,000 --> 00:06:21,000 +or maybe a cache where you can say, okay, + +171 +00:06:21,000 --> 00:06:23,000 +we have a session ID for this particular person. + +172 +00:06:23,000 --> 00:06:26,000 +Every time you get a request check in the shared database, + +173 +00:06:26,000 --> 00:06:29,000 +or maybe you can tell your load balances, + +174 +00:06:29,000 --> 00:06:30,000 +which are between your, + +175 +00:06:30,000 --> 00:06:33,000 +between you and your servers, that hey, you know, + +176 +00:06:33,000 --> 00:06:34,000 +every time you get a request from Navin, + +177 +00:06:34,000 --> 00:06:37,000 +send it to server one because server one knows who Navin is. + +178 +00:06:37,000 --> 00:06:39,000 +That's one of the solution. + +179 +00:06:39,000 --> 00:06:41,000 +But what if you don't do all those stuff + +180 +00:06:41,000 --> 00:06:44,000 +and every time a client goes to the server + +181 +00:06:44,000 --> 00:06:45,000 +and say login done, + +182 +00:06:45,000 --> 00:06:48,000 +after that, what if server can give you a pass, + +183 +00:06:48,000 --> 00:06:50,000 +the same pass, which I got in the coffee shop + +184 +00:06:50,000 --> 00:06:52,000 +and now every time you go to a server, + +185 +00:06:52,000 --> 00:06:54,000 +you can show this pass, + +186 +00:06:54,000 --> 00:06:56,000 +hey, I already paid $20 for the coffee. + +187 +00:06:56,000 --> 00:06:57,000 +Gimme my coffee for today. + +188 +00:06:57,000 --> 00:07:01,000 +That's one thing a client can do with that pass. + +189 +00:07:01,000 --> 00:07:04,000 +But of course we have to also sign the pass, right? + +190 +00:07:04,000 --> 00:07:07,000 +You don't want unsigned pass where anyone can use it. + +191 +00:07:07,000 --> 00:07:10,000 +And that's where to implement this coffee concept + +192 +00:07:10,000 --> 00:07:12,000 +or the subscription concept. + +193 +00:07:12,000 --> 00:07:14,000 +We have something called JWT, + +194 +00:07:14,000 --> 00:07:17,000 +which stands for JSON web tokens. + +195 +00:07:17,000 --> 00:07:18,000 +Basically the concept is about tokens + +196 +00:07:18,000 --> 00:07:21,000 +and another one of the way to implement that is JWT. + +197 +00:07:21,000 --> 00:07:25,000 +So what exactly JWT is and what are the options we have? + +198 +00:07:25,000 --> 00:07:27,000 +We'll see in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/004 What is JWT_en.srt b/22 - JWT (JSON Web Token) and OAuth2/004 What is JWT_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..35d4bd86798da882f13c297a95fc95da2dd35307 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/004 What is JWT_en.srt @@ -0,0 +1,696 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:04,000 +-: So what is JWT? + +3 +00:00:04,000 --> 00:00:07,000 +So JWT stands for JSON Web Token. + +4 +00:00:07,000 --> 00:00:11,000 +It's a open industry standard with RFC 7519, + +5 +00:00:11,000 --> 00:00:14,000 +and basically it is a way to transfer data + +6 +00:00:14,000 --> 00:00:16,000 +between two different parties, and that to securely. + +7 +00:00:16,000 --> 00:00:18,000 +Okay, now how it looks like. + +8 +00:00:18,000 --> 00:00:22,000 +See, whenever you send a token from a server to client, + +9 +00:00:22,000 --> 00:00:23,000 +it can be a normal XML as well, + +10 +00:00:23,000 --> 00:00:26,000 +so if you talk about technical world, not the coffee card. + +11 +00:00:26,000 --> 00:00:28,000 +So let's say if in the technical world, + +12 +00:00:28,000 --> 00:00:33,000 +you will be having this token a JSON data or XML data, + +13 +00:00:33,000 --> 00:00:36,000 +but in XML, the length of the data will be lengthy, right? + +14 +00:00:36,000 --> 00:00:38,000 +Even for the small amount of data, + +15 +00:00:38,000 --> 00:00:41,000 +you'll be having multiple tags and stuff, + +16 +00:00:41,000 --> 00:00:43,000 +plus encrypting it will be a different task. + +17 +00:00:43,000 --> 00:00:46,000 +The other way is you can use normal JSON, + +18 +00:00:46,000 --> 00:00:47,000 +but again, JSON will be lengthy as well. + +19 +00:00:47,000 --> 00:00:50,000 +In that case, we can use some encoded format, + +20 +00:00:50,000 --> 00:00:53,000 +and that's where the JSON web token comes to the picture. + +21 +00:00:53,000 --> 00:00:55,000 +Now this is the format I'm talking about. + +22 +00:00:55,000 --> 00:00:57,000 +So when you get a token from the server, + +23 +00:00:57,000 --> 00:00:58,000 +this is what you will get. + +24 +00:00:58,000 --> 00:01:02,000 +So this is alphanumeric values, but we can't read it + +25 +00:01:02,000 --> 00:01:04,000 +because this is in the encoded format. + +26 +00:01:04,000 --> 00:01:07,000 +So you can also decode this, and when you decode it, + +27 +00:01:07,000 --> 00:01:09,000 +you can see we have three sections here. + +28 +00:01:09,000 --> 00:01:10,000 +Now, before that, + +29 +00:01:10,000 --> 00:01:11,000 +you can see there's a section of algorithms. + +30 +00:01:11,000 --> 00:01:15,000 +So by the way, where I am now, I'm on jwt.io, + +31 +00:01:15,000 --> 00:01:17,000 +and you can see we can use different algorithms here. + +32 +00:01:17,000 --> 00:01:19,000 +Remember when I talked about the signature + +33 +00:01:19,000 --> 00:01:20,000 +or the encryption? + +34 +00:01:20,000 --> 00:01:23,000 +Basically we can use different algorithms here. + +35 +00:01:23,000 --> 00:01:23,000 +We have different options. + +36 +00:01:23,000 --> 00:01:28,000 +We got Hs, we got Rs, we got Es, and Ps. + +37 +00:01:28,000 --> 00:01:30,000 +Now basically we have, + +38 +00:01:30,000 --> 00:01:32,000 +remember when we talked about the asymmetric and symmetric? + +39 +00:01:32,000 --> 00:01:34,000 +So basically we have Hs here, + +40 +00:01:34,000 --> 00:01:38,000 +which is HMAC SHA-256, which is symmetric. + +41 +00:01:38,000 --> 00:01:41,000 +If you want to use asymmetric, we can also use Rs here, + +42 +00:01:41,000 --> 00:01:44,000 +and higher this numbers, it is better, + +43 +00:01:44,000 --> 00:01:45,000 +because you'll be having a bigger key. + +44 +00:01:45,000 --> 00:01:47,000 +Now here you can see we have three sections. + +45 +00:01:47,000 --> 00:01:49,000 +So each token will have three sections. + +46 +00:01:49,000 --> 00:01:51,000 +One is the header, one is a payload, + +47 +00:01:51,000 --> 00:01:53,000 +and next is the signature. + +48 +00:01:53,000 --> 00:01:55,000 +Now payload is very important here + +49 +00:01:55,000 --> 00:01:56,000 +because this is where you send your data. + +50 +00:01:56,000 --> 00:01:59,000 +So when you say a server's giving some token to the client, + +51 +00:01:59,000 --> 00:02:01,000 +with the information, + +52 +00:02:01,000 --> 00:02:03,000 +that information stays inside the payload. + +53 +00:02:03,000 --> 00:02:06,000 +Example, we got, who is subject the person? + +54 +00:02:06,000 --> 00:02:08,000 +So this is a subject, + +55 +00:02:08,000 --> 00:02:11,000 +a name, this, and the issuance time is this. + +56 +00:02:11,000 --> 00:02:14,000 +We can also set the expire time here if you want. + +57 +00:02:14,000 --> 00:02:19,000 +You can give a comma and you can say expiry, + +58 +00:02:19,000 --> 00:02:21,000 +and you can give a colon, and you can mention whatever time. + +59 +00:02:21,000 --> 00:02:24,000 +So maybe I want to use this time, + +60 +00:02:24,000 --> 00:02:25,000 +plus I want to change, + +61 +00:02:25,000 --> 00:02:28,000 +let's say I want to make this as 41. + +62 +00:02:28,000 --> 00:02:29,000 +So this is my expiration time, + +63 +00:02:29,000 --> 00:02:32,000 +and this should result some values. + +64 +00:02:32,000 --> 00:02:33,000 +So issuance is this. + +65 +00:02:33,000 --> 00:02:35,000 +This is a date for today, not today. + +66 +00:02:35,000 --> 00:02:39,000 +This is a date which is Jan 18th, 2018, + +67 +00:02:39,000 --> 00:02:42,000 +and 7:00 AM, looks like. + +68 +00:02:42,000 --> 00:02:46,000 +And if you look here, this is 7:30. + +69 +00:02:46,000 --> 00:02:48,000 +So we got this for 30 minutes, right? + +70 +00:02:48,000 --> 00:02:49,000 +The token. + +71 +00:02:49,000 --> 00:02:50,000 +Of course, the token will expire. + +72 +00:02:50,000 --> 00:02:52,000 +You can set the expire time here. + +73 +00:02:52,000 --> 00:02:54,000 +So this is your payload and you can send multiple data here. + +74 +00:02:54,000 --> 00:02:56,000 +It's not just you have to send only this. + +75 +00:02:56,000 --> 00:02:57,000 +We can add more data, + +76 +00:02:57,000 --> 00:03:02,000 +but trying to make payload as small as possible, + +77 +00:03:02,000 --> 00:03:03,000 +because you'll be sending this in the header, + +78 +00:03:03,000 --> 00:03:04,000 +and then there are some servers + +79 +00:03:04,000 --> 00:03:08,000 +which will not accept a bigger chunk of header. + +80 +00:03:08,000 --> 00:03:11,000 +So make sure that you don't put unnecessary stuff here. + +81 +00:03:11,000 --> 00:03:12,000 +Okay, now this is algorithm which we are using here, + +82 +00:03:12,000 --> 00:03:14,000 +which is HS226. + +83 +00:03:14,000 --> 00:03:15,000 +This is what you'll be having in the header. + +84 +00:03:15,000 --> 00:03:17,000 +You can change it and you can use some other algorithm. + +85 +00:03:17,000 --> 00:03:20,000 +Let's say if I want to use R, I can use that, + +86 +00:03:20,000 --> 00:03:21,000 +but let me stick to Hs here, + +87 +00:03:21,000 --> 00:03:25,000 +and then we are specifying the type of token, + +88 +00:03:25,000 --> 00:03:27,000 +which is JWT in this case. + +89 +00:03:27,000 --> 00:03:28,000 +Next we have a signature. + +90 +00:03:28,000 --> 00:03:30,000 +So when you say you want to sign it, + +91 +00:03:30,000 --> 00:03:31,000 +you have to use some signature, + +92 +00:03:31,000 --> 00:03:34,000 +and of course, you can use RSA + +93 +00:03:34,000 --> 00:03:34,000 +where you get two different signatures, + +94 +00:03:34,000 --> 00:03:36,000 +for private and public key, + +95 +00:03:36,000 --> 00:03:37,000 +but when you're using Hs, + +96 +00:03:37,000 --> 00:03:38,000 +you'll get only one secret key + +97 +00:03:38,000 --> 00:03:41,000 +which will be shared by client and server, + +98 +00:03:41,000 --> 00:03:43,000 +and you can encrypt it. + +99 +00:03:43,000 --> 00:03:45,000 +The problem is when you say encryption, + +100 +00:03:45,000 --> 00:03:48,000 +we are not achieving the actual encryption here. + +101 +00:03:48,000 --> 00:03:48,000 +We are just saying + +102 +00:03:48,000 --> 00:03:52,000 +that no one should be able to modify this token. + +103 +00:03:52,000 --> 00:03:54,000 +So example, if I got a token from the server, + +104 +00:03:54,000 --> 00:03:57,000 +no one else can hack my token and modify it, + +105 +00:03:57,000 --> 00:03:58,000 +because it is signed, right? + +106 +00:03:58,000 --> 00:04:00,000 +But what if you want to encrypt it? + +107 +00:04:00,000 --> 00:04:04,000 +So we also have an option of encrypting your JWT, + +108 +00:04:04,000 --> 00:04:06,000 +and by the default, we are not doing it here, + +109 +00:04:06,000 --> 00:04:08,000 +but we can encrypt it, and that's where. + +110 +00:04:08,000 --> 00:04:11,000 +When you are not encrypting your token, + +111 +00:04:11,000 --> 00:04:13,000 +that means anyone can see that. + +112 +00:04:13,000 --> 00:04:15,000 +That's right, anyone can see your data, + +113 +00:04:15,000 --> 00:04:19,000 +and that's why don't put secret data on in the payload. + +114 +00:04:19,000 --> 00:04:22,000 +Don't put your phone number or your Social Security number + +115 +00:04:22,000 --> 00:04:24,000 +or your bank password. + +116 +00:04:24,000 --> 00:04:26,000 +It should be only these details. + +117 +00:04:26,000 --> 00:04:28,000 +So that's basically JWT. + +118 +00:04:28,000 --> 00:04:30,000 +Now how we are going to implement this? + +119 +00:04:30,000 --> 00:04:32,000 +So if you want to implement this in your application, + +120 +00:04:32,000 --> 00:04:34,000 +so let's say we are going to build a very simple one + +121 +00:04:34,000 --> 00:04:37,000 +where a user sends a request to the server by saying, + +122 +00:04:37,000 --> 00:04:39,000 +I want to say hi, + +123 +00:04:39,000 --> 00:04:40,000 +and the server will say, "Welcome to Telusko." + +124 +00:04:40,000 --> 00:04:43,000 +That's it, a simple API, and I want to secure it. + +125 +00:04:43,000 --> 00:04:44,000 +Now how we are gonna do this? + +126 +00:04:44,000 --> 00:04:46,000 +First step, + +127 +00:04:46,000 --> 00:04:49,000 +a client will send the username and password to the server. + +128 +00:04:49,000 --> 00:04:51,000 +A server will say, + +129 +00:04:51,000 --> 00:04:52,000 +"Okay, I'm verifying the username and password, + +130 +00:04:52,000 --> 00:04:55,000 +which is correct, and now you are logged in." + +131 +00:04:55,000 --> 00:04:56,000 +Now once you are logged in, + +132 +00:04:56,000 --> 00:05:00,000 +the server will send a JWT token. + +133 +00:05:00,000 --> 00:05:02,000 +Now this token goes to the client, + +134 +00:05:02,000 --> 00:05:04,000 +and it's client's responsibility to save it now, + +135 +00:05:04,000 --> 00:05:07,000 +and next time when a client goes to the server, by saying, + +136 +00:05:07,000 --> 00:05:09,000 +"Hey, now I want sample predicted resource," + +137 +00:05:09,000 --> 00:05:12,000 +maybe I want to say hi, or maybe I want to add two numbers, + +138 +00:05:12,000 --> 00:05:15,000 +whatever it can be, the server will say, "Who are you?" + +139 +00:05:15,000 --> 00:05:17,000 +Then you can say, "Okay, hold on." + +140 +00:05:17,000 --> 00:05:20,000 +When I'm sending a request, I have to also send the token. + +141 +00:05:20,000 --> 00:05:22,000 +The token goes to a server. + +142 +00:05:22,000 --> 00:05:23,000 +Server will say, "Okay, let me wait. + +143 +00:05:23,000 --> 00:05:25,000 +Let me verify your token first," + +144 +00:05:25,000 --> 00:05:27,000 +and once the token is verified, + +145 +00:05:27,000 --> 00:05:31,000 +a server will be able to call the API + +146 +00:05:31,000 --> 00:05:33,000 +and send the response back. + +147 +00:05:33,000 --> 00:05:35,000 +That means every time you go to the server, + +148 +00:05:35,000 --> 00:05:38,000 +you have to take your token with you, + +149 +00:05:38,000 --> 00:05:41,000 +and of course, it should be done before it expires. + +150 +00:05:41,000 --> 00:05:43,000 +Now, if you want to implement this in your application, + +151 +00:05:43,000 --> 00:05:46,000 +we have to use some library for JWT, + +152 +00:05:46,000 --> 00:05:47,000 +and we are going to see that in the code. + +153 +00:05:47,000 --> 00:05:49,000 +And most of the things, + +154 +00:05:49,000 --> 00:05:52,000 +which will make sense in the code itself, don't worry. + +155 +00:05:52,000 --> 00:05:54,000 +But we have to add multiple mechanisms here. + +156 +00:05:54,000 --> 00:05:56,000 +First we have to have a library. + +157 +00:05:56,000 --> 00:05:59,000 +Now, using this library, you will be creating a token. + +158 +00:05:59,000 --> 00:06:01,000 +So of course, you have to do a login, + +159 +00:06:01,000 --> 00:06:03,000 +and once it is logged in, you have to create a token. + +160 +00:06:03,000 --> 00:06:06,000 +So you are responsible to create the token. + +161 +00:06:06,000 --> 00:06:08,000 +Next, you have to send this token to the client, + +162 +00:06:08,000 --> 00:06:11,000 +and when a client sends the next request, + +163 +00:06:11,000 --> 00:06:13,000 +a client will send a token, + +164 +00:06:13,000 --> 00:06:15,000 +and it is your job to verify the token. + +165 +00:06:15,000 --> 00:06:17,000 +So you have multiple steps to do, + +166 +00:06:17,000 --> 00:06:19,000 +and the upcoming code will not be that simple, + +167 +00:06:19,000 --> 00:06:20,000 +so just be with me. + +168 +00:06:20,000 --> 00:06:22,000 +I will try to make it simple, + +169 +00:06:22,000 --> 00:06:23,000 +but once you complete the entire code, + +170 +00:06:23,000 --> 00:06:26,000 +you can go through it and things will make sense. + +171 +00:06:26,000 --> 00:06:28,000 +So yeah, we have, we just have two tasks here, + +172 +00:06:28,000 --> 00:06:30,000 +create the token and verify the token, + +173 +00:06:30,000 --> 00:06:31,000 +and how do we do that? + +174 +00:06:31,000 --> 00:06:33,000 +That's in the practical videos. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/005 Project Setup for JWT_en.srt b/22 - JWT (JSON Web Token) and OAuth2/005 Project Setup for JWT_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..76bab9eb8d4a187db7e655ab16858776a446a0a0 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/005 Project Setup for JWT_en.srt @@ -0,0 +1,668 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:07,000 +-: So now, let's try to implement JWT in our project. + +3 +00:00:07,000 --> 00:00:10,000 +So basically we'll be changing our project + +4 +00:00:10,000 --> 00:00:13,000 +which we have done in the spring security section. + +5 +00:00:13,000 --> 00:00:15,000 +So we have built a basic application + +6 +00:00:15,000 --> 00:00:17,000 +where we are trying to manage the students. + +7 +00:00:17,000 --> 00:00:19,000 +And you will find the code + +8 +00:00:19,000 --> 00:00:22,000 +which we have taken from the last video of that section. + +9 +00:00:22,000 --> 00:00:24,000 +So basically I'm talking about the student application, + +10 +00:00:24,000 --> 00:00:26,000 +And just to show you the code, what I'm talking about. + +11 +00:00:26,000 --> 00:00:30,000 +So basically I got the same code, which we have done before. + +12 +00:00:30,000 --> 00:00:32,000 +Not the job one, but the student one, + +13 +00:00:32,000 --> 00:00:34,000 +and in which we have defense services. + +14 +00:00:34,000 --> 00:00:37,000 +Example, if I go back to my controller, + +15 +00:00:37,000 --> 00:00:39,000 +we got HelloController, we got StudentController, + +16 +00:00:39,000 --> 00:00:41,000 +we got UserController. + +17 +00:00:41,000 --> 00:00:43,000 +This user is for the account creation. + +18 +00:00:43,000 --> 00:00:45,000 +So you can see we have the extra user. + +19 +00:00:45,000 --> 00:00:47,000 +And we got StudentController + +20 +00:00:47,000 --> 00:00:48,000 +where we can manage the students. + +21 +00:00:48,000 --> 00:00:50,000 +Maybe you want to get old students. + +22 +00:00:50,000 --> 00:00:52,000 +Maybe you want to add a new student. + +23 +00:00:52,000 --> 00:00:54,000 +So those things will be done here. + +24 +00:00:54,000 --> 00:00:56,000 +And then we got HelloController, + +25 +00:00:56,000 --> 00:01:00,000 +a simple one where we have a hello API, then we got about. + +26 +00:01:00,000 --> 00:01:02,000 +And let's use this stuff. + +27 +00:01:02,000 --> 00:01:05,000 +So let me just use this particular hello here. + +28 +00:01:05,000 --> 00:01:08,000 +And also I want to make this as a rest control, + +29 +00:01:08,000 --> 00:01:09,000 +so this rest. + +30 +00:01:09,000 --> 00:01:11,000 +I will get this data in the response. Okay. + +31 +00:01:11,000 --> 00:01:13,000 +And maybe I don't even want a session ID this time. + +32 +00:01:13,000 --> 00:01:15,000 +Let's only work with the Hello World. + +33 +00:01:15,000 --> 00:01:16,000 +I will just remove this. + +34 +00:01:16,000 --> 00:01:18,000 +And you can see we got Hello World here. + +35 +00:01:18,000 --> 00:01:19,000 +And we got dao here + +36 +00:01:19,000 --> 00:01:23,000 +because we want to work with database well for the user. + +37 +00:01:23,000 --> 00:01:24,000 +So we got dao here. + +38 +00:01:24,000 --> 00:01:28,000 +And we got a model for student, + +39 +00:01:28,000 --> 00:01:30,000 +for user, and for UserPrincipal. + +40 +00:01:30,000 --> 00:01:32,000 +So user, UserPrincipal for the security. + +41 +00:01:32,000 --> 00:01:33,000 +We got student + +42 +00:01:33,000 --> 00:01:35,000 +because we are working with the student project. + +43 +00:01:35,000 --> 00:01:38,000 +And then in the service we got MyUserDetailsService + +44 +00:01:38,000 --> 00:01:39,000 +and UserService. + +45 +00:01:39,000 --> 00:01:41,000 +Okay, these are the things we have already done with, okay? + +46 +00:01:41,000 --> 00:01:44,000 +And now, let me run this application to see, + +47 +00:01:44,000 --> 00:01:47,000 +but before I run, I made some changes in the port number + +48 +00:01:47,000 --> 00:01:49,000 +because my agent report number was used by something else, + +49 +00:01:49,000 --> 00:01:50,000 +so I'm changing it. + +50 +00:01:50,000 --> 00:01:51,000 +You don't have to do that. + +51 +00:01:51,000 --> 00:01:54,000 +I've also changed my database name to mydb + +52 +00:01:54,000 --> 00:01:56,000 +and everything remains same. + +53 +00:01:56,000 --> 00:01:59,000 +One change, I want to create this table if it is not there. + +54 +00:01:59,000 --> 00:02:04,000 +So I've made these changes, hibernate.ddl-auto=update. + +55 +00:02:04,000 --> 00:02:06,000 +And I want to see the SQL in the console as well. + +56 +00:02:06,000 --> 00:02:08,000 +So you can see we got that. + +57 +00:02:08,000 --> 00:02:09,000 +Anymore changes, + +58 +00:02:09,000 --> 00:02:13,000 +I think we have to change something else as well. + +59 +00:02:13,000 --> 00:02:14,000 +So we'll go back to config + +60 +00:02:14,000 --> 00:02:16,000 +and I will go back to SecurityConfig. + +61 +00:02:16,000 --> 00:02:19,000 +Now this is where you have done this configuration + +62 +00:02:19,000 --> 00:02:21,000 +for the security. + +63 +00:02:21,000 --> 00:02:23,000 +Basically we are not using a default security + +64 +00:02:23,000 --> 00:02:25,000 +from Spring Security. + +65 +00:02:25,000 --> 00:02:26,000 +We have made some changes. + +66 +00:02:28,000 --> 00:02:30,000 +We have disabled this csrf + +67 +00:02:30,000 --> 00:02:33,000 +and then remaining everything should be authenticated. + +68 +00:02:33,000 --> 00:02:36,000 +And also, I have made this stateless. + +69 +00:02:36,000 --> 00:02:39,000 +So basically in JWT as well, we have to use stateless. + +70 +00:02:39,000 --> 00:02:40,000 +So let's keep it stateless. + +71 +00:02:40,000 --> 00:02:43,000 +And we got the authentication provider, + +72 +00:02:43,000 --> 00:02:45,000 +which is your DaoAuthenticationProvider, + +73 +00:02:45,000 --> 00:02:48,000 +which basically work with database to get you details. + +74 +00:02:48,000 --> 00:02:52,000 +So you can see we are basically setting the user details + +75 +00:02:52,000 --> 00:02:54,000 +and we have setting the password encoder, + +76 +00:02:54,000 --> 00:02:56,000 +and it can verify the data with the database. + +77 +00:02:56,000 --> 00:02:59,000 +And we are using bcrypt as well. + +78 +00:02:59,000 --> 00:03:00,000 +So just to give you a walkthrough + +79 +00:03:00,000 --> 00:03:03,000 +what we have done till now, this is what we have done. + +80 +00:03:03,000 --> 00:03:05,000 +I want to make one change. + +81 +00:03:05,000 --> 00:03:09,000 +Because if you see, when you talk about the UserController + +82 +00:03:09,000 --> 00:03:12,000 +we are sending a request for the register. + +83 +00:03:12,000 --> 00:03:15,000 +Now don't you think the entire application secure, + +84 +00:03:15,000 --> 00:03:17,000 +if you want to access any URL, + +85 +00:03:17,000 --> 00:03:20,000 +you have to basically send a request. + +86 +00:03:20,000 --> 00:03:21,000 +And in that request, + +87 +00:03:21,000 --> 00:03:23,000 +you have to also send the login details. + +88 +00:03:23,000 --> 00:03:25,000 +Otherwise, this will not work + +89 +00:03:25,000 --> 00:03:27,000 +because all the APIs are secured. + +90 +00:03:27,000 --> 00:03:29,000 +What I want to do is if someone is calling register, + +91 +00:03:29,000 --> 00:03:32,000 +I don't want them to logged in. + +92 +00:03:32,000 --> 00:03:34,000 +We have done that in the previous section, but not now. + +93 +00:03:34,000 --> 00:03:38,000 +Let's say I want anyone should be able to call register. + +94 +00:03:38,000 --> 00:03:39,000 +So in that case, what I will do + +95 +00:03:39,000 --> 00:03:41,000 +is I will go to my SecurityConfig + +96 +00:03:41,000 --> 00:03:45,000 +and I will add one more thing here, + +97 +00:03:45,000 --> 00:03:50,000 +which is requestMatchers in which I will provide some path. + +98 +00:03:50,000 --> 00:03:52,000 +One of the path I want to say is request. + +99 +00:03:52,000 --> 00:03:53,000 +Or not request, register. + +100 +00:03:53,000 --> 00:03:57,000 +So anyone request for register this URL, + +101 +00:03:57,000 --> 00:03:58,000 +I want this to be allowed. + +102 +00:03:58,000 --> 00:04:01,000 +So I will say .permitAll. + +103 +00:04:01,000 --> 00:04:03,000 +So whoever requests for register + +104 +00:04:03,000 --> 00:04:05,000 +should be directly able to access that + +105 +00:04:05,000 --> 00:04:06,000 +without the login details. + +106 +00:04:06,000 --> 00:04:09,000 +But apart from that, all the requests will be authenticated. + +107 +00:04:09,000 --> 00:04:11,000 +Let's see if this works. + +108 +00:04:11,000 --> 00:04:13,000 +So I will just go back, + +109 +00:04:13,000 --> 00:04:15,000 +I will just restart the application taking some time. + +110 +00:04:15,000 --> 00:04:17,000 +Okay, so servers started on port number 8081. + +111 +00:04:17,000 --> 00:04:19,000 +So I will go back to my postman + +112 +00:04:19,000 --> 00:04:21,000 +because that's what we are using. + +113 +00:04:21,000 --> 00:04:23,000 +So it's 8081. + +114 +00:04:23,000 --> 00:04:26,000 +And I will send a request for register. + +115 +00:04:26,000 --> 00:04:28,000 +So basically you have to send the data. + +116 +00:04:28,000 --> 00:04:30,000 +So what I will do is I will just go back here + +117 +00:04:30,000 --> 00:04:33,000 +and what data I have to send. + +118 +00:04:33,000 --> 00:04:36,000 +Now this is depend upon what register is accepting. + +119 +00:04:36,000 --> 00:04:40,000 +So if you go back to User, not User. + +120 +00:04:40,000 --> 00:04:42,000 +If you go back to UserController, + +121 +00:04:42,000 --> 00:04:45,000 +this register is accepting a user. + +122 +00:04:45,000 --> 00:04:46,000 +And then you have to go to User + +123 +00:04:46,000 --> 00:04:48,000 +to check what are things you have to send. + +124 +00:04:48,000 --> 00:04:49,000 +You have to send the ID. + +125 +00:04:49,000 --> 00:04:51,000 +You want to send the username and password. + +126 +00:04:51,000 --> 00:04:54,000 +Also, I don't want to pass the ID by myself. + +127 +00:04:54,000 --> 00:04:57,000 +I want the hibernate to create the ID for us. + +128 +00:04:57,000 --> 00:04:58,000 +So I can specify GeneratedValue, + +129 +00:04:58,000 --> 00:05:02,000 +strategy is equal to GeneratedType_IDENTITY. + +130 +00:05:02,000 --> 00:05:05,000 +So it'll generate the IDs for us. + +131 +00:05:05,000 --> 00:05:06,000 +And you don't have to pass that. + +132 +00:05:06,000 --> 00:05:08,000 +So you have to pass the username, password. + +133 +00:05:08,000 --> 00:05:10,000 +After making the changes, I will reload once again + +134 +00:05:10,000 --> 00:05:12,000 +and go back to my Postman. + +135 +00:05:12,000 --> 00:05:14,000 +And now, we have to send the username. + +136 +00:05:14,000 --> 00:05:16,000 +I'm sending the username as navin + +137 +00:05:17,000 --> 00:05:19,000 +and I'm sending the password. + +138 +00:05:19,000 --> 00:05:20,000 +Of course we are using bcrypts, + +139 +00:05:20,000 --> 00:05:24,000 +so we are safe when you are passing through the internet. + +140 +00:05:24,000 --> 00:05:28,000 +So I'm sendingn@123. + +141 +00:05:28,000 --> 00:05:29,000 +Send. + +142 +00:05:29,000 --> 00:05:31,000 +Okay, it is giving you error. + +143 +00:05:31,000 --> 00:05:33,000 +Something is not working the way we want it. + +144 +00:05:33,000 --> 00:05:36,000 +Okay, I have to make it No Auth. + +145 +00:05:37,000 --> 00:05:40,000 +So make sure that in the authorization you say No Auth. + +146 +00:05:40,000 --> 00:05:42,000 +And click on Send. + +147 +00:05:42,000 --> 00:05:43,000 +Okay, still not working. + +148 +00:05:43,000 --> 00:05:44,000 +Okay, my bad. + +149 +00:05:44,000 --> 00:05:45,000 +I'm using a GET request. + +150 +00:05:45,000 --> 00:05:47,000 +I should be using a POST request. + +151 +00:05:47,000 --> 00:05:48,000 +Click on Send. + +152 +00:05:48,000 --> 00:05:50,000 +So we are supposed to send a post request + +153 +00:05:50,000 --> 00:05:53,000 +and you can see in the response you got the details. + +154 +00:05:53,000 --> 00:05:54,000 +Now this is stored in the database. + +155 +00:05:54,000 --> 00:05:56,000 +Let's verify that. + +156 +00:05:56,000 --> 00:06:00,000 +I will go back to my pgAdmin, click on Run, + +157 +00:06:00,000 --> 00:06:01,000 +and you can see we got the entry here. + +158 +00:06:01,000 --> 00:06:03,000 +So we got the ID, we got the password + +159 +00:06:03,000 --> 00:06:06,000 +and we got the name as well. + +160 +00:06:06,000 --> 00:06:07,000 +So this is working perfect. + +161 +00:06:07,000 --> 00:06:11,000 +And now let's do the steps. + +162 +00:06:11,000 --> 00:06:13,000 +So we have done the registration. + +163 +00:06:13,000 --> 00:06:16,000 +The next steps should be login, right? + +164 +00:06:16,000 --> 00:06:18,000 +And so let's do that step by step. + +165 +00:06:18,000 --> 00:06:19,000 +In the next video, let's talk about login + +166 +00:06:19,000 --> 00:06:22,000 +and then we'll move towards how do we create a token + +167 +00:06:22,000 --> 00:06:23,000 +and how do we verify a token. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/006 Custom Login_en.srt b/22 - JWT (JSON Web Token) and OAuth2/006 Custom Login_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..b24fb9fd6d29e12ba00ca8ab5e1ae88e14a9def4 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/006 Custom Login_en.srt @@ -0,0 +1,628 @@ +1 +00:00:00,000 --> 00:00:03,000 +(upbeat music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now once you have registered the user, + +3 +00:00:05,000 --> 00:00:07,000 +and of course we can add multiple users there, + +4 +00:00:07,000 --> 00:00:09,000 +it's time to do the login. + +5 +00:00:09,000 --> 00:00:12,000 +And once you do the login, you have to return a token, + +6 +00:00:12,000 --> 00:00:14,000 +the JWT token. + +7 +00:00:14,000 --> 00:00:17,000 +Okay, now the weird stuff is JWT itself, + +8 +00:00:17,000 --> 00:00:18,000 +the T itself is token, right? + +9 +00:00:18,000 --> 00:00:20,000 +But we still do that, it doesn't matter. + +10 +00:00:20,000 --> 00:00:24,000 +So when I say JWT token, imagine the JSON web token, anyway. + +11 +00:00:24,000 --> 00:00:28,000 +So coming back here, if you go back to your user controller, + +12 +00:00:28,000 --> 00:00:32,000 +we do have the API for register. + +13 +00:00:32,000 --> 00:00:34,000 +I want API for the login. + +14 +00:00:34,000 --> 00:00:35,000 +So what I will do is I will say public + +15 +00:00:35,000 --> 00:00:37,000 +and I want to return string. + +16 +00:00:37,000 --> 00:00:41,000 +I will say login and this will accept the values. + +17 +00:00:41,000 --> 00:00:42,000 +Now what values I want + +18 +00:00:42,000 --> 00:00:45,000 +of course the same thing which a user accepts, + +19 +00:00:45,000 --> 00:00:47,000 +which is the username and password. + +20 +00:00:47,000 --> 00:00:51,000 +So I can simply say request body and user, user. + +21 +00:00:51,000 --> 00:00:53,000 +So for the store and for login, we'll send the same data. + +22 +00:00:53,000 --> 00:00:56,000 +So I can simply send the user. + +23 +00:00:56,000 --> 00:00:58,000 +I hope we only have two things there, anyway. + +24 +00:00:58,000 --> 00:01:00,000 +We are not sending the Id, so that perfectly works. + +25 +00:01:00,000 --> 00:01:03,000 +And then the mapping I want here is post mapping. + +26 +00:01:03,000 --> 00:01:05,000 +So I will simply mention + +27 +00:01:05,000 --> 00:01:07,000 +whenever someone calls for login, + +28 +00:01:07,000 --> 00:01:09,000 +this is a UL we have to hit, okay? + +29 +00:01:09,000 --> 00:01:10,000 +There's one little problem. + +30 +00:01:10,000 --> 00:01:13,000 +Every time you send a request to the server, + +31 +00:01:13,000 --> 00:01:15,000 +it has to be authenticated, right? + +32 +00:01:15,000 --> 00:01:17,000 +I mean, if you want to exercise URL, + +33 +00:01:17,000 --> 00:01:18,000 +you have to send the credentials, + +34 +00:01:18,000 --> 00:01:22,000 +but I don't want the credentials for register and for login. + +35 +00:01:22,000 --> 00:01:24,000 +We have done something for the register but not for login. + +36 +00:01:24,000 --> 00:01:25,000 +So I can just give a comma here + +37 +00:01:25,000 --> 00:01:29,000 +and I want the same exception for login as well. + +38 +00:01:29,000 --> 00:01:32,000 +So for this two URLs, I don't want it + +39 +00:01:32,000 --> 00:01:33,000 +to check for the credentials. + +40 +00:01:33,000 --> 00:01:37,000 +So it's a login, and now let's do the actual work. + +41 +00:01:37,000 --> 00:01:39,000 +How do you actually check if + +42 +00:01:39,000 --> 00:01:41,000 +the username password is correct? + +43 +00:01:41,000 --> 00:01:43,000 +Now in build in spring security, + +44 +00:01:43,000 --> 00:01:47,000 +we have something called user password authentication token. + +45 +00:01:47,000 --> 00:01:49,000 +So every time it checks it, + +46 +00:01:49,000 --> 00:01:52,000 +it'll verify your username password, it'll generate a token. + +47 +00:01:52,000 --> 00:01:53,000 +And with that we can actually check + +48 +00:01:53,000 --> 00:01:55,000 +if the user is valid or not. + +49 +00:01:55,000 --> 00:01:59,000 +And what it returns is the authentication object. + +50 +00:01:59,000 --> 00:02:01,000 +See, ultimately the username password is verified + +51 +00:02:01,000 --> 00:02:02,000 +with the help of authentication. + +52 +00:02:02,000 --> 00:02:05,000 +This is the object which holds the data. + +53 +00:02:05,000 --> 00:02:07,000 +So I will say this is authentication. + +54 +00:02:07,000 --> 00:02:10,000 +And again, authentication is equal to, + +55 +00:02:10,000 --> 00:02:14,000 +and once you get, I mean you have to put the data + +56 +00:02:14,000 --> 00:02:15,000 +of user into this. + +57 +00:02:15,000 --> 00:02:16,000 +And the way you can do that is + +58 +00:02:16,000 --> 00:02:19,000 +through authentication manager. + +59 +00:02:19,000 --> 00:02:20,000 +See, ultimately when you send a request, + +60 +00:02:20,000 --> 00:02:22,000 +a request goes to the authentication manager. + +61 +00:02:22,000 --> 00:02:24,000 +From that it goes, it uses something called + +62 +00:02:24,000 --> 00:02:25,000 +authentication provider. + +63 +00:02:25,000 --> 00:02:29,000 +In our case, we are using a Dao authentication provider. + +64 +00:02:29,000 --> 00:02:33,000 +And this uses object of authentication, right? + +65 +00:02:33,000 --> 00:02:35,000 +So if you want to get this hold of this object, + +66 +00:02:35,000 --> 00:02:37,000 +we have to use object + +67 +00:02:37,000 --> 00:02:40,000 +of authentication manager, but we don't have it. + +68 +00:02:40,000 --> 00:02:43,000 +So I want one of the bean for authentication managers. + +69 +00:02:43,000 --> 00:02:47,000 +I will just go back here and say I want a new bean. + +70 +00:02:47,000 --> 00:02:50,000 +And this should be for the authentication manager. + +71 +00:02:50,000 --> 00:02:55,000 +So it should return authentication manager. + +72 +00:02:56,000 --> 00:02:59,000 +This is the object I want and I will have the same name. + +73 +00:02:59,000 --> 00:03:01,000 +How will I get this object? + +74 +00:03:01,000 --> 00:03:04,000 +So we have something called authentication + +75 +00:03:04,000 --> 00:03:06,000 +configuration, config. + +76 +00:03:06,000 --> 00:03:08,000 +And using this config object, + +77 +00:03:08,000 --> 00:03:10,000 +we can get the manager instance. + +78 +00:03:10,000 --> 00:03:12,000 +So it's there, the object is there, + +79 +00:03:12,000 --> 00:03:13,000 +you just have to hold it. + +80 +00:03:13,000 --> 00:03:15,000 +And once you create a bean you can hold it now, + +81 +00:03:15,000 --> 00:03:18,000 +but it is giving you an error. + +82 +00:03:18,000 --> 00:03:20,000 +It might throw an exception, okay, no problem. + +83 +00:03:20,000 --> 00:03:23,000 +I will just say exception, yes, those exception. + +84 +00:03:23,000 --> 00:03:26,000 +This will clear out, now once you've got this object, + +85 +00:03:26,000 --> 00:03:28,000 +I can use this object here. + +86 +00:03:28,000 --> 00:03:31,000 +And the way you can get the hold on it is + +87 +00:03:31,000 --> 00:03:35,000 +by saying auto wired authentication + +88 +00:03:35,000 --> 00:03:39,000 +Manager, manager, yeah. + +89 +00:03:39,000 --> 00:03:41,000 +So I got the hold on this object + +90 +00:03:41,000 --> 00:03:43,000 +and now I can just come back here. + +91 +00:03:43,000 --> 00:03:46,000 +I can say authentication manager dot. + +92 +00:03:46,000 --> 00:03:48,000 +There's a method called authenticate. + +93 +00:03:48,000 --> 00:03:50,000 +So this will authenticate, but how so to get the hold + +94 +00:03:50,000 --> 00:03:52,000 +of this authentication, + +95 +00:03:52,000 --> 00:03:57,000 +what we can do is we can create object of user password, + +96 +00:03:57,000 --> 00:03:58,000 +authentication token. + +97 +00:03:58,000 --> 00:04:00,000 +This is the actual thing which will do that. + +98 +00:04:00,000 --> 00:04:03,000 +And now in this it you have to pass two things. + +99 +00:04:03,000 --> 00:04:07,000 +Basically it is accepting a principle and the credentials. + +100 +00:04:07,000 --> 00:04:10,000 +So you just have to pass two things, the username, password, + +101 +00:04:10,000 --> 00:04:11,000 +and we do have it here. + +102 +00:04:11,000 --> 00:04:13,000 +So you can say user dot, get password, + +103 +00:04:13,000 --> 00:04:17,000 +and user dot, okay, we have done it in reverse. + +104 +00:04:17,000 --> 00:04:22,000 +It should be username first, get username and get password. + +105 +00:04:22,000 --> 00:04:24,000 +Okay, so you have to get a hold on + +106 +00:04:24,000 --> 00:04:25,000 +the authentication object. + +107 +00:04:25,000 --> 00:04:27,000 +And once it authenticates, + +108 +00:04:27,000 --> 00:04:30,000 +I can basically check if it's authenticated, + +109 +00:04:30,000 --> 00:04:32,000 +I can return success or I can return fail. + +110 +00:04:32,000 --> 00:04:36,000 +So I can check if authentication dot + +111 +00:04:36,000 --> 00:04:38,000 +is authenticated, is it? + +112 +00:04:38,000 --> 00:04:43,000 +If it is correct, return success + +113 +00:04:43,000 --> 00:04:47,000 +else, return failure. + +114 +00:04:48,000 --> 00:04:50,000 +Or I can say login fail, okay? + +115 +00:04:50,000 --> 00:04:51,000 +And then I'm just reading a string here, right? + +116 +00:04:51,000 --> 00:04:53,000 +So if it is authenticated success, + +117 +00:04:53,000 --> 00:04:55,000 +otherwise it failed, of course in the actual way, + +118 +00:04:55,000 --> 00:04:57,000 +we don't have to return success, + +119 +00:04:57,000 --> 00:04:58,000 +we have to return our token. + +120 +00:04:58,000 --> 00:04:59,000 +But again, we'll see, how do we get token later? + +121 +00:04:59,000 --> 00:05:03,000 +At this point, I'm just saying, do we get success? + +122 +00:05:03,000 --> 00:05:05,000 +So we'll just restart this application + +123 +00:05:05,000 --> 00:05:06,000 +and I hope this will work. + +124 +00:05:06,000 --> 00:05:08,000 +Let's try it out, okay? + +125 +00:05:08,000 --> 00:05:11,000 +Don't see it in the other, in the console when you start it. + +126 +00:05:11,000 --> 00:05:14,000 +That's good to see, go back here. + +127 +00:05:14,000 --> 00:05:16,000 +And this time we have to send the request for login + +128 +00:05:16,000 --> 00:05:18,000 +and let's send the different details. + +129 +00:05:18,000 --> 00:05:20,000 +I will send this time for a right username, + +130 +00:05:20,000 --> 00:05:21,000 +but wrong password. + +131 +00:05:21,000 --> 00:05:26,000 +And when you say send, okay, we got the error is forbidden. + +132 +00:05:26,000 --> 00:05:31,000 +Why I got the post mapping, this is correct. + +133 +00:05:31,000 --> 00:05:32,000 +No problem here. + +134 +00:05:32,000 --> 00:05:37,000 +As you can see, I'm sending a request for login post. + +135 +00:05:37,000 --> 00:05:39,000 +This time I'm checking the basic stuff. + +136 +00:05:39,000 --> 00:05:43,000 +And in the config also I'm saying login, which is ignored. + +137 +00:05:43,000 --> 00:05:45,000 +Am I providing an authorization, no. + +138 +00:05:45,000 --> 00:05:48,000 +In the body I'm sending this data. + +139 +00:05:48,000 --> 00:05:51,000 +If I send the right data, is it the issue, okay. + +140 +00:05:51,000 --> 00:05:52,000 +Oh yeah. + +141 +00:05:52,000 --> 00:05:54,000 +Okay, so basically you can see we got the success. + +142 +00:05:54,000 --> 00:05:58,000 +So there was something wrong with my written type here. + +143 +00:05:58,000 --> 00:06:01,000 +Instead of saying login failed, + +144 +00:06:01,000 --> 00:06:02,000 +I should send a different message + +145 +00:06:02,000 --> 00:06:04,000 +because login failed is not authenticated. + +146 +00:06:04,000 --> 00:06:06,000 +Okay, so now send the request for login + +147 +00:06:06,000 --> 00:06:07,000 +and that's what we're doing here. + +148 +00:06:07,000 --> 00:06:12,000 +And let's set the same data with the username and password + +149 +00:06:12,000 --> 00:06:15,000 +and let's try with wrong password. + +150 +00:06:15,000 --> 00:06:18,000 +And you can see we got forbidden. + +151 +00:06:18,000 --> 00:06:19,000 +So it is not working. + +152 +00:06:19,000 --> 00:06:23,000 +But if you give a right password, you got success. + +153 +00:06:23,000 --> 00:06:24,000 +This is what I want. + +154 +00:06:24,000 --> 00:06:26,000 +But again, we don't want success. + +155 +00:06:26,000 --> 00:06:27,000 +What we want is the token. + +156 +00:06:27,000 --> 00:06:29,000 +How do we generate token? + +157 +00:06:29,000 --> 00:06:30,000 +Let's see in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/007 Generating token_en.srt b/22 - JWT (JSON Web Token) and OAuth2/007 Generating token_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..c03e001818243498aa773937febf6be45a9eae3e --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/007 Generating token_en.srt @@ -0,0 +1,872 @@ +1 +00:00:00,000 --> 00:00:03,000 +(calm bright piano music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now, basically, we are able to log in + +3 +00:00:05,000 --> 00:00:08,000 +and, of course, we got a message called success. + +4 +00:00:08,000 --> 00:00:09,000 +But then the aim is different, right? + +5 +00:00:09,000 --> 00:00:11,000 +We don't want success on the page. + +6 +00:00:11,000 --> 00:00:16,000 +What we want in the output or the client is the token. + +7 +00:00:16,000 --> 00:00:18,000 +So when I say log in, give me a token, + +8 +00:00:18,000 --> 00:00:20,000 +so that whenever I go to any API, + +9 +00:00:20,000 --> 00:00:23,000 +any particular URL in the application, + +10 +00:00:23,000 --> 00:00:26,000 +I should be able to get the service. + +11 +00:00:26,000 --> 00:00:28,000 +And, of course, for that, I have to send the token + +12 +00:00:28,000 --> 00:00:30,000 +from a client to server. + +13 +00:00:30,000 --> 00:00:31,000 +Okay, but then we are not generating token now. + +14 +00:00:31,000 --> 00:00:33,000 +So in this video, let's try to see + +15 +00:00:33,000 --> 00:00:35,000 +how do we generate a token + +16 +00:00:35,000 --> 00:00:38,000 +and specifically, we're talking about the JWT token. + +17 +00:00:38,000 --> 00:00:39,000 +So here, instead of returning a success, + +18 +00:00:39,000 --> 00:00:41,000 +I want to return a token, right? + +19 +00:00:41,000 --> 00:00:43,000 +So what I will do is let me create + +20 +00:00:43,000 --> 00:00:45,000 +an object of token and return. + +21 +00:00:45,000 --> 00:00:48,000 +So maybe for that, what I will do is I will call a method. + +22 +00:00:48,000 --> 00:00:49,000 +So instead of returning a success here, + +23 +00:00:49,000 --> 00:00:52,000 +so there should not be a success, I will just remove this + +24 +00:00:52,000 --> 00:00:56,000 +and say return, let's say I want to say generate token. + +25 +00:00:56,000 --> 00:00:59,000 +And then this token belongs to a particular username, right? + +26 +00:00:59,000 --> 00:01:01,000 +Of course, different users who when they log in, + +27 +00:01:01,000 --> 00:01:03,000 +they will get a different token. + +28 +00:01:03,000 --> 00:01:05,000 +So here, I want the username as well. + +29 +00:01:05,000 --> 00:01:07,000 +So I will say user.getUsername. + +30 +00:01:07,000 --> 00:01:10,000 +So based on this username, I want to generate a token. + +31 +00:01:10,000 --> 00:01:11,000 +That's what I want. + +32 +00:01:11,000 --> 00:01:13,000 +Okay, but then we don't have this method. + +33 +00:01:13,000 --> 00:01:15,000 +So we have to create this method and where? + +34 +00:01:15,000 --> 00:01:16,000 +Not in this particular class, + +35 +00:01:16,000 --> 00:01:18,000 +because this is a controller, it should be done + +36 +00:01:18,000 --> 00:01:22,000 +in a service and then which service? + +37 +00:01:22,000 --> 00:01:24,000 +So basically, I want to create my own service class, + +38 +00:01:24,000 --> 00:01:27,000 +but specifically for JWT. + +39 +00:01:27,000 --> 00:01:28,000 +So what I will do is I will just right click here. + +40 +00:01:28,000 --> 00:01:31,000 +I will create a new class, + +41 +00:01:31,000 --> 00:01:36,000 +and I'll name this class as JWT service. + +42 +00:01:36,000 --> 00:01:38,000 +So this class is responsible to have a logic + +43 +00:01:38,000 --> 00:01:40,000 +for JWT token generation. + +44 +00:01:40,000 --> 00:01:41,000 +So I will create this class. + +45 +00:01:41,000 --> 00:01:42,000 +And in this class, I want to have + +46 +00:01:42,000 --> 00:01:44,000 +that method call generate. + +47 +00:01:44,000 --> 00:01:47,000 +Okay, first of all, I will make this class as service, + +48 +00:01:47,000 --> 00:01:49,000 +so that in the controller, + +49 +00:01:49,000 --> 00:01:50,000 +you can create one more here. + +50 +00:01:50,000 --> 00:01:53,000 +We'll say Autowired. + +51 +00:01:53,000 --> 00:01:57,000 +And this is for private JWT service. + +52 +00:01:57,000 --> 00:02:00,000 +I will say JWT service. + +53 +00:02:00,000 --> 00:02:02,000 +Okay, so once I got this Autowired, + +54 +00:02:02,000 --> 00:02:05,000 +I can go back to my code here + +55 +00:02:05,000 --> 00:02:08,000 +and say jwtService.generateToken. + +56 +00:02:08,000 --> 00:02:10,000 +So this particular method will be a part + +57 +00:02:10,000 --> 00:02:12,000 +of this particular class, which is JWT service. + +58 +00:02:12,000 --> 00:02:15,000 +Now, we don't have it yet, so we'll just go back here + +59 +00:02:15,000 --> 00:02:18,000 +and say, hey, create a method call generateToken there. + +60 +00:02:18,000 --> 00:02:20,000 +And you can see we got a method, + +61 +00:02:20,000 --> 00:02:22,000 +but of course, this is not doing anything. + +62 +00:02:22,000 --> 00:02:24,000 +Of course, I can return any token data here, + +63 +00:02:24,000 --> 00:02:26,000 +but that should be a valid token, right? + +64 +00:02:26,000 --> 00:02:29,000 +And we have seen what exactly what a token means. + +65 +00:02:29,000 --> 00:02:31,000 +It should have a header then dot, + +66 +00:02:31,000 --> 00:02:35,000 +then it should have the payload dot and the signature. + +67 +00:02:35,000 --> 00:02:37,000 +So basically, we have to encode that, + +68 +00:02:37,000 --> 00:02:39,000 +and that's a format we want. + +69 +00:02:39,000 --> 00:02:40,000 +Okay, how do we do that? + +70 +00:02:40,000 --> 00:02:42,000 +See, when you say you want to generate a token, + +71 +00:02:42,000 --> 00:02:44,000 +you have to use those, all those encoding technique, + +72 +00:02:44,000 --> 00:02:47,000 +don't you think we should be doing that with some library? + +73 +00:02:47,000 --> 00:02:48,000 +You can do it by yourself, + +74 +00:02:48,000 --> 00:02:50,000 +but then that will be a huge code. + +75 +00:02:50,000 --> 00:02:53,000 +We have to use some library to generate the token. + +76 +00:02:53,000 --> 00:02:56,000 +And also, we have to use a library for verifying the token. + +77 +00:02:56,000 --> 00:02:58,000 +Okay, so now, there are multiple libraries available. + +78 +00:02:58,000 --> 00:03:00,000 +We are going use the simplest one. + +79 +00:03:00,000 --> 00:03:01,000 +Now, which one I'm talking about? + +80 +00:03:01,000 --> 00:03:04,000 +Let me add that in the pom.xml. + +81 +00:03:04,000 --> 00:03:09,000 +So this is where maybe after all this Postgres, + +82 +00:03:09,000 --> 00:03:12,000 +I will just put that after dev tool. + +83 +00:03:12,000 --> 00:03:12,000 +That makes sense. + +84 +00:03:12,000 --> 00:03:15,000 +So let me specify the dependency here. + +85 +00:03:15,000 --> 00:03:18,000 +Okay, now, you can get this dependency from MVN repository. + +86 +00:03:18,000 --> 00:03:19,000 +So you can go to MVN repository + +87 +00:03:19,000 --> 00:03:21,000 +and search for this particular dependency + +88 +00:03:21,000 --> 00:03:23,000 +and you can get it, but I already have with me, + +89 +00:03:23,000 --> 00:03:25,000 +so let me just use that. + +90 +00:03:25,000 --> 00:03:27,000 +Okay, so this is the library which you have to use, + +91 +00:03:27,000 --> 00:03:30,000 +which is jjwt-api. + +92 +00:03:30,000 --> 00:03:33,000 +This is the library which we're going to use. + +93 +00:03:33,000 --> 00:03:35,000 +As the name suggests, this is just an API. + +94 +00:03:35,000 --> 00:03:37,000 +We have to find the implementation as well. + +95 +00:03:37,000 --> 00:03:38,000 +And there are multiple implementation. + +96 +00:03:38,000 --> 00:03:41,000 +We are going to use the implementation. + +97 +00:03:41,000 --> 00:03:43,000 +So we have to add one more dependency + +98 +00:03:43,000 --> 00:03:45,000 +for the implementation of this API. + +99 +00:03:45,000 --> 00:03:46,000 +And that is this. + +100 +00:03:46,000 --> 00:03:47,000 +So basically, the same thing, + +101 +00:03:47,000 --> 00:03:49,000 +but then it says implementation. + +102 +00:03:49,000 --> 00:03:50,000 +So we have to get the API, + +103 +00:03:50,000 --> 00:03:52,000 +we have to get the implementation. + +104 +00:03:52,000 --> 00:03:54,000 +And sometime it may not work + +105 +00:03:54,000 --> 00:03:56,000 +because for the JSON conversion. + +106 +00:03:56,000 --> 00:03:58,000 +So to be on the safe side, + +107 +00:03:58,000 --> 00:03:59,000 +I'm going to add one more dependency. + +108 +00:03:59,000 --> 00:04:02,000 +So you try out without that dependency + +109 +00:04:02,000 --> 00:04:03,000 +and if it works, that's great. + +110 +00:04:03,000 --> 00:04:05,000 +Otherwise, just add the dependency. + +111 +00:04:05,000 --> 00:04:07,000 +I got some exceptions, so I'm just adding it here. + +112 +00:04:07,000 --> 00:04:08,000 +So this is the dependency we have to add. + +113 +00:04:08,000 --> 00:04:11,000 +So in total, we added three dependencies. + +114 +00:04:11,000 --> 00:04:16,000 +One is the API, so jjwt api, + +115 +00:04:16,000 --> 00:04:19,000 +jjwt implementation, and jjwt jackson. + +116 +00:04:19,000 --> 00:04:20,000 +So there are so many Js here. + +117 +00:04:20,000 --> 00:04:22,000 +Okay, so we have to get these three things. + +118 +00:04:22,000 --> 00:04:25,000 +And once you are done, just reload your maven. + +119 +00:04:25,000 --> 00:04:27,000 +That's important, so I'm gonna reload this, + +120 +00:04:27,000 --> 00:04:28,000 +taking some time. + +121 +00:04:28,000 --> 00:04:32,000 +Okay, once that is done, go back to your JWT service, + +122 +00:04:32,000 --> 00:04:34,000 +and we have to use that library to generate this token. + +123 +00:04:34,000 --> 00:04:35,000 +Okay, so what are the things we need? + +124 +00:04:35,000 --> 00:04:37,000 +So, of course, I can type the entire code, + +125 +00:04:37,000 --> 00:04:39,000 +but then it will be a very lengthy video + +126 +00:04:39,000 --> 00:04:40,000 +if I type the entire code. + +127 +00:04:40,000 --> 00:04:42,000 +So somewhere I will just use the code + +128 +00:04:42,000 --> 00:04:44,000 +which I have with me, copy, paste. + +129 +00:04:44,000 --> 00:04:45,000 +And these are standard things, right? + +130 +00:04:45,000 --> 00:04:48,000 +So whenever you try to implement JWT in your code, + +131 +00:04:48,000 --> 00:04:50,000 +in your project, you will do that only once. + +132 +00:04:50,000 --> 00:04:52,000 +So all the code, of course, I will explain them, + +133 +00:04:52,000 --> 00:04:55,000 +but then all the code will be happening only once. + +134 +00:04:55,000 --> 00:04:57,000 +Okay, so don't worry about changing those code. + +135 +00:04:57,000 --> 00:05:00,000 +Yes, the setup will take, I mean, there are a lot + +136 +00:05:00,000 --> 00:05:02,000 +of lines of code, which you have to mention, + +137 +00:05:02,000 --> 00:05:04,000 +but then you have to reuse that only once. + +138 +00:05:04,000 --> 00:05:05,000 +So the first thing you will do + +139 +00:05:05,000 --> 00:05:07,000 +is when you say generate token, + +140 +00:05:07,000 --> 00:05:10,000 +now, this token will be having some claims as well. + +141 +00:05:10,000 --> 00:05:11,000 +Now, what do you mean by claims? + +142 +00:05:11,000 --> 00:05:15,000 +So your payload will have some data about the user. + +143 +00:05:15,000 --> 00:05:16,000 +That is your claims. + +144 +00:05:16,000 --> 00:05:19,000 +A claim will have a username, maybe expiry date, + +145 +00:05:19,000 --> 00:05:21,000 +time efficiency, and all those details. + +146 +00:05:21,000 --> 00:05:23,000 +And we have to get those claims. + +147 +00:05:23,000 --> 00:05:25,000 +And since you'll be having multiple claims, + +148 +00:05:25,000 --> 00:05:27,000 +let's create a map for those claims, + +149 +00:05:27,000 --> 00:05:29,000 +and we'll say the key will be string, + +150 +00:05:29,000 --> 00:05:34,000 +and the value will be Object equal to new HashMap. + +151 +00:05:34,000 --> 00:05:36,000 +Okay, so once we got this claim, we have to create object, + +152 +00:05:36,000 --> 00:05:39,000 +or we have to give a reference as well, so that's claim. + +153 +00:05:39,000 --> 00:05:40,000 +And once you got this claim, + +154 +00:05:40,000 --> 00:05:43,000 +it is empty at this point, but we'll add some claims there. + +155 +00:05:43,000 --> 00:05:45,000 +Okay, so we have to generate a token + +156 +00:05:45,000 --> 00:05:46,000 +and written a string, right? + +157 +00:05:46,000 --> 00:05:51,000 +So I will say return, and we have to use a JWTS. + +158 +00:05:51,000 --> 00:05:54,000 +So to help me with the token, I will be using JWTS. + +159 +00:05:54,000 --> 00:05:56,000 +So you can see it is coming from this particular library, + +160 +00:05:56,000 --> 00:05:58,000 +which is JSON web token. + +161 +00:05:58,000 --> 00:06:01,000 +And I will say enter builder dot. + +162 +00:06:01,000 --> 00:06:02,000 +Now, it has multiple methods. + +163 +00:06:02,000 --> 00:06:04,000 +So, first of all, we have to set the claims. + +164 +00:06:04,000 --> 00:06:06,000 +So I will say set claims, + +165 +00:06:06,000 --> 00:06:07,000 +and we have to set these claims + +166 +00:06:07,000 --> 00:06:09,000 +which is the empty map at this point. + +167 +00:06:09,000 --> 00:06:12,000 +Then we are going to set the subject, + +168 +00:06:12,000 --> 00:06:15,000 +so which is here, the subject is the username. + +169 +00:06:15,000 --> 00:06:18,000 +So this subject represents the person in this case. + +170 +00:06:18,000 --> 00:06:20,000 +Then we have to set the issued at. + +171 +00:06:20,000 --> 00:06:22,000 +So we have to use this method, + +172 +00:06:22,000 --> 00:06:23,000 +and I'm going to use the current time. + +173 +00:06:23,000 --> 00:06:25,000 +So I'm going to say new date + +174 +00:06:25,000 --> 00:06:29,000 +and in which we have to pass System.currentTimeMillis, + +175 +00:06:29,000 --> 00:06:31,000 +so we have to pass the milliseconds + +176 +00:06:31,000 --> 00:06:32,000 +which will convert into date. + +177 +00:06:32,000 --> 00:06:35,000 +Then we have to set the expiration time, + +178 +00:06:35,000 --> 00:06:37,000 +and I will say new date. + +179 +00:06:37,000 --> 00:06:39,000 +So maybe I have to give some extra time for that. + +180 +00:06:39,000 --> 00:06:41,000 +So it's the current time millis, + +181 +00:06:41,000 --> 00:06:44,000 +and we just have to add some extra time here. + +182 +00:06:44,000 --> 00:06:47,000 +So I would say thousand seconds, I mean, + +183 +00:06:47,000 --> 00:06:52,000 +so millis in 2,000 seconds into 60, this number of minutes. + +184 +00:06:53,000 --> 00:06:54,000 +So that's one minute, and I will say three. + +185 +00:06:54,000 --> 00:06:57,000 +So somewhere, three minutes. + +186 +00:06:57,000 --> 00:06:58,000 +Now, once you have added all the details, + +187 +00:06:58,000 --> 00:07:00,000 +you can see we have set the claims, we have set the subject, + +188 +00:07:00,000 --> 00:07:03,000 +it showed at expiration, + +189 +00:07:03,000 --> 00:07:05,000 +and now, it's time to add the signature. + +190 +00:07:05,000 --> 00:07:08,000 +So basically, the header will have the type + +191 +00:07:08,000 --> 00:07:09,000 +of algorithm which you're going to use, + +192 +00:07:09,000 --> 00:07:11,000 +but also we need to add a signature. + +193 +00:07:11,000 --> 00:07:15,000 +So I will say signed with, and now we have to pass a key. + +194 +00:07:15,000 --> 00:07:17,000 +So it basically takes two parameters, if you can see. + +195 +00:07:17,000 --> 00:07:21,000 +One is the key, and second is the signature. + +196 +00:07:21,000 --> 00:07:23,000 +This is what I want to use. + +197 +00:07:23,000 --> 00:07:24,000 +So the key, how will you get the key? + +198 +00:07:24,000 --> 00:07:25,000 +So maybe, I will have a method + +199 +00:07:25,000 --> 00:07:27,000 +which will generate a key for me. + +200 +00:07:27,000 --> 00:07:28,000 +So I will say getKey, + +201 +00:07:28,000 --> 00:07:30,000 +and then we have to specify the algorithm as well. + +202 +00:07:30,000 --> 00:07:33,000 +And to specify that, I'm going to use signature. + +203 +00:07:33,000 --> 00:07:34,000 +Okay, just need to check the name. + +204 +00:07:34,000 --> 00:07:36,000 +Signature, algorithm, okay? + +205 +00:07:36,000 --> 00:07:39,000 +Dot, and we have multiple algorithm option here, + +206 +00:07:39,000 --> 00:07:42,000 +and we are going to use HS256. + +207 +00:07:42,000 --> 00:07:44,000 +And once that everything is done, + +208 +00:07:44,000 --> 00:07:48,000 +you will simply say compact, so it will give you the token. + +209 +00:07:48,000 --> 00:07:50,000 +But then we don't have this method which is getKey. + +210 +00:07:50,000 --> 00:07:51,000 +Again, we'll do that. + +211 +00:07:51,000 --> 00:07:55,000 +But then what we have done is we are generating a token + +212 +00:07:55,000 --> 00:07:56,000 +by setting all these details. + +213 +00:07:56,000 --> 00:07:59,000 +After that, we are signing it with this algorithm + +214 +00:07:59,000 --> 00:08:01,000 +and this key, of course, this key is important. + +215 +00:08:01,000 --> 00:08:03,000 +And we have not worked with the key yet. + +216 +00:08:03,000 --> 00:08:05,000 +So basically this is what will generate the token, + +217 +00:08:05,000 --> 00:08:07,000 +but then how do we define the key? + +218 +00:08:07,000 --> 00:08:09,000 +Let's do that in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/008 Token Generated_en.srt b/22 - JWT (JSON Web Token) and OAuth2/008 Token Generated_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..d6c917cc5345a98813e56506e9ca6238bd0cc92a --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/008 Token Generated_en.srt @@ -0,0 +1,596 @@ +1 +00:00:00,000 --> 00:00:02,000 +(upbeat music) + +2 +00:00:02,000 --> 00:00:04,000 +-: Okay, so let's see. + +3 +00:00:04,000 --> 00:00:05,000 +How do we generate this key? + +4 +00:00:05,000 --> 00:00:07,000 +So we have to get this key, right, get key. + +5 +00:00:07,000 --> 00:00:09,000 +For that, let me create a method, the same class. + +6 +00:00:09,000 --> 00:00:11,000 +So we'll just go back here + +7 +00:00:11,000 --> 00:00:13,000 +and say, create a method, get key, + +8 +00:00:13,000 --> 00:00:16,000 +and we've got the method which would return a key, + +9 +00:00:16,000 --> 00:00:17,000 +not the signature. + +10 +00:00:17,000 --> 00:00:20,000 +So we'll say key. + +11 +00:00:20,000 --> 00:00:21,000 +And you can see this is coming from other package, + +12 +00:00:21,000 --> 00:00:24,000 +which is Java.security.key. + +13 +00:00:24,000 --> 00:00:28,000 +Okay, now, how do we do this, so we'll return a key. + +14 +00:00:28,000 --> 00:00:32,000 +And to do that we have a class called keys, + +15 +00:00:32,000 --> 00:00:37,000 +in which you have a method called the hmacShaKeyFor + +16 +00:00:39,000 --> 00:00:41,000 +Okay, so we have to basically use this method + +17 +00:00:41,000 --> 00:00:43,000 +to generate the key, but then this will ask you + +18 +00:00:43,000 --> 00:00:45,000 +for a secret key. + +19 +00:00:45,000 --> 00:00:46,000 +So of course it'll sign it, + +20 +00:00:46,000 --> 00:00:48,000 +it'll generate a key, which it needs it. + +21 +00:00:48,000 --> 00:00:50,000 +But then you have to pass a secret message, + +22 +00:00:50,000 --> 00:00:52,000 +some included message, okay? + +23 +00:00:52,000 --> 00:00:54,000 +And to do that, you can say, + +24 +00:00:54,000 --> 00:00:57,000 +this is asking you for the byte string. + +25 +00:00:57,000 --> 00:00:59,000 +So what I will do is I will return a byte, keyBytes, + +26 +00:00:59,000 --> 00:01:03,000 +which is going to be passed here, keyBytes. + +27 +00:01:03,000 --> 00:01:06,000 +And so basically we have to get these bytes + +28 +00:01:06,000 --> 00:01:08,000 +by decoding a secret key. + +29 +00:01:08,000 --> 00:01:09,000 +Now, where do we get the secret key? + +30 +00:01:09,000 --> 00:01:12,000 +Of course, you can directly pass a message, + +31 +00:01:12,000 --> 00:01:17,000 +you can convert a text, a string into a byte. + +32 +00:01:17,000 --> 00:01:19,000 +But then you do want to hide it in the code. + +33 +00:01:19,000 --> 00:01:20,000 +And how do we get the secret key? + +34 +00:01:20,000 --> 00:01:23,000 +So what I will do is I will just go back here. + +35 +00:01:23,000 --> 00:01:26,000 +Of course there are multiple ways of creating this key. + +36 +00:01:26,000 --> 00:01:27,000 +One, you can hardcode the key + +37 +00:01:27,000 --> 00:01:30,000 +by getting it from the internet somewhere, + +38 +00:01:30,000 --> 00:01:33,000 +or you can use the method which is available + +39 +00:01:33,000 --> 00:01:35,000 +to generate a random key. + +40 +00:01:35,000 --> 00:01:36,000 +Example, something like this. + +41 +00:01:36,000 --> 00:01:38,000 +Maybe this is a hardcoded key, which you have. + +42 +00:01:38,000 --> 00:01:43,000 +And you can use this cigarette key here and it'll work. + +43 +00:01:43,000 --> 00:01:45,000 +So basically I can use cigarette here. + +44 +00:01:45,000 --> 00:01:46,000 +So this will get converted into bytes. + +45 +00:01:46,000 --> 00:01:49,000 +And then using those bytes, it'll generate a key for me, + +46 +00:01:49,000 --> 00:01:51,000 +which I'm going to use here. + +47 +00:01:51,000 --> 00:01:53,000 +Look so simple, right, but then you are basically + +48 +00:01:53,000 --> 00:01:56,000 +hardcoding a key here. + +49 +00:01:56,000 --> 00:01:58,000 +We don't want that, another approach is you can use some + +50 +00:01:58,000 --> 00:02:01,000 +methods to do that, something like this. + +51 +00:02:01,000 --> 00:02:04,000 +So you can see I'm creating a method called generate secret + +52 +00:02:04,000 --> 00:02:06,000 +key, which basically uses a key generator + +53 +00:02:06,000 --> 00:02:08,000 +for this particular algorithm. + +54 +00:02:08,000 --> 00:02:10,000 +And it generates the key. + +55 +00:02:10,000 --> 00:02:13,000 +And then, I mean, I'm also printing the key here. + +56 +00:02:13,000 --> 00:02:16,000 +And then that's get encoded. + +57 +00:02:16,000 --> 00:02:17,000 +Now here we are basically decoding it, + +58 +00:02:17,000 --> 00:02:19,000 +and then the process, + +59 +00:02:19,000 --> 00:02:22,000 +maybe I can even return the key from here if I want. + +60 +00:02:23,000 --> 00:02:25,000 +As I mentioned, there are different ways of doing this. + +61 +00:02:25,000 --> 00:02:27,000 +So either you can use this security key, + +62 +00:02:27,000 --> 00:02:30,000 +which is mentioned here, or you can use the code here. + +63 +00:02:30,000 --> 00:02:32,000 +Both works, it's just that you have + +64 +00:02:32,000 --> 00:02:34,000 +to call this method to generate the key. + +65 +00:02:34,000 --> 00:02:37,000 +So maybe I will just say private string, secret key, + +66 +00:02:42,000 --> 00:02:45,000 +and I will keep it empty here. + +67 +00:02:45,000 --> 00:02:47,000 +And then I will use a constructor in which I'm going + +68 +00:02:47,000 --> 00:02:52,000 +to specify this secret key equal to generate secret key. + +69 +00:02:52,000 --> 00:02:54,000 +Now, this is a key I can use here. + +70 +00:02:54,000 --> 00:02:56,000 +So either you can use this secret here + +71 +00:02:56,000 --> 00:02:57,000 +or you can use it here. + +72 +00:02:57,000 --> 00:02:58,000 +I'm giving you a choice, + +73 +00:02:58,000 --> 00:03:01,000 +so you can just replace this particular secret key + +74 +00:03:01,000 --> 00:03:02,000 +with the secret key, + +75 +00:03:02,000 --> 00:03:05,000 +and that's how you can generate this key here. + +76 +00:03:05,000 --> 00:03:07,000 +Okay, so at this point, we have created a token + +77 +00:03:07,000 --> 00:03:10,000 +with the help of key, but will this work? + +78 +00:03:10,000 --> 00:03:13,000 +So looks like it should generate a token for me. + +79 +00:03:13,000 --> 00:03:14,000 +Let me just check that. + +80 +00:03:14,000 --> 00:03:17,000 +So I will just relaunch this application, okay, no problem. + +81 +00:03:17,000 --> 00:03:20,000 +In the application, I will go back to my Postman. + +82 +00:03:20,000 --> 00:03:24,000 +So now let's try to do the register user, + +83 +00:03:24,000 --> 00:03:25,000 +a new user this time. + +84 +00:03:25,000 --> 00:03:30,000 +And I will say, let's say I will go with harsh and h@123. + +85 +00:03:31,000 --> 00:03:34,000 +So let me create a new user, okay? + +86 +00:03:34,000 --> 00:03:37,000 +Forbidden, I'm using, okay, so again, + +87 +00:03:37,000 --> 00:03:39,000 +I'm doing the same mistake. + +88 +00:03:39,000 --> 00:03:42,000 +It should not be get, it should be post. + +89 +00:03:42,000 --> 00:03:46,000 +I don't want to send any OAuth send, okay, it worked. + +90 +00:03:46,000 --> 00:03:49,000 +You can see we got the user, + +91 +00:03:49,000 --> 00:03:51,000 +we got a new user called harsh + +92 +00:03:51,000 --> 00:03:53,000 +and we'll use this data now. + +93 +00:03:53,000 --> 00:03:55,000 +So I just want to use this data for the login. + +94 +00:03:55,000 --> 00:03:57,000 +I will just go back here for the login. + +95 +00:03:57,000 --> 00:04:01,000 +Okay, body, a new user is harsh. + +96 +00:04:01,000 --> 00:04:04,000 +Click on send, login, done. + +97 +00:04:04,000 --> 00:04:07,000 +And you can see we got a response, which is a token. + +98 +00:04:07,000 --> 00:04:10,000 +So this time it is not saying success, + +99 +00:04:10,000 --> 00:04:11,000 +it is actually giving you a token. + +100 +00:04:11,000 --> 00:04:14,000 +Can we verify, is it correct? + +101 +00:04:14,000 --> 00:04:16,000 +Let me just copy this, go to my browser, okay? + +102 +00:04:16,000 --> 00:04:18,000 +And let's go to jwt.io. + +103 +00:04:18,000 --> 00:04:21,000 +I will just paste this new token which got created. + +104 +00:04:21,000 --> 00:04:23,000 +And voila, can you see that, it says algorithm is this. + +105 +00:04:23,000 --> 00:04:28,000 +These are the details, we got harsh, we got the IAT, + +106 +00:04:28,000 --> 00:04:31,000 +which is this time, if you can see this is 1738, + +107 +00:04:31,000 --> 00:04:32,000 +which is 5.38. + +108 +00:04:32,000 --> 00:04:34,000 +And the ending time is 41. + +109 +00:04:34,000 --> 00:04:36,000 +So we got a three minutes + +110 +00:04:36,000 --> 00:04:37,000 +for this particular token to use. + +111 +00:04:37,000 --> 00:04:39,000 +And that's your signature. + +112 +00:04:39,000 --> 00:04:40,000 +So yeah, this is the right token, + +113 +00:04:40,000 --> 00:04:42,000 +but now the question arise next time. + +114 +00:04:42,000 --> 00:04:44,000 +I mean we got the token, + +115 +00:04:44,000 --> 00:04:46,000 +but we have to keep this token somewhere. + +116 +00:04:46,000 --> 00:04:47,000 +I will just preserve it somewhere. + +117 +00:04:47,000 --> 00:04:50,000 +Or maybe we'll generate the new token next time. + +118 +00:04:50,000 --> 00:04:52,000 +But now what I wanna do is now when I want + +119 +00:04:52,000 --> 00:04:55,000 +to access a particular URL, so let's say if I go + +120 +00:04:55,000 --> 00:04:57,000 +to my hello controller for this particular method, I want + +121 +00:04:57,000 --> 00:05:00,000 +to send this request for, hello, I want to call this, + +122 +00:05:00,000 --> 00:05:02,000 +but this is of course, this is a protected resource. + +123 +00:05:02,000 --> 00:05:06,000 +If I want to access this, I have to provide the credentials, + +124 +00:05:06,000 --> 00:05:09,000 +but this time I'm going to pass a token. + +125 +00:05:09,000 --> 00:05:11,000 +So every time you send a request, send the token + +126 +00:05:11,000 --> 00:05:13,000 +and it is very easy to send. + +127 +00:05:13,000 --> 00:05:15,000 +Let me show you, I'll just go back to my Postman + +128 +00:05:15,000 --> 00:05:19,000 +and I will send the request for hello, the get request. + +129 +00:05:19,000 --> 00:05:23,000 +And if I click on send, you can see it says forbidden. + +130 +00:05:23,000 --> 00:05:26,000 +But then you have to send the token as well. + +131 +00:05:26,000 --> 00:05:28,000 +So we just go back here. + +132 +00:05:28,000 --> 00:05:30,000 +I will say this is a token + +133 +00:05:30,000 --> 00:05:34,000 +and just pass the token here, whatever the new token we got. + +134 +00:05:34,000 --> 00:05:36,000 +Am I pasting the right token, I'm not sure. + +135 +00:05:36,000 --> 00:05:37,000 +I have not even copied it. + +136 +00:05:37,000 --> 00:05:42,000 +So let me copy the token, I forgot to do the step, + +137 +00:05:43,000 --> 00:05:46,000 +copy, done, let me go back to hello in this. + +138 +00:05:46,000 --> 00:05:49,000 +So when we sent a hello request, you have to say + +139 +00:05:49,000 --> 00:05:53,000 +a better token, and here paste the new token, + +140 +00:05:53,000 --> 00:05:55,000 +which we created and click on send. + +141 +00:05:55,000 --> 00:05:57,000 +But it's still not working, you know why? + +142 +00:05:57,000 --> 00:05:58,000 +We are sending a token, + +143 +00:05:58,000 --> 00:06:01,000 +but on the server side, we are not doing anything + +144 +00:06:01,000 --> 00:06:04,000 +to verify this token or validate this token. + +145 +00:06:04,000 --> 00:06:07,000 +How do we do that, let's say in the upcoming videos. + +146 +00:06:07,000 --> 00:06:10,000 +But at this point we are able to generate a token. + +147 +00:06:10,000 --> 00:06:11,000 +So first step or second test step is done. + +148 +00:06:11,000 --> 00:06:12,000 +Third step is validating. + +149 +00:06:12,000 --> 00:06:14,000 +We'll see that in the upcoming videos. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/009 Creating a JWT Filter_en.srt b/22 - JWT (JSON Web Token) and OAuth2/009 Creating a JWT Filter_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..357a4885f7f92ca74bddb78a91c71e52afb03225 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/009 Creating a JWT Filter_en.srt @@ -0,0 +1,872 @@ +1 +00:00:00,000 --> 00:00:03,000 +(upbeat music) + +2 +00:00:03,000 --> 00:00:06,000 +-: So till now we are able to log in. + +3 +00:00:06,000 --> 00:00:09,000 +And then when you log in you also get a token, right? + +4 +00:00:09,000 --> 00:00:11,000 +And we have seen that in the last video. + +5 +00:00:11,000 --> 00:00:14,000 +So basically when you send a request for the login + +6 +00:00:14,000 --> 00:00:18,000 +and once you do a login, you do get a response as a token. + +7 +00:00:18,000 --> 00:00:21,000 +And then we tried to use that token to greet, I mean + +8 +00:00:21,000 --> 00:00:23,000 +to basically when you hit the url, which is, hello, + +9 +00:00:23,000 --> 00:00:26,000 +I'm sending the token, if you can see, + +10 +00:00:26,000 --> 00:00:27,000 +I'm sending a token here. + +11 +00:00:27,000 --> 00:00:29,000 +And of course I've just restarted the application. + +12 +00:00:29,000 --> 00:00:31,000 +So maybe I have to use a new token. + +13 +00:00:31,000 --> 00:00:34,000 +And also I'm recording this after a long time. + +14 +00:00:34,000 --> 00:00:36,000 +So the three minutes expire has done. + +15 +00:00:36,000 --> 00:00:39,000 +So if you use the same token, of course this will not work + +16 +00:00:39,000 --> 00:00:40,000 +and the server is not even on. + +17 +00:00:40,000 --> 00:00:42,000 +So let's do that. + +18 +00:00:42,000 --> 00:00:44,000 +First of all, let's start our server. + +19 +00:00:44,000 --> 00:00:45,000 +But even if you start the server, + +20 +00:00:45,000 --> 00:00:48,000 +the problem is you are basically sending a token, + +21 +00:00:48,000 --> 00:00:50,000 +but on the server side, we are doing nothing + +22 +00:00:50,000 --> 00:00:52,000 +to verify the token. + +23 +00:00:52,000 --> 00:00:54,000 +Of course we want to give the resources, + +24 +00:00:54,000 --> 00:00:56,000 +but then we have to verify the token first + +25 +00:00:56,000 --> 00:00:59,000 +and no way the server knows that we have + +26 +00:00:59,000 --> 00:01:03,000 +to basically use JW token to verify and to give the access. + +27 +00:01:03,000 --> 00:01:05,000 +So let's do that in this video. + +28 +00:01:05,000 --> 00:01:07,000 +First of all, let me just relaunch this application. + +29 +00:01:07,000 --> 00:01:12,000 +We'll take some time, but I will just make it fast and cool. + +30 +00:01:12,000 --> 00:01:13,000 +So server has started + +31 +00:01:13,000 --> 00:01:17,000 +and now let's hit the login with the post request. + +32 +00:01:17,000 --> 00:01:19,000 +And I'm sending a login for harsh + +33 +00:01:19,000 --> 00:01:22,000 +and we do have this account in the database. + +34 +00:01:22,000 --> 00:01:23,000 +I will say send. + +35 +00:01:23,000 --> 00:01:25,000 +And once you do that, you will see you, + +36 +00:01:25,000 --> 00:01:26,000 +you can, you got a token here. + +37 +00:01:26,000 --> 00:01:29,000 +I will just copy this token and let's use this. + +38 +00:01:29,000 --> 00:01:33,000 +I will go back to, hello URL, the get request and auth. + +39 +00:01:33,000 --> 00:01:37,000 +We have to select bearer here and let's use a new token + +40 +00:01:37,000 --> 00:01:38,000 +because the earlier token is expired. + +41 +00:01:38,000 --> 00:01:40,000 +If you see I'm using a old token, + +42 +00:01:40,000 --> 00:01:41,000 +if I say send, it's not working. + +43 +00:01:41,000 --> 00:01:44,000 +Even the new token will not work. + +44 +00:01:44,000 --> 00:01:46,000 +It's because we are not verifying the server. + +45 +00:01:46,000 --> 00:01:49,000 +See, by default, when you send a request, + +46 +00:01:49,000 --> 00:01:53,000 +there is a way your spring security verifies + +47 +00:01:53,000 --> 00:01:54,000 +the credentials. + +48 +00:01:54,000 --> 00:01:56,000 +It uses something called a username, + +49 +00:01:56,000 --> 00:01:58,000 +password authentication. + +50 +00:01:58,000 --> 00:02:00,000 +And we don't want to use that. + +51 +00:02:00,000 --> 00:02:03,000 +So we want to use our own technique. + +52 +00:02:03,000 --> 00:02:05,000 +So what we have to do is we have + +53 +00:02:05,000 --> 00:02:07,000 +to basically add a filter in between. + +54 +00:02:07,000 --> 00:02:10,000 +See what happens is whenever we use something + +55 +00:02:10,000 --> 00:02:12,000 +like spring framework here + +56 +00:02:12,000 --> 00:02:15,000 +for the web behind the scene, it is all servlet anyway, + +57 +00:02:15,000 --> 00:02:17,000 +we are running on Tomcat. + +58 +00:02:17,000 --> 00:02:19,000 +And Tomcat is a servlet container. + +59 +00:02:19,000 --> 00:02:21,000 +So that means even if you're using a spring framework, + +60 +00:02:21,000 --> 00:02:23,000 +behind the scene is get converted into servlets. + +61 +00:02:23,000 --> 00:02:25,000 +So whatever controllers you are creating, + +62 +00:02:25,000 --> 00:02:28,000 +they are basically a servlet behind the scene. + +63 +00:02:28,000 --> 00:02:31,000 +Now server is responsible to accept a user request + +64 +00:02:31,000 --> 00:02:34,000 +and respond it with some data. + +65 +00:02:34,000 --> 00:02:35,000 +So it uses two objects, + +66 +00:02:35,000 --> 00:02:37,000 +HTTP request and HTTP response object. + +67 +00:02:37,000 --> 00:02:41,000 +And if we can see we are using a CP server request here. + +68 +00:02:41,000 --> 00:02:43,000 +We don't need that. + +69 +00:02:43,000 --> 00:02:45,000 +But basically we have, we were doing something + +70 +00:02:45,000 --> 00:02:46,000 +and we have used it, even if we don't mention + +71 +00:02:46,000 --> 00:02:48,000 +that it perfectly works. + +72 +00:02:48,000 --> 00:02:50,000 +But behind the scene, these two objects are there. + +73 +00:02:50,000 --> 00:02:52,000 +Request object is responsible + +74 +00:02:52,000 --> 00:02:54,000 +to get the data from the client. + +75 +00:02:54,000 --> 00:02:55,000 +Response object is responsible + +76 +00:02:55,000 --> 00:02:57,000 +to send the data to the client. + +77 +00:02:57,000 --> 00:02:59,000 +But now before hit the servlet, + +78 +00:02:59,000 --> 00:03:02,000 +you can add a filters in between. + +79 +00:03:02,000 --> 00:03:05,000 +So what happens is when you use a sensor request, + +80 +00:03:05,000 --> 00:03:08,000 +and if you have filters in between, the request first goes + +81 +00:03:08,000 --> 00:03:11,000 +to the filter, then this filter will do certain things + +82 +00:03:11,000 --> 00:03:13,000 +and whatever logic you want to mention. + +83 +00:03:13,000 --> 00:03:16,000 +And then you can say, once this filter job is done, + +84 +00:03:16,000 --> 00:03:18,000 +send the request to the servlet. + +85 +00:03:18,000 --> 00:03:20,000 +The beauty is in this filter you can + +86 +00:03:20,000 --> 00:03:21,000 +modify the request data. + +87 +00:03:21,000 --> 00:03:23,000 +So what if your client says send some data + +88 +00:03:23,000 --> 00:03:25,000 +and maybe you wanna do some filtering + +89 +00:03:25,000 --> 00:03:28,000 +before you send it to the servlet. + +90 +00:03:28,000 --> 00:03:29,000 +So you can do that in the filter + +91 +00:03:29,000 --> 00:03:31,000 +and not just one filter, you can have multiple filters. + +92 +00:03:31,000 --> 00:03:35,000 +So maybe you can have filter one, F1 + +93 +00:03:35,000 --> 00:03:36,000 +and then request goes to F1. + +94 +00:03:36,000 --> 00:03:39,000 +And F1 says, okay, my job is done, I'm going + +95 +00:03:39,000 --> 00:03:41,000 +to call a next filter. + +96 +00:03:41,000 --> 00:03:43,000 +So that goes to next filter F two, + +97 +00:03:43,000 --> 00:03:45,000 +and then that goes to next filter F three. + +98 +00:03:45,000 --> 00:03:47,000 +Once all the filter work is done, + +99 +00:03:47,000 --> 00:03:49,000 +then you can send a request to the server or the servlet. + +100 +00:03:49,000 --> 00:03:51,000 +Now basically you can have multiple filters + +101 +00:03:51,000 --> 00:03:53,000 +and the beauty is since we are using spring security + +102 +00:03:53,000 --> 00:03:56,000 +by default it has added few filters + +103 +00:03:56,000 --> 00:03:59,000 +between your client and a servlet. + +104 +00:03:59,000 --> 00:04:00,000 +So we do have inbuilt filters. + +105 +00:04:00,000 --> 00:04:02,000 +We cannot see that + +106 +00:04:02,000 --> 00:04:05,000 +because we are as a user, we are using it. + +107 +00:04:05,000 --> 00:04:06,000 +But behind the scenes, + +108 +00:04:06,000 --> 00:04:08,000 +spring security is using certain filters. + +109 +00:04:08,000 --> 00:04:10,000 +And the filter who's responsible + +110 +00:04:10,000 --> 00:04:12,000 +to authenticate is your username password filter. + +111 +00:04:12,000 --> 00:04:14,000 +So request goes there. + +112 +00:04:14,000 --> 00:04:17,000 +Now what we have to do is since we're using JW, we can say, + +113 +00:04:17,000 --> 00:04:18,000 +"okay, let me do one thing. + +114 +00:04:18,000 --> 00:04:22,000 +Let me add one more filter in between the username or + +115 +00:04:22,000 --> 00:04:26,000 +communication filter and I can do my job there". + +116 +00:04:26,000 --> 00:04:29,000 +And that particular filter I'm talking about is JW filter. + +117 +00:04:29,000 --> 00:04:31,000 +So that means we have to add a filter. + +118 +00:04:31,000 --> 00:04:33,000 +How do we do that? + +119 +00:04:33,000 --> 00:04:35,000 +So what we can do is we can go to our configuration + +120 +00:04:35,000 --> 00:04:37,000 +and here in this chain, + +121 +00:04:37,000 --> 00:04:39,000 +so you can see we have security filter chain. + +122 +00:04:39,000 --> 00:04:40,000 +Yes, we do have a filter here. + +123 +00:04:40,000 --> 00:04:42,000 +That's what it says, the chain of filters. + +124 +00:04:42,000 --> 00:04:45,000 +So we can add one extra filter here. + +125 +00:04:45,000 --> 00:04:48,000 +So before we do build, I can just go back here + +126 +00:04:48,000 --> 00:04:53,000 +and say dot, I can say add filter before. + +127 +00:04:53,000 --> 00:04:55,000 +So we can add a filter after or before. + +128 +00:04:55,000 --> 00:04:58,000 +So I'm saying add filter before. + +129 +00:04:58,000 --> 00:04:59,000 +Now, before what filter? + +130 +00:04:59,000 --> 00:05:01,000 +So I'm going to say the filter before, + +131 +00:05:01,000 --> 00:05:05,000 +the filter username, password authentication filter. + +132 +00:05:05,000 --> 00:05:09,000 +So before this filter, I want to have my own filter here. + +133 +00:05:09,000 --> 00:05:14,000 +So maybe I want to say I'm going to create a JWT filter. + +134 +00:05:14,000 --> 00:05:16,000 +So which is a filter, which I'm going to use + +135 +00:05:16,000 --> 00:05:17,000 +before this one. + +136 +00:05:17,000 --> 00:05:19,000 +And this is where you can do all your checks. + +137 +00:05:19,000 --> 00:05:20,000 +Okay? + +138 +00:05:20,000 --> 00:05:21,000 +But this is a string, right? + +139 +00:05:21,000 --> 00:05:23,000 +Okay, so this should not be a string, + +140 +00:05:23,000 --> 00:05:24,000 +this should be an object. + +141 +00:05:24,000 --> 00:05:26,000 +So not double coat an object, + +142 +00:05:26,000 --> 00:05:28,000 +but we have to create this object. + +143 +00:05:28,000 --> 00:05:30,000 +It's very simple, you can just come back here + +144 +00:05:30,000 --> 00:05:32,000 +and create an object. + +145 +00:05:32,000 --> 00:05:34,000 +So I will say auto wired private + +146 +00:05:34,000 --> 00:05:39,000 +JWT filter and JWT filter. + +147 +00:05:40,000 --> 00:05:41,000 +So that's the object we have. + +148 +00:05:41,000 --> 00:05:43,000 +And using this object, you can pass it here + +149 +00:05:43,000 --> 00:05:46,000 +and it'll do your job. + +150 +00:05:46,000 --> 00:05:48,000 +Okay, but the problem is this is not an inbuilt class. + +151 +00:05:48,000 --> 00:05:51,000 +If you say control space here, you can see no suggestion. + +152 +00:05:51,000 --> 00:05:54,000 +That means we have to create this filter by ourself. + +153 +00:05:54,000 --> 00:05:56,000 +So it's very simple, you can just click here + +154 +00:05:56,000 --> 00:06:00,000 +and say, "Hey, create a class for me", which is WD filter, + +155 +00:06:02,000 --> 00:06:05,000 +yes, in the same package we'll do in config. + +156 +00:06:05,000 --> 00:06:06,000 +Okay, so you can see we got a filter here, + +157 +00:06:06,000 --> 00:06:08,000 +or maybe you can create a new package + +158 +00:06:08,000 --> 00:06:10,000 +where you can have all your filters. + +159 +00:06:10,000 --> 00:06:12,000 +That's your choice, at this point, I'm just putting + +160 +00:06:12,000 --> 00:06:14,000 +that in a config package and that's my filter. + +161 +00:06:14,000 --> 00:06:17,000 +Now I want this filter to be called + +162 +00:06:17,000 --> 00:06:19,000 +for all the request, okay? + +163 +00:06:19,000 --> 00:06:21,000 +For every request. + +164 +00:06:21,000 --> 00:06:25,000 +So example, let's say if you want to get the hello request, + +165 +00:06:25,000 --> 00:06:26,000 +again for that, also I want to call this filter. + +166 +00:06:26,000 --> 00:06:29,000 +If someone calls for, let's say add two numbers for that, + +167 +00:06:29,000 --> 00:06:30,000 +also I want to call this filter. + +168 +00:06:30,000 --> 00:06:35,000 +Let's say someone is calling for get a student. + +169 +00:06:35,000 --> 00:06:36,000 +Even for that, I want to call this filter. + +170 +00:06:36,000 --> 00:06:41,000 +That means this filter should be called for every request. + +171 +00:06:41,000 --> 00:06:42,000 +And to give that capability, + +172 +00:06:42,000 --> 00:06:44,000 +what we can do is we can say extends. + +173 +00:06:44,000 --> 00:06:48,000 +There's something called once per request filter. + +174 +00:06:48,000 --> 00:06:50,000 +A good name to give a filter. + +175 +00:06:50,000 --> 00:06:52,000 +So for every request we have to call this filter. + +176 +00:06:52,000 --> 00:06:54,000 +That's why we say once per request. + +177 +00:06:54,000 --> 00:06:58,000 +So this filter will be called per request and only once. + +178 +00:06:58,000 --> 00:07:01,000 +Okay, so this is the class we have to do. + +179 +00:07:01,000 --> 00:07:03,000 +And since we are extending a class, + +180 +00:07:03,000 --> 00:07:07,000 +and if you jump to this class, this is an abstract class. + +181 +00:07:07,000 --> 00:07:11,000 +So basically we have to implement the methods, okay? + +182 +00:07:11,000 --> 00:07:13,000 +What method we have here, which we have to implement. + +183 +00:07:13,000 --> 00:07:16,000 +So we'll go back here, I will say implement methods, + +184 +00:07:16,000 --> 00:07:19,000 +and we do, we just have one method called do + +185 +00:07:19,000 --> 00:07:21,000 +filter internal. + +186 +00:07:21,000 --> 00:07:23,000 +I will click on, okay, yeah. + +187 +00:07:23,000 --> 00:07:25,000 +So you can say this is a method we have to work with. + +188 +00:07:25,000 --> 00:07:29,000 +And if you observe, we got two method, two objects, request + +189 +00:07:29,000 --> 00:07:31,000 +and response as I mentioned before. + +190 +00:07:31,000 --> 00:07:34,000 +But also we got one more object called filter chain. + +191 +00:07:34,000 --> 00:07:36,000 +Remember when I talked about F1, F2, F3? + +192 +00:07:36,000 --> 00:07:38,000 +So how F1 will know where + +193 +00:07:38,000 --> 00:07:40,000 +to send the next next request to F2. + +194 +00:07:40,000 --> 00:07:42,000 +F2 will send request to F3. + +195 +00:07:42,000 --> 00:07:44,000 +That's the chain we can create. + +196 +00:07:44,000 --> 00:07:45,000 +So that's a filter chain here, okay? + +197 +00:07:45,000 --> 00:07:48,000 +Now this is where we have to verify or validate our token. + +198 +00:07:48,000 --> 00:07:49,000 +Okay, now you tell me + +199 +00:07:49,000 --> 00:07:54,000 +how exactly we are receiving this token. + +200 +00:07:54,000 --> 00:07:55,000 +So basically in the request object, + +201 +00:07:55,000 --> 00:07:58,000 +we are receiving the token, right? + +202 +00:07:58,000 --> 00:08:00,000 +So we have to use a request object + +203 +00:08:00,000 --> 00:08:02,000 +to get the token and verify it. + +204 +00:08:02,000 --> 00:08:04,000 +How exactly we're gonna do that, + +205 +00:08:04,000 --> 00:08:05,000 +let's say in the next video. + +206 +00:08:05,000 --> 00:08:08,000 +In this video we just created a filter called JWT filter, + +207 +00:08:08,000 --> 00:08:11,000 +which extends one per request filter. + +208 +00:08:11,000 --> 00:08:14,000 +And this is where you have to verify your token. + +209 +00:08:14,000 --> 00:08:17,000 +But then by default this will not be called. + +210 +00:08:17,000 --> 00:08:20,000 +And that's why we have added a filter here. + +211 +00:08:20,000 --> 00:08:23,000 +So you can see we added add filter before. + +212 +00:08:23,000 --> 00:08:24,000 +So before you call this particular filter, + +213 +00:08:24,000 --> 00:08:26,000 +which is which spring security will do + +214 +00:08:26,000 --> 00:08:28,000 +by default we are saying, "Hey, + +215 +00:08:28,000 --> 00:08:31,000 +before you call that, call this particular filter", + +216 +00:08:31,000 --> 00:08:34,000 +which is JWT filter and that's how it is going there. + +217 +00:08:34,000 --> 00:08:36,000 +But again, we are not doing anything here. + +218 +00:08:36,000 --> 00:08:38,000 +Let's try to implement that in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/010 Setting AuthToken in SecurityContext_en.srt b/22 - JWT (JSON Web Token) and OAuth2/010 Setting AuthToken in SecurityContext_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..a0695df3928218b19b3a03dd646bb5c07f67a40a --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/010 Setting AuthToken in SecurityContext_en.srt @@ -0,0 +1,912 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright calm piano music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now, it's time to get the header + +3 +00:00:05,000 --> 00:00:07,000 +from this request object. + +4 +00:00:07,000 --> 00:00:10,000 +Also, we want to work with the username and the token. + +5 +00:00:10,000 --> 00:00:12,000 +So we'll get that from this request. + +6 +00:00:12,000 --> 00:00:14,000 +So first of all, let me get the header. + +7 +00:00:14,000 --> 00:00:16,000 +So I will say string, let's say, I want + +8 +00:00:16,000 --> 00:00:19,000 +to get the header equal to, how will I get the header? + +9 +00:00:19,000 --> 00:00:22,000 +So, of course, from the request objects + +10 +00:00:22,000 --> 00:00:25,000 +or request dot, there's a method called get header. + +11 +00:00:25,000 --> 00:00:27,000 +But then header will have a lot of different data, right? + +12 +00:00:27,000 --> 00:00:29,000 +We just want auth header. + +13 +00:00:29,000 --> 00:00:30,000 +In fact, it'll be better if you go + +14 +00:00:30,000 --> 00:00:32,000 +with auth header here. + +15 +00:00:32,000 --> 00:00:36,000 +Only we want one thing, and we can get that by passing. + +16 +00:00:36,000 --> 00:00:39,000 +I just want to get the authorization header. + +17 +00:00:39,000 --> 00:00:42,000 +Once we get that, now, we can perform the operations. + +18 +00:00:42,000 --> 00:00:44,000 +So when I say operations, what I want? + +19 +00:00:44,000 --> 00:00:46,000 +Of course, I want a thing called a token, + +20 +00:00:46,000 --> 00:00:49,000 +so I will just get the token from the header + +21 +00:00:49,000 --> 00:00:51,000 +and by default, I don't know the tokens. + +22 +00:00:51,000 --> 00:00:52,000 +I will just keep it empty, + +23 +00:00:52,000 --> 00:00:54,000 +and I want to, in fact, instead of empty, + +24 +00:00:54,000 --> 00:00:58,000 +I would say null whether to handle the exceptions. + +25 +00:00:58,000 --> 00:01:02,000 +And then I also want a username, so I will say both on null. + +26 +00:01:02,000 --> 00:01:05,000 +Okay, so I want to get this data from this header. + +27 +00:01:05,000 --> 00:01:08,000 +The way you can do that, first of all, we'll check. + +28 +00:01:08,000 --> 00:01:10,000 +We have to make sure that this is not empty. + +29 +00:01:10,000 --> 00:01:13,000 +So before you fetch, you have to check if this is not empty. + +30 +00:01:13,000 --> 00:01:17,000 +So I will say authheader is not equal to null. + +31 +00:01:17,000 --> 00:01:19,000 +And also, even if it's not null, + +32 +00:01:19,000 --> 00:01:23,000 +maybe the client is not sending the better token. + +33 +00:01:23,000 --> 00:01:25,000 +So we have to also check do we have a better token? + +34 +00:01:25,000 --> 00:01:26,000 +So we'll say authorheader dot, + +35 +00:01:26,000 --> 00:01:29,000 +and the way you can check the header, + +36 +00:01:29,000 --> 00:01:31,000 +it should start with, so the header, + +37 +00:01:31,000 --> 00:01:36,000 +so start with something called a better token, okay? + +38 +00:01:36,000 --> 00:01:37,000 +So if this is starting with this, + +39 +00:01:37,000 --> 00:01:40,000 +then we're happy when if you check with B-E-R-B-E-A, + +40 +00:01:40,000 --> 00:01:41,000 +that even works. + +41 +00:01:41,000 --> 00:01:43,000 +You don't have to check everything, + +42 +00:01:43,000 --> 00:01:44,000 +but let's say I'm checking for better. + +43 +00:01:44,000 --> 00:01:46,000 +And once it is matching, that means, yes, + +44 +00:01:46,000 --> 00:01:48,000 +the client is sending a token. + +45 +00:01:48,000 --> 00:01:50,000 +And now, from that, I want to fetch the token, + +46 +00:01:50,000 --> 00:01:51,000 +so I will say token. + +47 +00:01:52,000 --> 00:01:56,000 +And how will you fetch the token from the authheader, + +48 +00:01:57,000 --> 00:02:00,000 +is we don't want to get the entire string, + +49 +00:02:00,000 --> 00:02:04,000 +but the string which has a token, but not the better keyword + +50 +00:02:04,000 --> 00:02:05,000 +because when you send it from the client, + +51 +00:02:05,000 --> 00:02:08,000 +you will have better space and the token, right? + +52 +00:02:08,000 --> 00:02:10,000 +Better being a six-letter word + +53 +00:02:11,000 --> 00:02:13,000 +and plus one extra space. + +54 +00:02:13,000 --> 00:02:15,000 +So I want to get the token, + +55 +00:02:15,000 --> 00:02:18,000 +but something should start with seven. + +56 +00:02:18,000 --> 00:02:23,000 +So zero, B zero, then one, two, three, four, five, six. + +57 +00:02:23,000 --> 00:02:26,000 +So space is six, we have to start from seven, + +58 +00:02:26,000 --> 00:02:28,000 +and that's how you get the token. + +59 +00:02:28,000 --> 00:02:30,000 +Now, from the request, I want to also get the username. + +60 +00:02:30,000 --> 00:02:32,000 +So it's a username is equal to, + +61 +00:02:32,000 --> 00:02:33,000 +so how will I get the username? + +62 +00:02:33,000 --> 00:02:37,000 +So username, you can get it from the request object, + +63 +00:02:37,000 --> 00:02:38,000 +but it's not directly available. + +64 +00:02:38,000 --> 00:02:41,000 +For that, we have to use extract username. + +65 +00:02:41,000 --> 00:02:42,000 +Of course, you have multiple ways of doing that. + +66 +00:02:42,000 --> 00:02:44,000 +I'm going to use extract methods + +67 +00:02:44,000 --> 00:02:47,000 +for extracting the username, expiration time, + +68 +00:02:47,000 --> 00:02:49,000 +and different attributes. + +69 +00:02:49,000 --> 00:02:53,000 +So I'm going to use extract username from the token. + +70 +00:02:53,000 --> 00:02:55,000 +So from the token, I'm going to extract the username, + +71 +00:02:55,000 --> 00:02:58,000 +but we know that this is coming in the encoded format, + +72 +00:02:58,000 --> 00:03:00,000 +so we have to write some code to extract that. + +73 +00:03:00,000 --> 00:03:01,000 +And I don't want to write the code in the filter. + +74 +00:03:01,000 --> 00:03:05,000 +It should be there in the service JWT service class. + +75 +00:03:05,000 --> 00:03:08,000 +So I'm going to use the JWT service here, + +76 +00:03:08,000 --> 00:03:10,000 +auto wired, JWT service. + +77 +00:03:10,000 --> 00:03:13,000 +And then, okay, why there is a problem, auto wired? + +78 +00:03:13,000 --> 00:03:15,000 +This should be component. + +79 +00:03:15,000 --> 00:03:19,000 +Okay, and now, I can basically use the JWT service + +80 +00:03:19,000 --> 00:03:21,000 +to extract the username. + +81 +00:03:21,000 --> 00:03:23,000 +And at this point, you can see we don't have that method. + +82 +00:03:23,000 --> 00:03:25,000 +So what I will do is I will just click here + +83 +00:03:25,000 --> 00:03:27,000 +and say, hey, create a method. + +84 +00:03:27,000 --> 00:03:28,000 +I will define this method later. + +85 +00:03:28,000 --> 00:03:31,000 +At this point, I will say return empty string. + +86 +00:03:31,000 --> 00:03:34,000 +We'll define that later and I will get the username. + +87 +00:03:34,000 --> 00:03:37,000 +Okay, now, once I have the token, once I have the username, + +88 +00:03:37,000 --> 00:03:40,000 +it's time to validate the token. + +89 +00:03:40,000 --> 00:03:42,000 +And to do that, what I will do is I will, again, check + +90 +00:03:42,000 --> 00:03:45,000 +whatever username I got should not be null. + +91 +00:03:45,000 --> 00:03:48,000 +If it is null, then it doesn't make any sense. + +92 +00:03:48,000 --> 00:03:51,000 +And I also want to check if there's already + +93 +00:03:51,000 --> 00:03:53,000 +an authentication object available. + +94 +00:03:53,000 --> 00:03:56,000 +If the authentication object is already available, + +95 +00:03:56,000 --> 00:03:58,000 +then why you have to check once again? + +96 +00:03:58,000 --> 00:04:00,000 +So we'll do this validation only + +97 +00:04:00,000 --> 00:04:02,000 +when the authentication object is not there. + +98 +00:04:02,000 --> 00:04:06,000 +And to do that, I'm going to use security context + +99 +00:04:06,000 --> 00:04:07,000 +of spring security. + +100 +00:04:07,000 --> 00:04:12,000 +Get the context. Okay, I need to use hold, hold, yeah. + +101 +00:04:12,000 --> 00:04:17,000 +So SecurityContextHolder.getContext.getAuthentication. + +102 +00:04:18,000 --> 00:04:22,000 +If, so, this will basically return you the object, okay? + +103 +00:04:22,000 --> 00:04:25,000 +And if it is null, then only do the validation. + +104 +00:04:25,000 --> 00:04:27,000 +If it's not null, that means you + +105 +00:04:27,000 --> 00:04:28,000 +already have the authentication. + +106 +00:04:28,000 --> 00:04:29,000 +You don't need to verify once again. + +107 +00:04:29,000 --> 00:04:31,000 +Now, this is where you have to validate. + +108 +00:04:31,000 --> 00:04:32,000 +And after while validation, + +109 +00:04:32,000 --> 00:04:35,000 +you have to generate a authentication object, right? + +110 +00:04:35,000 --> 00:04:36,000 +Because it's for spring security, + +111 +00:04:36,000 --> 00:04:39,000 +it's all about the authentication object. + +112 +00:04:39,000 --> 00:04:40,000 +It doesn't care about JWT and other stuff. + +113 +00:04:40,000 --> 00:04:43,000 +It says you give an object of authentication, I'm happy. + +114 +00:04:43,000 --> 00:04:46,000 +But the way you do that is, as a programmer, + +115 +00:04:46,000 --> 00:04:49,000 +you are defining, we are going to do that with JWT. + +116 +00:04:49,000 --> 00:04:50,000 +So we have to validate the token + +117 +00:04:50,000 --> 00:04:53,000 +and then we have to generate authentication object. + +118 +00:04:53,000 --> 00:04:58,000 +So we can check, okay, where they're going to validate it? + +119 +00:04:58,000 --> 00:05:00,000 +We are going to do that in the service object again. + +120 +00:05:00,000 --> 00:05:02,000 +So I'm going to use JWT service dot, + +121 +00:05:02,000 --> 00:05:07,000 +and I will say validate token by passing two things. + +122 +00:05:07,000 --> 00:05:08,000 +We have to pass a token, + +123 +00:05:08,000 --> 00:05:12,000 +and also, I want to pass user details object. + +124 +00:05:12,000 --> 00:05:15,000 +Okay, and for that, I have to create a user detailed object. + +125 +00:05:15,000 --> 00:05:18,000 +So I will say user details. User details is equal to. + +126 +00:05:18,000 --> 00:05:21,000 +So how will you get this object? + +127 +00:05:21,000 --> 00:05:25,000 +I can basically use my user details service, + +128 +00:05:25,000 --> 00:05:29,000 +which returns me object of user details, okay? + +129 +00:05:29,000 --> 00:05:32,000 +So I have to create an object of that, + +130 +00:05:32,000 --> 00:05:34,000 +but I think it will create a cyclic dependency. + +131 +00:05:34,000 --> 00:05:37,000 +I want to get the object from the spring container. + +132 +00:05:37,000 --> 00:05:40,000 +So instead of using directly creating a reference variable + +133 +00:05:40,000 --> 00:05:42,000 +for MyUserDetailsService, I'm going + +134 +00:05:42,000 --> 00:05:45,000 +to use application context, context. + +135 +00:05:45,000 --> 00:05:47,000 +And using this context, + +136 +00:05:47,000 --> 00:05:52,000 +I can say getBean of MyUserDetailsService.class. + +137 +00:05:55,000 --> 00:06:00,000 +And then with this object, I can say load by username + +138 +00:06:00,000 --> 00:06:02,000 +and pass the username here. + +139 +00:06:02,000 --> 00:06:03,000 +So basically, what I'm trying to do + +140 +00:06:03,000 --> 00:06:07,000 +is I'm getting the object of my user details service + +141 +00:06:07,000 --> 00:06:09,000 +and then calling this method called loadUserByUsername. + +142 +00:06:11,000 --> 00:06:14,000 +And we'll get these user details, so that when you send + +143 +00:06:14,000 --> 00:06:16,000 +for validation, you will send these two things, + +144 +00:06:16,000 --> 00:06:19,000 +the token and the user details. + +145 +00:06:19,000 --> 00:06:21,000 +And let's say, if it is valid, + +146 +00:06:21,000 --> 00:06:22,000 +again, we don't have this method. + +147 +00:06:22,000 --> 00:06:27,000 +I will just say create this method in the JWT service. + +148 +00:06:27,000 --> 00:06:28,000 +Again, we are not defining anything. + +149 +00:06:28,000 --> 00:06:30,000 +At this point, I will return true. + +150 +00:06:30,000 --> 00:06:33,000 +We have to define all this stuff in some time. + +151 +00:06:33,000 --> 00:06:36,000 +But now, you can say, if this is valid, + +152 +00:06:36,000 --> 00:06:37,000 +then what you have to do? + +153 +00:06:37,000 --> 00:06:40,000 +You have to generate the authentication object. + +154 +00:06:40,000 --> 00:06:43,000 +But then which authentication object? + +155 +00:06:43,000 --> 00:06:46,000 +Now, if you go back to your config, + +156 +00:06:46,000 --> 00:06:48,000 +we are doing JWT filter before what? + +157 +00:06:48,000 --> 00:06:51,000 +Before user password authentication filter, right? + +158 +00:06:51,000 --> 00:06:54,000 +You just have to create authentication for this + +159 +00:06:54,000 --> 00:06:58,000 +because ultimately spring security works with this. + +160 +00:06:58,000 --> 00:07:00,000 +So now, I just have to come here + +161 +00:07:00,000 --> 00:07:02,000 +and create an object of username, + +162 +00:07:02,000 --> 00:07:04,000 +password authentication token. + +163 +00:07:04,000 --> 00:07:09,000 +I will say this is auth token equal to new username + +164 +00:07:10,000 --> 00:07:13,000 +and authentication token which basically needs two things. + +165 +00:07:13,000 --> 00:07:15,000 +If you can see, it needs the principle, + +166 +00:07:15,000 --> 00:07:19,000 +and it also needs the credentials or the authorities. + +167 +00:07:19,000 --> 00:07:21,000 +So I'm going to pass the principle and the authorities. + +168 +00:07:21,000 --> 00:07:24,000 +So principle is user details. + +169 +00:07:24,000 --> 00:07:27,000 +I don't want to specify the credentials. I will say null. + +170 +00:07:27,000 --> 00:07:30,000 +And I want to specify the authorities, + +171 +00:07:30,000 --> 00:07:33,000 +and I will say userDetails.getAuthorities. + +172 +00:07:33,000 --> 00:07:35,000 +Okay, so by doing this, see, once it is valid, + +173 +00:07:35,000 --> 00:07:38,000 +then you can create a token for authentication. + +174 +00:07:38,000 --> 00:07:39,000 +That means the user is valid. + +175 +00:07:39,000 --> 00:07:41,000 +And now let me create the token. + +176 +00:07:41,000 --> 00:07:44,000 +We got the token now, but we have to set this token + +177 +00:07:44,000 --> 00:07:46,000 +to the security context. + +178 +00:07:46,000 --> 00:07:47,000 +We have this token with us, + +179 +00:07:47,000 --> 00:07:50,000 +but this is not there with this spring security context. + +180 +00:07:50,000 --> 00:07:51,000 +We have to set it there. + +181 +00:07:51,000 --> 00:07:54,000 +Here we are getting the authentication to check it is there. + +182 +00:07:54,000 --> 00:07:57,000 +And since it is not there, we have to set it there. + +183 +00:07:57,000 --> 00:07:59,000 +And this detail, this token should also know + +184 +00:07:59,000 --> 00:08:02,000 +the request object, what exactly the request data has, + +185 +00:08:02,000 --> 00:08:03,000 +so it's a set detail. + +186 +00:08:03,000 --> 00:08:04,000 +So whatever details you've got + +187 +00:08:04,000 --> 00:08:06,000 +on the request object, you will set it here. + +188 +00:08:06,000 --> 00:08:08,000 +And the way you can do that is by saying, + +189 +00:08:08,000 --> 00:08:12,000 +new WebAuthenticationDetailsSource.build details. + +190 +00:08:15,000 --> 00:08:17,000 +And this will be coming from request. + +191 +00:08:17,000 --> 00:08:19,000 +Again, there are so many things, so many moving parts. + +192 +00:08:19,000 --> 00:08:23,000 +It's just that you will be doing all the settings only once. + +193 +00:08:23,000 --> 00:08:24,000 +I could simply pasted this code. + +194 +00:08:24,000 --> 00:08:25,000 +But I'm just trying to get you + +195 +00:08:25,000 --> 00:08:27,000 +through line by line what is happening. + +196 +00:08:27,000 --> 00:08:29,000 +So we got the token here, + +197 +00:08:29,000 --> 00:08:31,000 +but then we got a new token which has no idea, + +198 +00:08:31,000 --> 00:08:34,000 +which it only has the details about the user, + +199 +00:08:34,000 --> 00:08:36,000 +but what exactly the request was, it has no idea. + +200 +00:08:36,000 --> 00:08:38,000 +And to do that, we are setting this detail here. + +201 +00:08:38,000 --> 00:08:40,000 +The authToken should know the request. + +202 +00:08:40,000 --> 00:08:44,000 +And then we have to also set this in the security context. + +203 +00:08:44,000 --> 00:08:47,000 +So I will say SecurityContextHolder.getContext. + +204 +00:08:49,000 --> 00:08:51,000 +And this time, instead of saying get authentication, + +205 +00:08:51,000 --> 00:08:54,000 +we have to say set authentication with the authToken. + +206 +00:08:54,000 --> 00:08:58,000 +So by doing this, we have created a new token, + +207 +00:08:58,000 --> 00:09:00,000 +and we have added that in the security context. + +208 +00:09:00,000 --> 00:09:02,000 +But now since this is a filter, + +209 +00:09:02,000 --> 00:09:05,000 +we have to call the main servlet as well, right? + +210 +00:09:05,000 --> 00:09:07,000 +And the way you do that, or not just the servlet, + +211 +00:09:07,000 --> 00:09:09,000 +but also the filters in between. + +212 +00:09:09,000 --> 00:09:11,000 +So how do you forward to the next filter? + +213 +00:09:11,000 --> 00:09:14,000 +To do that, you will come back here in the method + +214 +00:09:14,000 --> 00:09:19,000 +and say, use a filter chain object and say, doFilter + +215 +00:09:19,000 --> 00:09:22,000 +and pass the request object and response object. + +216 +00:09:22,000 --> 00:09:24,000 +So by doing this, you are continuing the filter chain. + +217 +00:09:24,000 --> 00:09:27,000 +This is how you basically create a JWT filter. + +218 +00:09:27,000 --> 00:09:30,000 +But now, it's not complete yet. You know what is missing? + +219 +00:09:31,000 --> 00:09:34,000 +We have added two methods in the JWT service. + +220 +00:09:34,000 --> 00:09:37,000 +These two and these are still empty. + +221 +00:09:37,000 --> 00:09:39,000 +And let's try to fill that in the next video. + +222 +00:09:39,000 --> 00:09:42,000 +So in this video, we have basically worked + +223 +00:09:42,000 --> 00:09:46,000 +with the JWT filter, and we have created a token + +224 +00:09:46,000 --> 00:09:47,000 +for the user password authentication + +225 +00:09:47,000 --> 00:09:49,000 +based on the JWT filter. + +226 +00:09:49,000 --> 00:09:51,000 +Because ultimately spring security works + +227 +00:09:51,000 --> 00:09:53,000 +with the username password authentication filter. + +228 +00:09:53,000 --> 00:09:57,000 +Okay, now, let's complete the JWT service in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/011 Source-Code.url b/22 - JWT (JSON Web Token) and OAuth2/011 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..c3aa276c900eeef3004f8713c8aed8d2b89be8c7 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/011 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/80e0c059c5c259ef6009838a934ed9e92865c3d7/14.1%20JWT \ No newline at end of file diff --git a/22 - JWT (JSON Web Token) and OAuth2/011 Validating token_en.srt b/22 - JWT (JSON Web Token) and OAuth2/011 Validating token_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..35522b1ed453f8b216ffe66c9e59016ac969c286 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/011 Validating token_en.srt @@ -0,0 +1,376 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:06,000 +-: So now, it's time to implement this extractUsername + +3 +00:00:06,000 --> 00:00:08,000 +and validateToken. + +4 +00:00:08,000 --> 00:00:09,000 +Now there are multiple things + +5 +00:00:09,000 --> 00:00:10,000 +we have which you have to do here. + +6 +00:00:10,000 --> 00:00:12,000 +First of all, when you want to extract, + +7 +00:00:12,000 --> 00:00:13,000 +the token is signed, right? + +8 +00:00:13,000 --> 00:00:15,000 +And when you say you have to extract, so basically you have + +9 +00:00:15,000 --> 00:00:18,000 +to use a key to get the token + +10 +00:00:18,000 --> 00:00:21,000 +and then you can extract the values which you want. + +11 +00:00:21,000 --> 00:00:23,000 +And of course all these values are called claims. + +12 +00:00:23,000 --> 00:00:25,000 +So maybe you can get all the claims together + +13 +00:00:25,000 --> 00:00:28,000 +and you can use different methods to extract each value. + +14 +00:00:28,000 --> 00:00:32,000 +So maybe you can use a method called extract claims, + +15 +00:00:32,000 --> 00:00:33,000 +which will give you everything. + +16 +00:00:33,000 --> 00:00:34,000 +And then extract username + +17 +00:00:34,000 --> 00:00:36,000 +will give you the username, extract. + +18 +00:00:36,000 --> 00:00:40,000 +Expiration will give you expiration and different methods. + +19 +00:00:40,000 --> 00:00:42,000 +Now, instead of typing it, I do have the code with me. + +20 +00:00:42,000 --> 00:00:43,000 +So I'm just pasting it here. + +21 +00:00:43,000 --> 00:00:47,000 +And now if you can see we have multiple methods, okay. + +22 +00:00:47,000 --> 00:00:48,000 +So you have all these methods here. + +23 +00:00:48,000 --> 00:00:50,000 +So we wanted username, but username will be getting + +24 +00:00:50,000 --> 00:00:53,000 +its username from the extract claim. + +25 +00:00:53,000 --> 00:00:54,000 +So as I mentioned, we can have one method + +26 +00:00:54,000 --> 00:00:56,000 +which will give you all the claims. + +27 +00:00:56,000 --> 00:00:59,000 +This is the extract claim method, which is taking a token + +28 +00:00:59,000 --> 00:01:02,000 +and what type of data you want to get. + +29 +00:01:02,000 --> 00:01:06,000 +So here I want to pass, get subject, which is the get user. + +30 +00:01:06,000 --> 00:01:07,000 +And that's a function. + +31 +00:01:07,000 --> 00:01:08,000 +And you can see it is extracting all + +32 +00:01:08,000 --> 00:01:12,000 +the claims and it it'll return you the particular claim. + +33 +00:01:12,000 --> 00:01:15,000 +Now in extract all claims, you are basically getting a key. + +34 +00:01:15,000 --> 00:01:18,000 +And from that you are basically + +35 +00:01:18,000 --> 00:01:21,000 +passing your claims and you're getting the body. + +36 +00:01:21,000 --> 00:01:22,000 +So that's how you will get the username. + +37 +00:01:22,000 --> 00:01:24,000 +That's how you get the claims. + +38 +00:01:24,000 --> 00:01:25,000 +For validating the token, + +39 +00:01:25,000 --> 00:01:27,000 +what you're basically doing is whatever username you got + +40 +00:01:27,000 --> 00:01:28,000 +from the token, we have to verify that + +41 +00:01:28,000 --> 00:01:30,000 +from the data database. + +42 +00:01:30,000 --> 00:01:31,000 +Is it really there? + +43 +00:01:31,000 --> 00:01:32,000 +And to do that, + +44 +00:01:32,000 --> 00:01:34,000 +the username is something which you are passing + +45 +00:01:34,000 --> 00:01:37,000 +from in the token and in the user details, + +46 +00:01:37,000 --> 00:01:39,000 +we are getting data from the database. + +47 +00:01:39,000 --> 00:01:42,000 +Is it matching? If it is equal, great, it's true. + +48 +00:01:42,000 --> 00:01:45,000 +And also we have to verify, is the token token expired? + +49 +00:01:45,000 --> 00:01:48,000 +If the token is expired, we have to return false, right? + +50 +00:01:48,000 --> 00:01:50,000 +And that's where we have to check that as well. + +51 +00:01:50,000 --> 00:01:52,000 +And since we have this method, we have + +52 +00:01:52,000 --> 00:01:53,000 +to also verify each token expired + +53 +00:01:53,000 --> 00:01:57,000 +and that we can do that with extract expiration. + +54 +00:01:57,000 --> 00:01:59,000 +And this should be before the current time. + +55 +00:01:59,000 --> 00:02:01,000 +So how do you know the token is expired? + +56 +00:02:01,000 --> 00:02:04,000 +If the expression time is before the current time + +57 +00:02:04,000 --> 00:02:06,000 +and it'll turn through if it has expired. + +58 +00:02:06,000 --> 00:02:08,000 +And that's what we are checking here. + +59 +00:02:08,000 --> 00:02:10,000 +And how do you get the expiration, of course, + +60 +00:02:10,000 --> 00:02:12,000 +from the same extract claim, + +61 +00:02:12,000 --> 00:02:15,000 +the same method which we have used to extract the username + +62 +00:02:15,000 --> 00:02:18,000 +to extract the expiration date, right? + +63 +00:02:18,000 --> 00:02:19,000 +So this is how you basically implement this. + +64 +00:02:19,000 --> 00:02:22,000 +Again, you will do this setup only once, + +65 +00:02:22,000 --> 00:02:25,000 +but by doing this we are extracting all the details + +66 +00:02:25,000 --> 00:02:28,000 +and now the goal looks complete. + +67 +00:02:28,000 --> 00:02:32,000 +Let's verify that. So I'll just restart the application. + +68 +00:02:33,000 --> 00:02:35,000 +And again, we have to create a new token + +69 +00:02:37,000 --> 00:02:39,000 +because the earlier token has expired already. + +70 +00:02:39,000 --> 00:02:41,000 +So I will go back to my postman. + +71 +00:02:41,000 --> 00:02:44,000 +Of course, if you say hello, this will not work. + +72 +00:02:44,000 --> 00:02:48,000 +And in the console, if you see you also got errors here + +73 +00:02:48,000 --> 00:02:50,000 +and the errors will talk about + +74 +00:02:52,000 --> 00:02:55,000 +the JW signature does not match locally. + +75 +00:02:55,000 --> 00:02:57,000 +It may be it is not trusted. + +76 +00:02:57,000 --> 00:03:00,000 +Okay, so there's some problem with the old token. + +77 +00:03:00,000 --> 00:03:03,000 +So let's create new. So go back to login + +78 +00:03:03,000 --> 00:03:05,000 +with the same details. + +79 +00:03:05,000 --> 00:03:08,000 +Harsh and H@123 + +80 +00:03:08,000 --> 00:03:10,000 +The consent, you've got a new token + +81 +00:03:10,000 --> 00:03:11,000 +and this will work only you for three minutes. + +82 +00:03:11,000 --> 00:03:16,000 +So let's be quick. Let's go to hello and authorization. + +83 +00:03:16,000 --> 00:03:20,000 +Let's set the new token. Click on Send, voila. + +84 +00:03:20,000 --> 00:03:21,000 +You got Hello World. So yeah, so you're able + +85 +00:03:21,000 --> 00:03:24,000 +to send the token to the server. + +86 +00:03:24,000 --> 00:03:28,000 +So to the server is verifying that with the of JWT + +87 +00:03:28,000 --> 00:03:30,000 +filters and it's able able to get that. + +88 +00:03:30,000 --> 00:03:31,000 +This other is from the earlier one, + +89 +00:03:31,000 --> 00:03:34,000 +but now if you can see, when I search for it, + +90 +00:03:34,000 --> 00:03:36,000 +it's hitting the database as well to verify + +91 +00:03:36,000 --> 00:03:39,000 +if the user is really, really the user of the system. + +92 +00:03:39,000 --> 00:03:43,000 +So yeah, that's how we are basically done with JWT + +93 +00:03:43,000 --> 00:03:45,000 +where you are generating a token. + +94 +00:03:45,000 --> 00:03:49,000 +And I'll try to summarize this in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/012 JWT Summary_en.srt b/22 - JWT (JSON Web Token) and OAuth2/012 JWT Summary_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..23ae26cdf91e1cf318972e71e15d44c512be3809 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/012 JWT Summary_en.srt @@ -0,0 +1,540 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle bright piano music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now, let's do a quick summary. + +3 +00:00:05,000 --> 00:00:08,000 +So what I will do, basically, close all the tabs here + +4 +00:00:08,000 --> 00:00:10,000 +and let's go step by step. + +5 +00:00:10,000 --> 00:00:13,000 +See what we want is we to get this hello. + +6 +00:00:13,000 --> 00:00:17,000 +So I want to send the hello request from the client + +7 +00:00:17,000 --> 00:00:18,000 +to get hello world. + +8 +00:00:18,000 --> 00:00:19,000 +Of course, this can be any request. + +9 +00:00:19,000 --> 00:00:21,000 +It doesn't matter, but we are sending a hello request. + +10 +00:00:21,000 --> 00:00:23,000 +And now to send this request, + +11 +00:00:23,000 --> 00:00:25,000 +now since this is a protected resource + +12 +00:00:25,000 --> 00:00:28,000 +when you send a request to the server, server says okay, + +13 +00:00:28,000 --> 00:00:31,000 +if you want to use this, we have to check + +14 +00:00:31,000 --> 00:00:33,000 +if you are a user of this system. + +15 +00:00:33,000 --> 00:00:35,000 +We want to know you're credentials. + +16 +00:00:35,000 --> 00:00:36,000 +And the way that you can do that, of course, + +17 +00:00:36,000 --> 00:00:38,000 +you can pass a username and passwords. + +18 +00:00:38,000 --> 00:00:40,000 +Spring security has an in-built feature + +19 +00:00:40,000 --> 00:00:42,000 +where it will verify username and password with a database, + +20 +00:00:42,000 --> 00:00:44,000 +of course, you have to do some modifications there. + +21 +00:00:44,000 --> 00:00:46,000 +And then you are in. + +22 +00:00:46,000 --> 00:00:48,000 +But then we don't want to work with the sessions + +23 +00:00:48,000 --> 00:00:49,000 +we want to work with JWT + +24 +00:00:49,000 --> 00:00:52,000 +where every time you send a request, + +25 +00:00:52,000 --> 00:00:56,000 +you have to basically send a token which ASO will verify + +26 +00:00:56,000 --> 00:00:57,000 +and then it will give you the access + +27 +00:00:57,000 --> 00:00:59,000 +and that's what we want here. + +28 +00:00:59,000 --> 00:01:01,000 +To achieve that, first of all, + +29 +00:01:01,000 --> 00:01:04,000 +you have to make some modification in this security config + +30 +00:01:04,000 --> 00:01:08,000 +where you are saying that once a user logged in, + +31 +00:01:08,000 --> 00:01:12,000 +I want to basically create a new token, okay? + +32 +00:01:12,000 --> 00:01:13,000 +And the way you do that, + +33 +00:01:13,000 --> 00:01:16,000 +is by going to the user controller. + +34 +00:01:16,000 --> 00:01:20,000 +So whenever I use a log in, we have to generate a token, + +35 +00:01:20,000 --> 00:01:21,000 +a JWT token. + +36 +00:01:21,000 --> 00:01:24,000 +And before you generate, we have to verify if the username + +37 +00:01:24,000 --> 00:01:26,000 +and password is correct. + +38 +00:01:26,000 --> 00:01:28,000 +And as you can see, we are doing that here. + +39 +00:01:28,000 --> 00:01:30,000 +If the username and password is correct, + +40 +00:01:30,000 --> 00:01:31,000 +if it is authenticated, + +41 +00:01:31,000 --> 00:01:33,000 +then only you will create a token. + +42 +00:01:33,000 --> 00:01:35,000 +And this particular method, + +43 +00:01:35,000 --> 00:01:38,000 +we have created that in the JWT service. + +44 +00:01:38,000 --> 00:01:41,000 +Now, what this method does is it creates a token for us, + +45 +00:01:41,000 --> 00:01:44,000 +and in this token, you will be having your claims, + +46 +00:01:44,000 --> 00:01:46,000 +basically you'll be having your user + +47 +00:01:46,000 --> 00:01:49,000 +which is your subject, the issue date, expiration + +48 +00:01:49,000 --> 00:01:51,000 +and then you have to also sign it. + +49 +00:01:51,000 --> 00:01:53,000 +And there are different algorithms available for signing. + +50 +00:01:53,000 --> 00:01:58,000 +We are using HS256 which is the HMAC SHA-256. + +51 +00:02:00,000 --> 00:02:01,000 +The bigger number is better, + +52 +00:02:01,000 --> 00:02:02,000 +so we can also go with 512. + +53 +00:02:02,000 --> 00:02:03,000 +And when you say expiration, + +54 +00:02:03,000 --> 00:02:05,000 +I'm setting the expiration for three minutes, + +55 +00:02:05,000 --> 00:02:07,000 +so this is thousand milliseconds which is one second + +56 +00:02:07,000 --> 00:02:11,000 +in 260 which is one minute into three, three minutes. + +57 +00:02:11,000 --> 00:02:14,000 +You can also make it 30 if you want for 30 minutes time. + +58 +00:02:16,000 --> 00:02:19,000 +Now, basically, by doing this, you are generating a token. + +59 +00:02:19,000 --> 00:02:21,000 +Now, to generate a token, you also need a secret key. + +60 +00:02:21,000 --> 00:02:24,000 +You can hard code the secret key that we have done here, + +61 +00:02:24,000 --> 00:02:28,000 +or you can generate a secret key just like this. + +62 +00:02:28,000 --> 00:02:29,000 +And once you got your token, + +63 +00:02:29,000 --> 00:02:32,000 +you have to respond back to the client. + +64 +00:02:32,000 --> 00:02:35,000 +So you can see this is where you are generating a token, + +65 +00:02:35,000 --> 00:02:37,000 +and you are sending that as a string. + +66 +00:02:37,000 --> 00:02:41,000 +And that's what we will have received in the response. + +67 +00:02:41,000 --> 00:02:43,000 +And when you say log-in and send this detail, + +68 +00:02:43,000 --> 00:02:45,000 +okay, the server is down. + +69 +00:02:46,000 --> 00:02:48,000 +Maybe, I am using a wrong one. + +70 +00:02:50,000 --> 00:02:53,000 +Okay, so you can see, we've got the token back here. + +71 +00:02:53,000 --> 00:02:56,000 +But now, we have to use that token + +72 +00:02:56,000 --> 00:02:58,000 +to send the hello request. + +73 +00:02:58,000 --> 00:02:59,000 +So now, with the old token, + +74 +00:02:59,000 --> 00:03:02,000 +I have to send it once again, copy. + +75 +00:03:02,000 --> 00:03:05,000 +And with this old token, or new token, + +76 +00:03:05,000 --> 00:03:08,000 +I'm going to change the old token to the new token, + +77 +00:03:08,000 --> 00:03:09,000 +and click on Send. + +78 +00:03:09,000 --> 00:03:10,000 +You got the response. + +79 +00:03:10,000 --> 00:03:12,000 +Now, what is happening behind the scenes? + +80 +00:03:12,000 --> 00:03:14,000 +When you send this token, the request goes to the server. + +81 +00:03:14,000 --> 00:03:18,000 +On the server side, it is your responsibility to filter, + +82 +00:03:18,000 --> 00:03:20,000 +to basically add a filter for the JWT. + +83 +00:03:20,000 --> 00:03:22,000 +And the way you can do that is first of all, + +84 +00:03:22,000 --> 00:03:25,000 +you have to inform your Spring Security + +85 +00:03:25,000 --> 00:03:27,000 +that we are adding a filter + +86 +00:03:27,000 --> 00:03:31,000 +before the username password authentication filter. + +87 +00:03:31,000 --> 00:03:33,000 +This is the filter which is JWT filter. + +88 +00:03:33,000 --> 00:03:35,000 +And we have to define that by ourself. + +89 +00:03:35,000 --> 00:03:38,000 +And this is the class where we are doing that. + +90 +00:03:38,000 --> 00:03:40,000 +We have to extend OncePerRequestFilter, + +91 +00:03:42,000 --> 00:03:45,000 +so that we can get this meta called doFilterInternal + +92 +00:03:45,000 --> 00:03:48,000 +which basically, gets the request, + +93 +00:03:48,000 --> 00:03:50,000 +and you can perform whatever option you want here. + +94 +00:03:50,000 --> 00:03:52,000 +What we are doing is we are getting a hold + +95 +00:03:52,000 --> 00:03:55,000 +on the request object, get the authorization. + +96 +00:03:55,000 --> 00:03:58,000 +And then from that, extract the token. + +97 +00:03:58,000 --> 00:03:59,000 +Because token starts with Bearer, + +98 +00:03:59,000 --> 00:04:01,000 +we have to check that, is it there? + +99 +00:04:01,000 --> 00:04:03,000 +If it is there, get the token, + +100 +00:04:03,000 --> 00:04:05,000 +but then not the Bearer key word. + +101 +00:04:05,000 --> 00:04:08,000 +We have to skip that. We are starting with seven. + +102 +00:04:08,000 --> 00:04:09,000 +And once you got that, + +103 +00:04:09,000 --> 00:04:11,000 +you have to also extract the username. + +104 +00:04:11,000 --> 00:04:12,000 +Once you got the username, + +105 +00:04:12,000 --> 00:04:14,000 +you have to also check if the username is null or not. + +106 +00:04:14,000 --> 00:04:16,000 +If it is null, then of course, + +107 +00:04:16,000 --> 00:04:17,000 +we don't have to do anything extra. + +108 +00:04:17,000 --> 00:04:19,000 +You can simply spawn by saying, + +109 +00:04:19,000 --> 00:04:21,000 +hey, invalid user, invalid token. + +110 +00:04:21,000 --> 00:04:23,000 +But then we got a username here, let's say. + +111 +00:04:23,000 --> 00:04:24,000 +And then we have to also check + +112 +00:04:24,000 --> 00:04:27,000 +if the authentication object is already present. + +113 +00:04:27,000 --> 00:04:29,000 +If it is present, then why you have to do this? + +114 +00:04:29,000 --> 00:04:31,000 +Now, since this is not present, + +115 +00:04:31,000 --> 00:04:32,000 +what we are going to do is we are going + +116 +00:04:32,000 --> 00:04:34,000 +to validate the token. + +117 +00:04:34,000 --> 00:04:36,000 +And once it is validated, + +118 +00:04:36,000 --> 00:04:39,000 +you will be creating a new token object + +119 +00:04:39,000 --> 00:04:42,000 +which you have to give to the security context, + +120 +00:04:42,000 --> 00:04:45,000 +so that it will know that there is a token available. + +121 +00:04:45,000 --> 00:04:46,000 +And that's it. + +122 +00:04:46,000 --> 00:04:48,000 +Then you have to pile the request to the next filter. + +123 +00:04:48,000 --> 00:04:50,000 +But in the validate token, + +124 +00:04:50,000 --> 00:04:51,000 +we are doing some extra stuff. + +125 +00:04:51,000 --> 00:04:54,000 +We are basically checking the username + +126 +00:04:54,000 --> 00:04:55,000 +which is sent in the token. + +127 +00:04:55,000 --> 00:04:58,000 +Is it matching with the username in the database? + +128 +00:04:58,000 --> 00:04:59,000 +And that's what we're doing here. + +129 +00:04:59,000 --> 00:05:01,000 +We are also checking, is the token expired? + +130 +00:05:01,000 --> 00:05:02,000 +We are also checking, + +131 +00:05:02,000 --> 00:05:04,000 +we're also checking all the claims from this + +132 +00:05:04,000 --> 00:05:06,000 +if you want to get some extra details. + +133 +00:05:07,000 --> 00:05:11,000 +And, yeah, that's how this entire JWT we have done. + +134 +00:05:11,000 --> 00:05:14,000 +I know, big topic, but all this configuration, + +135 +00:05:14,000 --> 00:05:16,000 +you will be doing only once. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/013 Implementing OAuth2_en.srt b/22 - JWT (JSON Web Token) and OAuth2/013 Implementing OAuth2_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..77c0dfca78544614e5fa5e479f90b5a54c8e15b0 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/013 Implementing OAuth2_en.srt @@ -0,0 +1,608 @@ +1 +00:00:00,000 --> 00:00:02,000 +(upbeat music) + +2 +00:00:02,000 --> 00:00:05,000 +-: In this video we'll talk about OAuth, + +3 +00:00:05,000 --> 00:00:07,000 +which stands for open authorization. + +4 +00:00:07,000 --> 00:00:09,000 +See, the thing is whenever you go + +5 +00:00:09,000 --> 00:00:11,000 +to any particular application or web application + +6 +00:00:11,000 --> 00:00:13,000 +and then you want to log in, + +7 +00:00:13,000 --> 00:00:14,000 +of course this particular servers, + +8 +00:00:14,000 --> 00:00:16,000 +they have their own login system. + +9 +00:00:16,000 --> 00:00:18,000 +So they can, they will ask you for username + +10 +00:00:18,000 --> 00:00:23,000 +and password, but then you also see more options there. + +11 +00:00:23,000 --> 00:00:25,000 +Sometime you can see an option of login through Google + +12 +00:00:25,000 --> 00:00:28,000 +or login through GitHub or login through Facebook. + +13 +00:00:28,000 --> 00:00:32,000 +And most of us, or maybe I'm talking about me here, + +14 +00:00:32,000 --> 00:00:36,000 +I normally prefer to log into any application using Google + +15 +00:00:36,000 --> 00:00:39,000 +because it is much faster and I'm lazy to enter + +16 +00:00:39,000 --> 00:00:40,000 +the username password, just click on Google, + +17 +00:00:40,000 --> 00:00:42,000 +I'm already logged in my browser. + +18 +00:00:42,000 --> 00:00:44,000 +It'll pick up the credentials, + +19 +00:00:44,000 --> 00:00:46,000 +it'll pick up the details about me, + +20 +00:00:46,000 --> 00:00:48,000 +and voila, I'm into the application. + +21 +00:00:48,000 --> 00:00:50,000 +And every time I want to log in, + +22 +00:00:50,000 --> 00:00:51,000 +I will do the same thing, right? + +23 +00:00:51,000 --> 00:00:53,000 +It is much faster. + +24 +00:00:53,000 --> 00:00:56,000 +Now, this is possible with the help of OAuth. + +25 +00:00:56,000 --> 00:00:59,000 +So basically what is happening is you are the owner, + +26 +00:00:59,000 --> 00:01:01,000 +so you are the resource owner. + +27 +00:01:01,000 --> 00:01:03,000 +Basically you want to log into a particular application. + +28 +00:01:03,000 --> 00:01:07,000 +And then this third party application says "if you want + +29 +00:01:07,000 --> 00:01:09,000 +to log in, you have to enter username, password." + +30 +00:01:09,000 --> 00:01:11,000 +But then you are saying, "Hey, + +31 +00:01:11,000 --> 00:01:12,000 +I already have a Google account. + +32 +00:01:12,000 --> 00:01:15,000 +What if you can check my credentials there?" + +33 +00:01:15,000 --> 00:01:17,000 +Because ultimately you have to prove yourself, right? + +34 +00:01:17,000 --> 00:01:19,000 +So you have to prove that the person you are claiming + +35 +00:01:19,000 --> 00:01:21,000 +to be, is it real? + +36 +00:01:21,000 --> 00:01:22,000 +And the way you can do that is + +37 +00:01:22,000 --> 00:01:23,000 +by passing the username password. + +38 +00:01:23,000 --> 00:01:25,000 +Or you can say check with Google. + +39 +00:01:25,000 --> 00:01:27,000 +But again, you don't want to provide the username + +40 +00:01:27,000 --> 00:01:29,000 +and password for Google again. + +41 +00:01:29,000 --> 00:01:30,000 +So it's a big security risk. + +42 +00:01:30,000 --> 00:01:33,000 +So what you can do is the third party application can + +43 +00:01:33,000 --> 00:01:36,000 +request to Google for the login or for the details, + +44 +00:01:36,000 --> 00:01:39,000 +and Google will use your ID + +45 +00:01:39,000 --> 00:01:41,000 +and credentials to provide the access. + +46 +00:01:41,000 --> 00:01:44,000 +Now of course, this third party will not be able + +47 +00:01:44,000 --> 00:01:46,000 +to get the username and password from Google. + +48 +00:01:46,000 --> 00:01:49,000 +You can specify what are the features you want to share + +49 +00:01:49,000 --> 00:01:50,000 +because let's say if you talk about Google, + +50 +00:01:50,000 --> 00:01:52,000 +they have multiple services, right? + +51 +00:01:52,000 --> 00:01:54,000 +They have lot of data about you, + +52 +00:01:54,000 --> 00:01:56,000 +but of course they will not share everything + +53 +00:01:56,000 --> 00:01:58,000 +with this particular third party application. + +54 +00:01:58,000 --> 00:02:01,000 +Now, depending upon what their use case is, maybe you want + +55 +00:02:01,000 --> 00:02:04,000 +to fetch all the email letters of your Gmail account. + +56 +00:02:04,000 --> 00:02:06,000 +So maybe you can give the access to it. + +57 +00:02:06,000 --> 00:02:08,000 +Maybe you want to share the contacts. + +58 +00:02:08,000 --> 00:02:10,000 +Example, when you buy Android phone, + +59 +00:02:10,000 --> 00:02:12,000 +you get all your Google contacts there. + +60 +00:02:12,000 --> 00:02:14,000 +So that's how you provide the access. + +61 +00:02:14,000 --> 00:02:18,000 +Now basically this is possible with the help of OAuth + +62 +00:02:18,000 --> 00:02:21,000 +and the current version of OAuth is OAuth2, + +63 +00:02:21,000 --> 00:02:23,000 +and we can implement that in our project. + +64 +00:02:23,000 --> 00:02:25,000 +Now to understand that, what I will do is + +65 +00:02:25,000 --> 00:02:26,000 +I will create a very simple project here. + +66 +00:02:26,000 --> 00:02:30,000 +As you can see, I'm going to spring initializr + +67 +00:02:30,000 --> 00:02:32,000 +and then I will select a Maven project. + +68 +00:02:32,000 --> 00:02:34,000 +And then the version I will go + +69 +00:02:34,000 --> 00:02:38,000 +to 3.2.4 and I will say com.telusko, + +70 +00:02:38,000 --> 00:02:43,000 +artifact is SpringOAuthDemo. + +71 +00:02:43,000 --> 00:02:46,000 +And then the version Java version I have is Java 21, + +72 +00:02:46,000 --> 00:02:48,000 +Java 22 recently released. + +73 +00:02:48,000 --> 00:02:49,000 +But I'm going to stick to Java 21 + +74 +00:02:49,000 --> 00:02:52,000 +because in my machine I have Java 21 here, + +75 +00:02:52,000 --> 00:02:54,000 +and that's LTS version. + +76 +00:02:54,000 --> 00:02:56,000 +Next, I'm going to add some dependency. + +77 +00:02:56,000 --> 00:02:58,000 +First of all, I want to make a web application, + +78 +00:02:58,000 --> 00:03:01,000 +so spring web, but also I want to implement OAuth. + +79 +00:03:01,000 --> 00:03:05,000 +So I will search for the OAuth2 client, + +80 +00:03:05,000 --> 00:03:08,000 +and then you can click on generate. + +81 +00:03:08,000 --> 00:03:09,000 +So this will create a create your project. + +82 +00:03:09,000 --> 00:03:11,000 +And I got this project here. + +83 +00:03:11,000 --> 00:03:14,000 +It's time to open that project in my intelligent idea. + +84 +00:03:14,000 --> 00:03:17,000 +So this is the project, a very simple one, nothing fancy. + +85 +00:03:17,000 --> 00:03:18,000 +And now if you check the pom file, + +86 +00:03:18,000 --> 00:03:20,000 +you can see we have two dependencies here. + +87 +00:03:20,000 --> 00:03:23,000 +At this point, I will just comment this section, + +88 +00:03:23,000 --> 00:03:25,000 +we'll try to implement this later. + +89 +00:03:25,000 --> 00:03:30,000 +What I want is, I want to basically have some request. + +90 +00:03:30,000 --> 00:03:33,000 +So let's say if I go to a URL + +91 +00:03:33,000 --> 00:03:35,000 +by saying hello, it should return something. + +92 +00:03:35,000 --> 00:03:37,000 +That's it, nothing much, so let me create a simple + +93 +00:03:37,000 --> 00:03:41,000 +controller and I will say this is as hello controller, + +94 +00:03:41,000 --> 00:03:44,000 +and this will have, I mean this should be + +95 +00:03:44,000 --> 00:03:46,000 +of course a rest controller. + +96 +00:03:46,000 --> 00:03:48,000 +And here maybe I want to have a method which is public, + +97 +00:03:48,000 --> 00:03:49,000 +which returns a string. + +98 +00:03:49,000 --> 00:03:52,000 +And I will say greet simple one. + +99 +00:03:52,000 --> 00:03:54,000 +And this should be get mapping. + +100 +00:03:54,000 --> 00:03:58,000 +And whenever you send a request for, let's say hello, + +101 +00:03:58,000 --> 00:04:02,000 +I want to call this method, and this method returns. + +102 +00:04:02,000 --> 00:04:05,000 +"Welcome to Telusko", okay? + +103 +00:04:05,000 --> 00:04:06,000 +Nothing fancy, simple stuff. + +104 +00:04:06,000 --> 00:04:08,000 +I'm just saying welcome to Telusko. + +105 +00:04:08,000 --> 00:04:11,000 +And now let's try to run this. + +106 +00:04:11,000 --> 00:04:15,000 +My port for 8080 is busy somewhere. + +107 +00:04:15,000 --> 00:04:17,000 +So what I will do is I will go to application properties, + +108 +00:04:17,000 --> 00:04:20,000 +and this is where I will set the port number, not spring, + +109 +00:04:20,000 --> 00:04:25,000 +server.port, and let me go with 8000. + +110 +00:04:25,000 --> 00:04:28,000 +So that's a port I'm using here. + +111 +00:04:28,000 --> 00:04:31,000 +And now let me, we are done some changes in the moment. + +112 +00:04:31,000 --> 00:04:33,000 +So let's reload that. + +113 +00:04:33,000 --> 00:04:35,000 +Okay, after making these changes, + +114 +00:04:35,000 --> 00:04:36,000 +let me just run this application. + +115 +00:04:36,000 --> 00:04:38,000 +I'm just being a bit quick here + +116 +00:04:38,000 --> 00:04:40,000 +because we have, we know a lot of different stuff now. + +117 +00:04:40,000 --> 00:04:44,000 +Let me run this and it'll take some time. + +118 +00:04:44,000 --> 00:04:49,000 +So as you can see, this started on port 8000. + +119 +00:04:49,000 --> 00:04:53,000 +So I'll go to my browser and say localhost:8000, enter. + +120 +00:04:53,000 --> 00:04:55,000 +And you can see we got an error, okay? + +121 +00:04:55,000 --> 00:04:57,000 +So not 8000, it should be, hello. + +122 +00:04:57,000 --> 00:04:59,000 +And we got welcome to Telusko. + +123 +00:04:59,000 --> 00:05:02,000 +What I want to do is I want to secure it + +124 +00:05:02,000 --> 00:05:03,000 +with the help of OAuth2. + +125 +00:05:03,000 --> 00:05:05,000 +So of course I want to get a login form, + +126 +00:05:05,000 --> 00:05:08,000 +but then we can use Spring Security to get the login form. + +127 +00:05:08,000 --> 00:05:10,000 +And we do get the default login form. + +128 +00:05:10,000 --> 00:05:13,000 +But again, for that you have to enter username, password, + +129 +00:05:13,000 --> 00:05:16,000 +and you, your job is basically + +130 +00:05:16,000 --> 00:05:18,000 +to verify the username password. + +131 +00:05:18,000 --> 00:05:19,000 +You need to have a database. + +132 +00:05:19,000 --> 00:05:21,000 +What if I don't want to manage the database? + +133 +00:05:21,000 --> 00:05:24,000 +Let's try to use Google login here, a Google log, GitHub. + +134 +00:05:24,000 --> 00:05:27,000 +Let's try one by one, so let's try Google login here. + +135 +00:05:27,000 --> 00:05:30,000 +So if someone is trying to access this page, + +136 +00:05:30,000 --> 00:05:32,000 +I want to have a Google login. + +137 +00:05:32,000 --> 00:05:35,000 +So the way I'm going to do that is, first of all, + +138 +00:05:35,000 --> 00:05:37,000 +let me go back to my application. + +139 +00:05:37,000 --> 00:05:41,000 +And here, let me uncomment this section, okay? + +140 +00:05:41,000 --> 00:05:43,000 +This is what I want to use. + +141 +00:05:43,000 --> 00:05:44,000 +So after doing uncomment + +142 +00:05:44,000 --> 00:05:48,000 +I will just reload my Maven and now reload done. + +143 +00:05:48,000 --> 00:05:49,000 +Let me restart the application + +144 +00:05:49,000 --> 00:05:52,000 +because we have some changes in the library now. + +145 +00:05:52,000 --> 00:05:54,000 +So let's see, is it secure by default now? + +146 +00:05:54,000 --> 00:05:58,000 +So if I hit refresh, yeah, we've got a login. + +147 +00:05:58,000 --> 00:06:01,000 +Now, we got this login form because of spring security. + +148 +00:06:01,000 --> 00:06:05,000 +So whenever we are using OAuth2 client behind the scene, + +149 +00:06:05,000 --> 00:06:07,000 +it is also implementing spring security. + +150 +00:06:07,000 --> 00:06:09,000 +But then we don't want this login form. + +151 +00:06:09,000 --> 00:06:12,000 +Where is our Google and GitHub icons? + +152 +00:06:12,000 --> 00:06:15,000 +Let's see that in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/014 Google Oauth2 Login_en.srt b/22 - JWT (JSON Web Token) and OAuth2/014 Google Oauth2 Login_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..027e1cc45ad5981b678efaf295b3e61b53f32285 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/014 Google Oauth2 Login_en.srt @@ -0,0 +1,676 @@ +1 +00:00:00,000 --> 00:00:03,000 +(upbeat music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now let's try to configure our spring security + +3 +00:00:05,000 --> 00:00:07,000 +by saying I don't need your login form. + +4 +00:00:07,000 --> 00:00:11,000 +I want to have a login through Google or GitHub. + +5 +00:00:11,000 --> 00:00:14,000 +So to do that, what I will do is I will configure, so one, + +6 +00:00:14,000 --> 00:00:16,000 +you have to configure the security configuration. + +7 +00:00:16,000 --> 00:00:18,000 +So I will go, you will get a Java class + +8 +00:00:18,000 --> 00:00:20,000 +and maybe you will name it + +9 +00:00:20,000 --> 00:00:23,000 +as security config the way we are doing from a long time. + +10 +00:00:23,000 --> 00:00:27,000 +And then I want to say this is the configuration + +11 +00:00:27,000 --> 00:00:29,000 +and also I want to implement a web security. + +12 +00:00:29,000 --> 00:00:34,000 +So do that, and then we have to basically create a bean + +13 +00:00:34,000 --> 00:00:36,000 +for security filter chain. + +14 +00:00:36,000 --> 00:00:41,000 +So I will say bean and public security filter chain. + +15 +00:00:41,000 --> 00:00:43,000 +And we are doing this for a long time. + +16 +00:00:43,000 --> 00:00:45,000 +So I will say default security filter chain. + +17 +00:00:45,000 --> 00:00:47,000 +And this might throw an exception. + +18 +00:00:47,000 --> 00:00:48,000 +And also I want to get the hold + +19 +00:00:48,000 --> 00:00:51,000 +on the HTTP security objects. + +20 +00:00:51,000 --> 00:00:53,000 +I will say HTTP. + +21 +00:00:53,000 --> 00:00:58,000 +And now here we have to return HTTP.build. + +22 +00:00:58,000 --> 00:01:01,000 +Okay, so now let's do the modification. + +23 +00:01:01,000 --> 00:01:03,000 +So this is something you have to do. + +24 +00:01:03,000 --> 00:01:04,000 +You have to get the hold on HTTP object + +25 +00:01:04,000 --> 00:01:08,000 +and then make some changes in the HTTP object and return. + +26 +00:01:08,000 --> 00:01:09,000 +But what are the changes I have to make? + +27 +00:01:09,000 --> 00:01:11,000 +First of all, I have to say HTTP dot, + +28 +00:01:11,000 --> 00:01:13,000 +I have to authorize all the request + +29 +00:01:13,000 --> 00:01:16,000 +and we'll take an auth.object, not this one. + +30 +00:01:16,000 --> 00:01:17,000 +And on this auth.object I want + +31 +00:01:17,000 --> 00:01:21,000 +to say any request should be authenticated. + +32 +00:01:21,000 --> 00:01:23,000 +That's the first thing I'm doing. + +33 +00:01:23,000 --> 00:01:24,000 +The next thing I want to do + +34 +00:01:24,000 --> 00:01:27,000 +on the HTTP object is I don't want to use form login. + +35 +00:01:27,000 --> 00:01:29,000 +So by default will give you form login. + +36 +00:01:29,000 --> 00:01:32,000 +And I don't want to work with anything else. + +37 +00:01:32,000 --> 00:01:36,000 +The only thing I want to focus is on auth2Login + +38 +00:01:36,000 --> 00:01:38,000 +and I will just go with the defaults. + +39 +00:01:38,000 --> 00:01:40,000 +So Customizer.withdefaults. + +40 +00:01:40,000 --> 00:01:42,000 +Okay, so basically what I'm doing is I'm saying + +41 +00:01:42,000 --> 00:01:45,000 +that you have to authorize all the requests + +42 +00:01:45,000 --> 00:01:48,000 +and the login should be happening with the oauth2 + +43 +00:01:48,000 --> 00:01:50,000 +just by making this changes now. + +44 +00:01:50,000 --> 00:01:54,000 +So if I reload this application, I might get error + +45 +00:01:54,000 --> 00:01:56,000 +because we have not set the properties yet. + +46 +00:01:58,000 --> 00:02:00,000 +Okay, so we got the errors + +47 +00:02:00,000 --> 00:02:03,000 +and I was expecting some error here. + +48 +00:02:03,000 --> 00:02:05,000 +So it says the application failed is + +49 +00:02:05,000 --> 00:02:08,000 +because some properties are missing. + +50 +00:02:08,000 --> 00:02:11,000 +So it's not found, now how do you set this properties? + +51 +00:02:11,000 --> 00:02:13,000 +So basically what happens is behind the scene, + +52 +00:02:13,000 --> 00:02:16,000 +this uses some classes which needs to set some properties. + +53 +00:02:16,000 --> 00:02:21,000 +The thing is, if your application is allowing for Google + +54 +00:02:21,000 --> 00:02:25,000 +to have the login, you need to have a developer account + +55 +00:02:25,000 --> 00:02:27,000 +or developer console. + +56 +00:02:27,000 --> 00:02:29,000 +So everyone, I mean you can + +57 +00:02:29,000 --> 00:02:30,000 +create your own developer console. + +58 +00:02:30,000 --> 00:02:31,000 +What I'm talking about is this. + +59 +00:02:31,000 --> 00:02:32,000 +If you go to your browser + +60 +00:02:32,000 --> 00:02:36,000 +and search for Google cloud console, so search + +61 +00:02:36,000 --> 00:02:39,000 +for this Google Cloud console, it'll take you to this page. + +62 +00:02:39,000 --> 00:02:40,000 +Otherwise you can just go + +63 +00:02:40,000 --> 00:02:44,000 +to the address plan type cloud.google.com/cloudconsole + +64 +00:02:45,000 --> 00:02:47,000 +or cloud-console. + +65 +00:02:47,000 --> 00:02:49,000 +And then you will see this option of going to console. + +66 +00:02:49,000 --> 00:02:52,000 +So go to console and this is where you can create your + +67 +00:02:52,000 --> 00:02:54,000 +credentials and where you will get it. + +68 +00:02:54,000 --> 00:02:59,000 +So if you click on this menu bar here and click on APIs + +69 +00:02:59,000 --> 00:03:02,000 +and services, you will find an option of credentials. + +70 +00:03:02,000 --> 00:03:05,000 +Click on this, already have two client IDs. + +71 +00:03:05,000 --> 00:03:07,000 +Just ignore this one, let's create a new one. + +72 +00:03:07,000 --> 00:03:10,000 +Now if you are doing this for the first time, + +73 +00:03:10,000 --> 00:03:12,000 +you will not see this OAuth consent screen. + +74 +00:03:12,000 --> 00:03:15,000 +Just make sure that you fill this details + +75 +00:03:15,000 --> 00:03:18,000 +and where you will get this on this page itself, + +76 +00:03:18,000 --> 00:03:22,000 +you will see that you don't have the consent completed. + +77 +00:03:22,000 --> 00:03:24,000 +Just complete that, so it'll ask you + +78 +00:03:24,000 --> 00:03:27,000 +for the application name, it'll ask you for certain details, + +79 +00:03:27,000 --> 00:03:30,000 +just say, okay, and only fill the composite stuff, + +80 +00:03:30,000 --> 00:03:31,000 +otherwise we can keep it default. + +81 +00:03:31,000 --> 00:03:33,000 +Now, once you have done that, you can click on create + +82 +00:03:33,000 --> 00:03:37,000 +credentials and click on OAuth client ID. + +83 +00:03:37,000 --> 00:03:39,000 +This is what you're creating, and from here you have + +84 +00:03:39,000 --> 00:03:41,000 +to select the application type. + +85 +00:03:41,000 --> 00:03:45,000 +The application type we are doing is web application. + +86 +00:03:45,000 --> 00:03:47,000 +So you can also do it for the Android iOS, + +87 +00:03:47,000 --> 00:03:48,000 +but we are doing for the web application. + +88 +00:03:48,000 --> 00:03:50,000 +And let's name something. + +89 +00:03:50,000 --> 00:03:53,000 +So I will say sample app, I mean + +90 +00:03:53,000 --> 00:03:55,000 +of course you can name it anything you want. + +91 +00:03:55,000 --> 00:03:56,000 +So I'm seeing sample app + +92 +00:03:56,000 --> 00:03:58,000 +and you can see I've already done this, + +93 +00:03:58,000 --> 00:04:01,000 +my OAuth consent screen. + +94 +00:04:01,000 --> 00:04:03,000 +The next thing you have to mention is + +95 +00:04:03,000 --> 00:04:05,000 +the authorized redirect URL. + +96 +00:04:05,000 --> 00:04:06,000 +Now this is where you have to mention + +97 +00:04:06,000 --> 00:04:09,000 +whenever you click that button. + +98 +00:04:09,000 --> 00:04:10,000 +So it'll give you a button of Google, + +99 +00:04:10,000 --> 00:04:12,000 +which URL it should go to. + +100 +00:04:12,000 --> 00:04:13,000 +And I'll mention the URL here. + +101 +00:04:13,000 --> 00:04:15,000 +Now since I'm doing it on the local host, + +102 +00:04:15,000 --> 00:04:17,000 +I'll be using this URL. + +103 +00:04:17,000 --> 00:04:19,000 +If you already have your application deployed + +104 +00:04:19,000 --> 00:04:23,000 +and you have a URL, public URL, you can mention that here. + +105 +00:04:23,000 --> 00:04:27,000 +And you have to say /login/oauth2/code/Google. + +106 +00:04:27,000 --> 00:04:32,000 +And since the portal I'm using is 8,000, I will modify that. + +107 +00:04:32,000 --> 00:04:37,000 +So do mention this particular URL here and click on create. + +108 +00:04:37,000 --> 00:04:39,000 +That's it, it's that simple. + +109 +00:04:39,000 --> 00:04:41,000 +Now what it'll give you, it'll give you two things. + +110 +00:04:41,000 --> 00:04:43,000 +One, it'll give you the client ID. + +111 +00:04:43,000 --> 00:04:47,000 +I will just copy this and have it somewhere. + +112 +00:04:47,000 --> 00:04:50,000 +So I'll say client ID is this. + +113 +00:04:50,000 --> 00:04:53,000 +And next I want to also have the client secret. + +114 +00:04:53,000 --> 00:04:57,000 +So I'll say, this is client secret and done. + +115 +00:04:57,000 --> 00:04:59,000 +So I've added these two things here. + +116 +00:04:59,000 --> 00:05:01,000 +I will just use it later, and that's it. + +117 +00:05:01,000 --> 00:05:02,000 +This is the setting you have to do in the Google. + +118 +00:05:02,000 --> 00:05:06,000 +And now you have a credentials to make it work. + +119 +00:05:06,000 --> 00:05:10,000 +Now where you have to make the changes in the ID, go back + +120 +00:05:10,000 --> 00:05:12,000 +to your ID and you have to mention this detail + +121 +00:05:12,000 --> 00:05:15,000 +in the application properties, but which properties? + +122 +00:05:15,000 --> 00:05:17,000 +So the property we are working with is client ID, + +123 +00:05:17,000 --> 00:05:19,000 +but this should be for Google + +124 +00:05:19,000 --> 00:05:20,000 +and I can't see the option for Google. + +125 +00:05:20,000 --> 00:05:25,000 +So whatever, do us also spring.security.oauth2.registration + +126 +00:05:27,000 --> 00:05:31,000 +google.client-ID. + +127 +00:05:31,000 --> 00:05:32,000 +So this is the first one you have to do. + +128 +00:05:32,000 --> 00:05:34,000 +And then let's copy paste this one. + +129 +00:05:34,000 --> 00:05:38,000 +But instead of ID, I'll be saying the secret. + +130 +00:05:38,000 --> 00:05:40,000 +So these are the two details you have to mention. + +131 +00:05:40,000 --> 00:05:43,000 +We got ID and we got secret, where do we have this detail? + +132 +00:05:43,000 --> 00:05:46,000 +I've pasted that in my notes. + +133 +00:05:46,000 --> 00:05:49,000 +I will just use this ID, client ID, paste it here. + +134 +00:05:49,000 --> 00:05:53,000 +And the client's secret, this one paste here. + +135 +00:05:53,000 --> 00:05:56,000 +So I'm showing you my client ID and secret. + +136 +00:05:56,000 --> 00:05:58,000 +So I will delete that once this video is done + +137 +00:05:58,000 --> 00:06:00,000 +so that you can't misuse it. + +138 +00:06:00,000 --> 00:06:02,000 +So I will just save this and that's it. + +139 +00:06:02,000 --> 00:06:04,000 +You just have to save these details + +140 +00:06:04,000 --> 00:06:05,000 +and I hope this will work. + +141 +00:06:05,000 --> 00:06:07,000 +Just restart your application + +142 +00:06:07,000 --> 00:06:09,000 +and you can see, this time we have not got any error + +143 +00:06:09,000 --> 00:06:12,000 +because we do have these properties here. + +144 +00:06:12,000 --> 00:06:15,000 +And now if I go back to my browser, + +145 +00:06:15,000 --> 00:06:19,000 +and if I say refresh, voila, can you see that? + +146 +00:06:19,000 --> 00:06:22,000 +It says login with OAuth2, so which URL, I'm hitting. + +147 +00:06:22,000 --> 00:06:23,000 +So basically I was hitting hello, + +148 +00:06:23,000 --> 00:06:26,000 +but it is rewriting me to log in + +149 +00:06:26,000 --> 00:06:29,000 +and now it's asking me for the login here. + +150 +00:06:29,000 --> 00:06:31,000 +Okay, but then I'm not going to use my Safari + +151 +00:06:31,000 --> 00:06:33,000 +because I don't type my Google account logged in there. + +152 +00:06:33,000 --> 00:06:36,000 +What I will do is I will open Chrome and hit the same URL. + +153 +00:06:36,000 --> 00:06:39,000 +I will say enter, and you can see it has opened + +154 +00:06:39,000 --> 00:06:42,000 +my Google account directly and now I can choose + +155 +00:06:42,000 --> 00:06:44,000 +which one I want to log in through. + +156 +00:06:44,000 --> 00:06:47,000 +So I'll be saying I want to log in from this email ID. + +157 +00:06:47,000 --> 00:06:48,000 +Of course you cannot see that + +158 +00:06:48,000 --> 00:06:51,000 +because I've hidden it, hopefully, + +159 +00:06:51,000 --> 00:06:54,000 +and when I click on my email, which is already logged in, + +160 +00:06:54,000 --> 00:06:57,000 +so you can see we got welcome to the telescope. + +161 +00:06:57,000 --> 00:06:59,000 +It's so simple to implement Google login. + +162 +00:06:59,000 --> 00:07:02,000 +And even if I refresh, even if I go to some other page, + +163 +00:07:02,000 --> 00:07:04,000 +it'll still be valid. + +164 +00:07:04,000 --> 00:07:07,000 +Of course, I don't have any other page, let me say demo, + +165 +00:07:07,000 --> 00:07:10,000 +which is not there, but at least you are still logged in. + +166 +00:07:10,000 --> 00:07:13,000 +Example, if I go back to, hello, you're still logged in. + +167 +00:07:13,000 --> 00:07:15,000 +So that's how basically you can do a Google login. + +168 +00:07:15,000 --> 00:07:18,000 +But what about GitHub Login again, when that is simple. + +169 +00:07:18,000 --> 00:07:20,000 +Let's do that in the next video. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/015 Github Login_en.srt b/22 - JWT (JSON Web Token) and OAuth2/015 Github Login_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..dd45fa36513b13936b90b54d6140804a6277bf34 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/015 Github Login_en.srt @@ -0,0 +1,380 @@ +1 +00:00:03,000 --> 00:00:06,000 +-: And now let's try to add the GitHub login. + +2 +00:00:06,000 --> 00:00:08,000 +So the way we are going to do that is very simple. + +3 +00:00:08,000 --> 00:00:10,000 +We, we can see we got two properties here. + +4 +00:00:10,000 --> 00:00:14,000 +Let me just copy those properties and paste it here. + +5 +00:00:14,000 --> 00:00:19,000 +And just change this from Google to GitHub. It's so simple. + +6 +00:00:19,000 --> 00:00:21,000 +So from Google, we are changing it to GitHub + +7 +00:00:23,000 --> 00:00:24,000 +and now restart the application. + +8 +00:00:24,000 --> 00:00:27,000 +Okay, so I was started, let me go back to my Safari + +9 +00:00:27,000 --> 00:00:29,000 +where I don't have the all this account login + +10 +00:00:29,000 --> 00:00:31,000 +and I will hit the same URL. + +11 +00:00:31,000 --> 00:00:33,000 +And now you can see we got two login. + +12 +00:00:34,000 --> 00:00:36,000 +One is Google, one is GitHub. + +13 +00:00:36,000 --> 00:00:38,000 +When I click on Google, it's ask me for Google one. + +14 +00:00:38,000 --> 00:00:41,000 +But now when I hit GitHub, it's asking for GitHub details. + +15 +00:00:41,000 --> 00:00:43,000 +But the problem is there's one little problem here. + +16 +00:00:43,000 --> 00:00:44,000 +Even if you complete this, + +17 +00:00:44,000 --> 00:00:46,000 +I'm not sure if this will, this will work. + +18 +00:00:46,000 --> 00:00:49,000 +Let me try it on my Chrome. I thought it'll not work. + +19 +00:00:49,000 --> 00:00:52,000 +Why it's working. Maybe it'll give you a bit later. + +20 +00:00:52,000 --> 00:00:55,000 +So let me try from here. + +21 +00:00:55,000 --> 00:00:58,000 +Okay, so now I will log in through GitHub + +22 +00:00:59,000 --> 00:01:02,000 +and you can see it says page 4 0 4. + +23 +00:01:02,000 --> 00:01:06,000 +You know what went wrong? The the values which you have set + +24 +00:01:06,000 --> 00:01:09,000 +here is not from, from GitHub, it's from Google, right? + +25 +00:01:10,000 --> 00:01:11,000 +How this will work, as you can see, + +26 +00:01:11,000 --> 00:01:13,000 +we have Google user content here. + +27 +00:01:13,000 --> 00:01:15,000 +So this has to be changed. + +28 +00:01:15,000 --> 00:01:17,000 +But how the same thing which we done did on Google need + +29 +00:01:17,000 --> 00:01:18,000 +to be done on GitHub. + +30 +00:01:18,000 --> 00:01:20,000 +And the way you can do that is by going back + +31 +00:01:20,000 --> 00:01:22,000 +to your browser, make sure + +32 +00:01:22,000 --> 00:01:24,000 +that you're logged in in your GitHub account. + +33 +00:01:24,000 --> 00:01:26,000 +So I do have a GitHub account and that's my profile. + +34 +00:01:26,000 --> 00:01:29,000 +If you have not followed yet, do follow. + +35 +00:01:29,000 --> 00:01:34,000 +And then you have to click on your settings here. + +36 +00:01:34,000 --> 00:01:36,000 +So go to your profile, click on settings. + +37 +00:01:36,000 --> 00:01:40,000 +And in settings, if you go down, you will see an option + +38 +00:01:40,000 --> 00:01:42,000 +of developers setting. + +39 +00:01:42,000 --> 00:01:43,000 +Just click on this + +40 +00:01:44,000 --> 00:01:48,000 +and you will have an option of OAuth apps. + +41 +00:01:48,000 --> 00:01:50,000 +Click on this one. And then you have + +42 +00:01:50,000 --> 00:01:52,000 +to register a new application. + +43 +00:01:52,000 --> 00:01:54,000 +So click on this, it'll ask you for the application name. + +44 +00:01:54,000 --> 00:01:56,000 +I will say again, sample app. + +45 +00:01:56,000 --> 00:01:59,000 +Then you have to mention the homepage if you want. + +46 +00:01:59,000 --> 00:02:02,000 +And I will say, yeah, I do have a homepage URL, + +47 +00:02:02,000 --> 00:02:03,000 +http colon + +48 +00:02:04,000 --> 00:02:08,000 +slash slash local host colon 8000. + +49 +00:02:08,000 --> 00:02:11,000 +And then if you want to provide some + +50 +00:02:11,000 --> 00:02:12,000 +description, you can do that. + +51 +00:02:12,000 --> 00:02:13,000 +And then you have to provide the + +52 +00:02:13,000 --> 00:02:15,000 +authorization callback, URL. + +53 +00:02:15,000 --> 00:02:18,000 +Now this is same what we have done for the Google as well. + +54 +00:02:18,000 --> 00:02:19,000 +It's just that instead of Google, you have + +55 +00:02:19,000 --> 00:02:20,000 +to use the GitHub. + +56 +00:02:21,000 --> 00:02:23,000 +So that's the URL. But instead + +57 +00:02:23,000 --> 00:02:24,000 +of its original, have to say 8000. + +58 +00:02:24,000 --> 00:02:26,000 +But again, once your application is deployed somewhere + +59 +00:02:26,000 --> 00:02:27,000 +with a public URL + +60 +00:02:28,000 --> 00:02:31,000 +or your own domain name, you can use that here. + +61 +00:02:31,000 --> 00:02:34,000 +And yeah, this is the URL you have to mention + +62 +00:02:34,000 --> 00:02:36,000 +and click on register application and done. + +63 +00:02:36,000 --> 00:02:38,000 +And you can see we got the client id. + +64 +00:02:38,000 --> 00:02:40,000 +So I will just, I will use this, + +65 +00:02:40,000 --> 00:02:44,000 +but then if you can see, the client's secret is not there. + +66 +00:02:44,000 --> 00:02:46,000 +So you have to click on generate a new client. + +67 +00:02:46,000 --> 00:02:48,000 +And it is asking me for the authorize, + +68 +00:02:48,000 --> 00:02:53,000 +I have done the 2FA, which is two factor authentication. + +69 +00:02:54,000 --> 00:02:56,000 +I will just enter the details. + +70 +00:02:56,000 --> 00:02:59,000 +Okay, so once I have done that, I can click on this + +71 +00:02:59,000 --> 00:03:02,000 +and you can see we got the secret key. + +72 +00:03:02,000 --> 00:03:03,000 +So this is my secret key, that's my client id. + +73 +00:03:05,000 --> 00:03:06,000 +So I've copied the secret key, I'll paste it here, + +74 +00:03:07,000 --> 00:03:10,000 +and then I have to copy the client, key client ID as well. + +75 +00:03:12,000 --> 00:03:14,000 +And I'll paste it here. And that's it. + +76 +00:03:14,000 --> 00:03:16,000 +We got the details for GitHub. + +77 +00:03:16,000 --> 00:03:18,000 +Now let's relaunch the application. + +78 +00:03:18,000 --> 00:03:20,000 +And I hope this time this will work. + +79 +00:03:20,000 --> 00:03:23,000 +So I'm going to use my Chrome browser to log in to + +80 +00:03:23,000 --> 00:03:24,000 +that page. + +81 +00:03:24,000 --> 00:03:27,000 +So localhost:8080/hello, enter. + +82 +00:03:27,000 --> 00:03:29,000 +And you can see we got two logins. + +83 +00:03:29,000 --> 00:03:31,000 +Once again, if I click on Google, it'll take me + +84 +00:03:31,000 --> 00:03:33,000 +to the my login items. + +85 +00:03:33,000 --> 00:03:35,000 +But if I click on GitHub, + +86 +00:03:35,000 --> 00:03:40,000 +and you can see since I'm already logged in in my GitHub + +87 +00:03:40,000 --> 00:03:43,000 +account on this browser, it says, authorized navinreddy + +88 +00:03:43,000 --> 00:03:44,000 +I'm saying yes. + +89 +00:03:44,000 --> 00:03:46,000 +And now I'll be writing to my page and I got the login. + +90 +00:03:46,000 --> 00:03:49,000 +So it is so simple to actually have this Google login + +91 +00:03:49,000 --> 00:03:52,000 +and GitHub login or all the social media logins + +92 +00:03:52,000 --> 00:03:53,000 +in your application. + +93 +00:03:53,000 --> 00:03:55,000 +And that is possible with the help of OAuth2. + +94 +00:03:55,000 --> 00:03:57,000 +So I hope it made some sense, + +95 +00:03:57,000 --> 00:04:02,000 +and that's how we can secure our applications. + diff --git a/22 - JWT (JSON Web Token) and OAuth2/015 Source-Code.url b/22 - JWT (JSON Web Token) and OAuth2/015 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..a3279380d373c78ee7de278943f436965ebff538 --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/015 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/spring6-course/tree/80e0c059c5c259ef6009838a934ed9e92865c3d7/14.2%20OAuth2 \ No newline at end of file diff --git a/22 - JWT (JSON Web Token) and OAuth2/external-links.txt b/22 - JWT (JSON Web Token) and OAuth2/external-links.txt new file mode 100644 index 0000000000000000000000000000000000000000..751556ee99cec115221a8dc98d6115284741ce9a --- /dev/null +++ b/22 - JWT (JSON Web Token) and OAuth2/external-links.txt @@ -0,0 +1,6 @@ + +011 Source-Code +https://github.com/navinreddy20/spring6-course/tree/80e0c059c5c259ef6009838a934ed9e92865c3d7/14.1%20JWT + +015 Source-Code +https://github.com/navinreddy20/spring6-course/tree/80e0c059c5c259ef6009838a934ed9e92865c3d7/14.2%20OAuth2 diff --git a/23 - Spring AI/001 Spring AI Introduction_en.srt b/23 - Spring AI/001 Spring AI Introduction_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..209ec3cc617833fd1f9697cd2f6642dab2df87e9 --- /dev/null +++ b/23 - Spring AI/001 Spring AI Introduction_en.srt @@ -0,0 +1,448 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: So if you want to build enterprise application, + +3 +00:00:06,000 --> 00:00:07,000 +we can use Spring Boot. + +4 +00:00:07,000 --> 00:00:09,000 +In fact, it has multiple modules. + +5 +00:00:09,000 --> 00:00:12,000 +You can use Spring Web, you can use Spring JPA + +6 +00:00:12,000 --> 00:00:14,000 +and you can use Spring Security to secure it + +7 +00:00:14,000 --> 00:00:17,000 +and you can build the enterprise application. + +8 +00:00:17,000 --> 00:00:20,000 +But we are living in a world of AI now. + +9 +00:00:20,000 --> 00:00:22,000 +So of course if you want to do certain things, + +10 +00:00:22,000 --> 00:00:24,000 +you will be using AI. + +11 +00:00:24,000 --> 00:00:26,000 +If you want to write an essay, you will use AI there. + +12 +00:00:26,000 --> 00:00:29,000 +Even if you want to send an email, we use AI. + +13 +00:00:29,000 --> 00:00:31,000 +So we go to ChatGPT, or we go to Gemini + +14 +00:00:31,000 --> 00:00:33,000 +and we type our question or the prompt + +15 +00:00:33,000 --> 00:00:35,000 +and then it gives you a response. + +16 +00:00:35,000 --> 00:00:37,000 +Sometime you ask for the email + +17 +00:00:37,000 --> 00:00:38,000 +and then you copy paste the email as it is + +18 +00:00:38,000 --> 00:00:41,000 +and you send, of course we have to make some changes. + +19 +00:00:41,000 --> 00:00:42,000 +Now where I'm going with this, + +20 +00:00:42,000 --> 00:00:45,000 +see if we talk about AI solutions, they are great + +21 +00:00:45,000 --> 00:00:49,000 +and since they are great, we also want to include that + +22 +00:00:49,000 --> 00:00:50,000 +in the enterprise project. + +23 +00:00:50,000 --> 00:00:52,000 +Now the question is how, + +24 +00:00:52,000 --> 00:00:54,000 +so let's say if you are building a e-commerce website + +25 +00:00:54,000 --> 00:00:56,000 +or maybe any application for that matter. + +26 +00:00:56,000 --> 00:00:58,000 +Now in this application, + +27 +00:00:58,000 --> 00:01:01,000 +sometime you want AI features included. + +28 +00:01:01,000 --> 00:01:04,000 +Maybe if you are building a feature + +29 +00:01:04,000 --> 00:01:05,000 +where you can interact with a client + +30 +00:01:05,000 --> 00:01:08,000 +and a client is asking some questions + +31 +00:01:08,000 --> 00:01:09,000 +or the user is asking some questions, + +32 +00:01:09,000 --> 00:01:11,000 +you can add a chat bot there, right? + +33 +00:01:11,000 --> 00:01:14,000 +Which is AI bot, then you can also have, + +34 +00:01:14,000 --> 00:01:17,000 +let's say if you want to add a product in the e-commerce, + +35 +00:01:17,000 --> 00:01:19,000 +maybe you want to generate the description, + +36 +00:01:19,000 --> 00:01:21,000 +maybe you want to generate the image + +37 +00:01:21,000 --> 00:01:23,000 +and that should be done with the help of AI. + +38 +00:01:23,000 --> 00:01:25,000 +How do we add those features? + +39 +00:01:25,000 --> 00:01:28,000 +Now, one way you can simply create + +40 +00:01:28,000 --> 00:01:31,000 +UPS application in Java using Spring. + +41 +00:01:31,000 --> 00:01:34,000 +And then now since in the AI world, Python is very famous + +42 +00:01:34,000 --> 00:01:36,000 +and you'll be building this AI solution + +43 +00:01:36,000 --> 00:01:40,000 +which will give you data in your enterprise application + +44 +00:01:40,000 --> 00:01:41,000 +with the help of some other language. + +45 +00:01:41,000 --> 00:01:42,000 +Now of course you can do that, + +46 +00:01:42,000 --> 00:01:44,000 +but there will a lot of API calling happening + +47 +00:01:44,000 --> 00:01:45,000 +between two different languages + +48 +00:01:45,000 --> 00:01:48,000 +and you need two different teams who will work on them. + +49 +00:01:48,000 --> 00:01:50,000 +And to simplify that, + +50 +00:01:50,000 --> 00:01:51,000 +what if you can build this layer + +51 +00:01:51,000 --> 00:01:55,000 +which talks to the LLM models in Java itself + +52 +00:01:55,000 --> 00:01:56,000 +and that will be awesome, right? + +53 +00:01:56,000 --> 00:01:59,000 +And that's what you get in Spring AI. + +54 +00:01:59,000 --> 00:02:01,000 +Now the question is why we need LLM model? + +55 +00:02:01,000 --> 00:02:03,000 +Why I mentioned that there. + +56 +00:02:03,000 --> 00:02:05,000 +Now in this course, I'm assuming you know certain things + +57 +00:02:05,000 --> 00:02:09,000 +about AI like models, LLM models should be very specific, + +58 +00:02:09,000 --> 00:02:10,000 +which is large language models. + +59 +00:02:10,000 --> 00:02:13,000 +And you might have heard about different service providers, + +60 +00:02:13,000 --> 00:02:16,000 +like we got OpenAI, we got Anthropic, which is Cloud. + +61 +00:02:16,000 --> 00:02:18,000 +Then you can also run your models + +62 +00:02:18,000 --> 00:02:21,000 +on your local machine with the help of Ollama. + +63 +00:02:21,000 --> 00:02:23,000 +Now if you know any of this, this will make sense. + +64 +00:02:23,000 --> 00:02:25,000 +Now the thing is all these LLM models, + +65 +00:02:25,000 --> 00:02:27,000 +basically they are proprietary, right? + +66 +00:02:27,000 --> 00:02:30,000 +If you talk about OpenAI, if we talk about Anthropic + +67 +00:02:30,000 --> 00:02:33,000 +and they run their own models, of course you can use them + +68 +00:02:33,000 --> 00:02:36,000 +with the help of ChatGPT, you can use the Google's model + +69 +00:02:36,000 --> 00:02:38,000 +with the help of Gemini. + +70 +00:02:38,000 --> 00:02:41,000 +But then what if you want to use them in your application? + +71 +00:02:41,000 --> 00:02:43,000 +And that's where this particular providers + +72 +00:02:43,000 --> 00:02:46,000 +or this companies they have providing their models + +73 +00:02:46,000 --> 00:02:49,000 +as a service, you can say MAS, right? + +74 +00:02:49,000 --> 00:02:51,000 +So you can use those models as a service, + +75 +00:02:51,000 --> 00:02:54,000 +you create an account there, you add some credit + +76 +00:02:54,000 --> 00:02:56,000 +and then you can send the request to the models + +77 +00:02:56,000 --> 00:02:59,000 +and it'll give a response, right? + +78 +00:02:59,000 --> 00:03:01,000 +Of course you have to get a key as well. + +79 +00:03:01,000 --> 00:03:03,000 +Now we'll do that step by step in this course. + +80 +00:03:03,000 --> 00:03:05,000 +So you've got your Enterprise application, + +81 +00:03:05,000 --> 00:03:07,000 +you have to build something layer in between + +82 +00:03:07,000 --> 00:03:08,000 +so that you can connect with the LLM models. + +83 +00:03:08,000 --> 00:03:11,000 +Now to achieve this, we got Spring AI. + +84 +00:03:11,000 --> 00:03:12,000 +So if you go to spring AI here, + +85 +00:03:12,000 --> 00:03:15,000 +so this I'm into Spring + +86 +00:03:15,000 --> 00:03:16,000 +and if I click on projects, + +87 +00:03:16,000 --> 00:03:18,000 +you can see there's option of Spring AI. + +88 +00:03:18,000 --> 00:03:20,000 +There are a lot of projects in Spring + +89 +00:03:20,000 --> 00:03:21,000 +you might have seen before. + +90 +00:03:21,000 --> 00:03:23,000 +And Spring AI is a new feature. + +91 +00:03:23,000 --> 00:03:28,000 +Now Spring AI is a application framework for AI engineering. + +92 +00:03:28,000 --> 00:03:31,000 +So we got this new term, a fancy term, but it makes sense. + +93 +00:03:31,000 --> 00:03:35,000 +So you can use AI features in your application + +94 +00:03:35,000 --> 00:03:38,000 +and you can enhance your application in that sense. + +95 +00:03:38,000 --> 00:03:40,000 +So basically whatever application you have, + +96 +00:03:40,000 --> 00:03:42,000 +you can inject generative AI in that. + +97 +00:03:42,000 --> 00:03:45,000 +So basically you can get your data in Gen AI + +98 +00:03:45,000 --> 00:03:48,000 +and you can use the Gen AI features in your application. + +99 +00:03:48,000 --> 00:03:50,000 +That's what Spring AI provides you. + +100 +00:03:50,000 --> 00:03:53,000 +Now it's not difficult if you know Spring, + +101 +00:03:53,000 --> 00:03:55,000 +it's not difficult if you know how a model works + +102 +00:03:55,000 --> 00:03:57,000 +and how do you send a prompt + +103 +00:03:57,000 --> 00:04:00,000 +and you will learn all those things step by step anyway. + +104 +00:04:00,000 --> 00:04:02,000 +So it provides a different feature. + +105 +00:04:02,000 --> 00:04:04,000 +So there are different model type + +106 +00:04:04,000 --> 00:04:05,000 +it supports like chat completion, + +107 +00:04:05,000 --> 00:04:07,000 +embedding text to image, audio transcription, + +108 +00:04:07,000 --> 00:04:09,000 +text to speech and moderation. + +109 +00:04:09,000 --> 00:04:12,000 +And we'll try to do maximum of it. + +110 +00:04:12,000 --> 00:04:13,000 +So this is Spring AI. + +111 +00:04:13,000 --> 00:04:15,000 +So which basically a layer in between your application + +112 +00:04:15,000 --> 00:04:20,000 +and the LLM models, it makes your work very, very easy. + diff --git a/23 - Spring AI/002 Why Spring AI_en.srt b/23 - Spring AI/002 Why Spring AI_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..64fd464568282e95541de2f9dcd17e032fc92710 --- /dev/null +++ b/23 - Spring AI/002 Why Spring AI_en.srt @@ -0,0 +1,416 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now we know basically to inject the AI + +3 +00:00:06,000 --> 00:00:08,000 +or gen AI features in your application, + +4 +00:00:08,000 --> 00:00:10,000 +you need Spring AI in between. + +5 +00:00:10,000 --> 00:00:13,000 +And we have also talked about different LLM providers. + +6 +00:00:13,000 --> 00:00:16,000 +So LLM providers, we have OpenAI, we got Anthropic, + +7 +00:00:16,000 --> 00:00:17,000 +and then we have list of them, + +8 +00:00:17,000 --> 00:00:20,000 +like we also have Azure OpenAI, + +9 +00:00:20,000 --> 00:00:21,000 +and we'll see the list later. + +10 +00:00:21,000 --> 00:00:23,000 +But if you talk about those LLM models + +11 +00:00:23,000 --> 00:00:25,000 +and if you have your application, + +12 +00:00:25,000 --> 00:00:28,000 +can you just skip this Spring AI in between + +13 +00:00:28,000 --> 00:00:32,000 +and connect your application with OpenAI or Anthropic? + +14 +00:00:32,000 --> 00:00:33,000 +Actually, you can do that. + +15 +00:00:33,000 --> 00:00:34,000 +So let me go to OpenAI. + +16 +00:00:34,000 --> 00:00:37,000 +And of course, once we start using OpenAI + +17 +00:00:37,000 --> 00:00:39,000 +in your application, since it's not free, + +18 +00:00:39,000 --> 00:00:40,000 +basically you have to add the credits. + +19 +00:00:40,000 --> 00:00:41,000 +We'll see that later. + +20 +00:00:41,000 --> 00:00:42,000 +Now, once you have credits, + +21 +00:00:42,000 --> 00:00:44,000 +let's say you have your OpenAI account, + +22 +00:00:44,000 --> 00:00:48,000 +you got the developer account there, and then you got API. + +23 +00:00:48,000 --> 00:00:51,000 +How do you request, or how do you send a request to OpenAI + +24 +00:00:51,000 --> 00:00:52,000 +with your prompt? + +25 +00:00:52,000 --> 00:00:55,000 +Example, let's say if you want to listen to a joke + +26 +00:00:55,000 --> 00:00:57,000 +or maybe you want to understand a particular topic, + +27 +00:00:57,000 --> 00:00:59,000 +when you send a prompt, + +28 +00:00:59,000 --> 00:01:01,000 +it should go to OpenAI and it should respond. + +29 +00:01:01,000 --> 00:01:03,000 +Now to do that in your code, + +30 +00:01:03,000 --> 00:01:07,000 +OpenAI, they provide SDK for different languages. + +31 +00:01:07,000 --> 00:01:09,000 +Example, if I go down, so I'm into libraries + +32 +00:01:09,000 --> 00:01:12,000 +of OpenAI platform console. + +33 +00:01:12,000 --> 00:01:15,000 +So you can see there are different SDK available. + +34 +00:01:15,000 --> 00:01:17,000 +We got for JavaScript, Python, .NET, and Java. + +35 +00:01:17,000 --> 00:01:19,000 +We are focusing on Java here. + +36 +00:01:19,000 --> 00:01:23,000 +And if you go down, you can create a simple project, + +37 +00:01:23,000 --> 00:01:26,000 +a Maven project in which you can add this dependency, + +38 +00:01:26,000 --> 00:01:27,000 +which is OpenAI Java. + +39 +00:01:27,000 --> 00:01:30,000 +And once you do that, if you go down, + +40 +00:01:30,000 --> 00:01:32,000 +this is the code which you have to work with. + +41 +00:01:32,000 --> 00:01:37,000 +So basically you import the OpenAI library or the packages. + +42 +00:01:37,000 --> 00:01:39,000 +Basically you have to add your key + +43 +00:01:39,000 --> 00:01:41,000 +in the environment variable so it will read it, + +44 +00:01:41,000 --> 00:01:44,000 +and you just have to make sure that it is OPENAI_API_KEY. + +45 +00:01:44,000 --> 00:01:47,000 +And once you do that, you can create this + +46 +00:01:47,000 --> 00:01:49,000 +and you can send your prompt, this is your prompt here. + +47 +00:01:49,000 --> 00:01:50,000 +Say this is a test, or maybe you can say, + +48 +00:01:50,000 --> 00:01:52,000 +tell me a joke or anything. + +49 +00:01:52,000 --> 00:01:54,000 +So you can mention your prompt here, + +50 +00:01:54,000 --> 00:01:57,000 +you can mention the model which you want to run off OpenAI. + +51 +00:01:57,000 --> 00:01:59,000 +And then you will say, build and create. + +52 +00:01:59,000 --> 00:02:01,000 +It should send you a response. + +53 +00:02:01,000 --> 00:02:03,000 +Such a simple, simple stuff. + +54 +00:02:03,000 --> 00:02:06,000 +So whatever application you have, you can use this code + +55 +00:02:06,000 --> 00:02:10,000 +just by adding this one dependency and this will work. + +56 +00:02:10,000 --> 00:02:14,000 +If this will work, then why we are moving towards Spring AI? + +57 +00:02:14,000 --> 00:02:15,000 +Doesn't make sense, right? + +58 +00:02:15,000 --> 00:02:17,000 +So we can directly do that. + +59 +00:02:17,000 --> 00:02:20,000 +But if you go to Anthropic, which is one of the, + +60 +00:02:20,000 --> 00:02:23,000 +again, cloud provider, and if you see their SDK, + +61 +00:02:23,000 --> 00:02:25,000 +again, there's a problem, they are still into beta. + +62 +00:02:25,000 --> 00:02:27,000 +Of course there it will become generally + +63 +00:02:27,000 --> 00:02:29,000 +available in some time. + +64 +00:02:29,000 --> 00:02:30,000 +But if you look at the code, + +65 +00:02:30,000 --> 00:02:33,000 +the code looks different than OpenAI. + +66 +00:02:33,000 --> 00:02:34,000 +Of course this code will also work, + +67 +00:02:34,000 --> 00:02:38,000 +but then OpenAI has a different way of working with it. + +68 +00:02:38,000 --> 00:02:41,000 +Anthropic has a different way of working with it. + +69 +00:02:41,000 --> 00:02:43,000 +And if you're using only one model, + +70 +00:02:43,000 --> 00:02:44,000 +it makes sense to use any of them. + +71 +00:02:44,000 --> 00:02:46,000 +But let's say if you want to build an application + +72 +00:02:46,000 --> 00:02:49,000 +with multiple models and if you want to interact with them + +73 +00:02:49,000 --> 00:02:52,000 +and maybe in future you want to shift to another model, + +74 +00:02:52,000 --> 00:02:54,000 +you have to basically change a lot of code. + +75 +00:02:54,000 --> 00:02:57,000 +And that's why we think we need abstraction here. + +76 +00:02:57,000 --> 00:03:01,000 +We don't want to write a specific model code + +77 +00:03:01,000 --> 00:03:02,000 +in your application. + +78 +00:03:02,000 --> 00:03:03,000 +And in future, if it changes, + +79 +00:03:03,000 --> 00:03:04,000 +you have to change a lot of stuff. + +80 +00:03:04,000 --> 00:03:06,000 +And we don't just have two. + +81 +00:03:06,000 --> 00:03:10,000 +We have a list of LLM models which provides their services. + +82 +00:03:10,000 --> 00:03:12,000 +And how do you provide that abstraction? + +83 +00:03:12,000 --> 00:03:14,000 +Of course, you can manually write the code + +84 +00:03:14,000 --> 00:03:16,000 +to create an abstraction layer, + +85 +00:03:16,000 --> 00:03:18,000 +and in that you can create functions + +86 +00:03:18,000 --> 00:03:20,000 +which will call individual models. + +87 +00:03:20,000 --> 00:03:23,000 +But why you are wasting so much of time + +88 +00:03:23,000 --> 00:03:26,000 +when Spring AI says, let me handle it. + +89 +00:03:26,000 --> 00:03:28,000 +So basically Spring AI provides you those features + +90 +00:03:28,000 --> 00:03:30,000 +of abstraction and that's why if you, + +91 +00:03:30,000 --> 00:03:32,000 +intentionally, I skipped it somewhere. + +92 +00:03:32,000 --> 00:03:35,000 +It provides the abstraction for all those things. + +93 +00:03:35,000 --> 00:03:36,000 +Maybe in the documentation itself they have mentioned that. + +94 +00:03:36,000 --> 00:03:39,000 +Now Spring AI provide that abstraction + +95 +00:03:39,000 --> 00:03:42,000 +and you can see it supports major AI models + +96 +00:03:42,000 --> 00:03:45,000 +provided like Anthropic, OpenAI, Microsoft, Amazon, + +97 +00:03:45,000 --> 00:03:47,000 +Google, and Ollama. + +98 +00:03:47,000 --> 00:03:49,000 +And we're gonna see this, at least three of them for sure, + +99 +00:03:49,000 --> 00:03:51,000 +Anthropic, OpenAI, and Ollama, + +100 +00:03:51,000 --> 00:03:52,000 +because we are going to run something + +101 +00:03:52,000 --> 00:03:54,000 +on the local machine as well. + +102 +00:03:54,000 --> 00:03:58,000 +And for that abstraction, we are going to use Spring AI. + +103 +00:03:58,000 --> 00:04:00,000 +And most of the companies, they need abstraction so + +104 +00:04:00,000 --> 00:04:05,000 +that they can make their code look clean and easy to manage. + diff --git a/23 - Spring AI/003 Spring AI Docs_en.srt b/23 - Spring AI/003 Spring AI Docs_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..5be9aaf9ff95e170e96dc9c191dc0c0d7ed65a01 --- /dev/null +++ b/23 - Spring AI/003 Spring AI Docs_en.srt @@ -0,0 +1,284 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now let's talk about the Spring AI documentation. + +3 +00:00:06,000 --> 00:00:08,000 +Of course, in the video course we'll go through step + +4 +00:00:08,000 --> 00:00:11,000 +by step, but normally it's better + +5 +00:00:11,000 --> 00:00:12,000 +to refer documentation as well. + +6 +00:00:12,000 --> 00:00:14,000 +If you ask if you have some questions + +7 +00:00:14,000 --> 00:00:17,000 +or just give a look on the documentation. + +8 +00:00:17,000 --> 00:00:19,000 +So when you go to the Spring page + +9 +00:00:19,000 --> 00:00:21,000 +or Spring AI page here in the projects, + +10 +00:00:21,000 --> 00:00:23,000 +and when you click on learn, this is + +11 +00:00:23,000 --> 00:00:25,000 +where you will get the reference documentation. + +12 +00:00:25,000 --> 00:00:29,000 +And if you observe, yes, it is still into SNAPSHOT + +13 +00:00:29,000 --> 00:00:31,000 +soon you'll be getting the GA version. + +14 +00:00:31,000 --> 00:00:32,000 +Or if you want to try some extra stuff, + +15 +00:00:32,000 --> 00:00:35,000 +you can also check out the M6 here. + +16 +00:00:35,000 --> 00:00:37,000 +But I will just go with the snapshot version, + +17 +00:00:37,000 --> 00:00:39,000 +click on reference documentation, + +18 +00:00:39,000 --> 00:00:43,000 +and this is the awesome documentation of Spring AI. + +19 +00:00:43,000 --> 00:00:45,000 +And maybe somewhere here they will + +20 +00:00:45,000 --> 00:00:46,000 +talk about the abstraction. + +21 +00:00:46,000 --> 00:00:48,000 +So Spring AI project aimed to stream the development + +22 +00:00:48,000 --> 00:00:53,000 +of application and yeah, without the unnecessary complexity. + +23 +00:00:53,000 --> 00:00:55,000 +And this was important. + +24 +00:00:55,000 --> 00:00:56,000 +And of course, + +25 +00:00:56,000 --> 00:00:59,000 +you know at the start we mentioned about you can use + +26 +00:00:59,000 --> 00:01:01,000 +different languages like Python to achieve that, + +27 +00:01:01,000 --> 00:01:06,000 +to integrate your existing application with LLM, you can do + +28 +00:01:06,000 --> 00:01:08,000 +that with help of LangChain, + +29 +00:01:08,000 --> 00:01:10,000 +but again, it's better to use Java itself there. + +30 +00:01:10,000 --> 00:01:13,000 +And Spring AI is inspired by LangChain, + +31 +00:01:13,000 --> 00:01:15,000 +it's not exact replacement, + +32 +00:01:15,000 --> 00:01:16,000 +but you can do a lot of things + +33 +00:01:16,000 --> 00:01:18,000 +which you can do in LangChain. + +34 +00:01:18,000 --> 00:01:20,000 +And if you go down it talks about + +35 +00:01:20,000 --> 00:01:21,000 +the same thing, which we have. + +36 +00:01:21,000 --> 00:01:22,000 +And yeah, this is the line. + +37 +00:01:22,000 --> 00:01:24,000 +So Spring AI provides abstraction. + +38 +00:01:24,000 --> 00:01:26,000 +That's always a foundation for developing AI application. + +39 +00:01:26,000 --> 00:01:28,000 +So we don't have to work with those complex code + +40 +00:01:28,000 --> 00:01:30,000 +and different code for each model. + +41 +00:01:30,000 --> 00:01:31,000 +So therefore this documentation, + +42 +00:01:31,000 --> 00:01:32,000 +it talks about different stuff. + +43 +00:01:32,000 --> 00:01:34,000 +In fact, we also have a concept of RAG, + +44 +00:01:34,000 --> 00:01:36,000 +which we're gonna see later. + +45 +00:01:36,000 --> 00:01:38,000 +Then we got different ways of interacting + +46 +00:01:38,000 --> 00:01:40,000 +with the LLM models. + +47 +00:01:40,000 --> 00:01:42,000 +We got chat lines. We also have chat models. + +48 +00:01:42,000 --> 00:01:44,000 +In fact, if we go to Chat AI models, + +49 +00:01:44,000 --> 00:01:46,000 +it also talks about chat model. + +50 +00:01:46,000 --> 00:01:49,000 +And for different models, we got different chat models. + +51 +00:01:49,000 --> 00:01:52,000 +Okay, I know that sounds weird, but it actually makes sense. + +52 +00:01:52,000 --> 00:01:55,000 +So if you are using OpenAI, you got OpenAI chat model. + +53 +00:01:55,000 --> 00:01:57,000 +If you're using Anthropic, you've got Anthropic chat model. + +54 +00:01:57,000 --> 00:02:00,000 +And it talks about those things here + +55 +00:02:00,000 --> 00:02:02,000 +and look at the least here. + +56 +00:02:02,000 --> 00:02:05,000 +So we got Anthropic, we got Azure OpenAI, we've got + +57 +00:02:05,000 --> 00:02:09,000 +DeepSeek, Vertex AI, Gog, HuggingFace, Mistral. + +58 +00:02:10,000 --> 00:02:12,000 +Not sure about this, + +59 +00:02:12,000 --> 00:02:15,000 +but yeah, we got Moonshot, NVIDIA model, + +60 +00:02:15,000 --> 00:02:18,000 +and OpenAI, we also got Ollama here. + +61 +00:02:18,000 --> 00:02:20,000 +So there's a list of models you can work with. + +62 +00:02:20,000 --> 00:02:22,000 +And day by day they're increasing this list here + +63 +00:02:22,000 --> 00:02:25,000 +and it'll be fun to work with those models as well. + +64 +00:02:25,000 --> 00:02:28,000 +And think AI got undergone a lot of updates. + +65 +00:02:28,000 --> 00:02:31,000 +And finally we are into one. + +66 +00:02:31,000 --> 00:02:32,000 +I hope they will not make major changes + +67 +00:02:32,000 --> 00:02:35,000 +after the release of this course, and it should work. + +68 +00:02:35,000 --> 00:02:37,000 +Anyway, if they're updating, I will just update + +69 +00:02:37,000 --> 00:02:39,000 +the course according to it. + +70 +00:02:39,000 --> 00:02:41,000 +So yeah, that's about this documentation. + +71 +00:02:41,000 --> 00:02:45,000 +And now let's get started with the project. + diff --git a/23 - Spring AI/004 Creating a Spring AI Project_en.srt b/23 - Spring AI/004 Creating a Spring AI Project_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..d3f77a70634817062f40ec7c427859a843558e11 --- /dev/null +++ b/23 - Spring AI/004 Creating a Spring AI Project_en.srt @@ -0,0 +1,300 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now let's create a Spring Boot project, + +3 +00:00:05,000 --> 00:00:08,000 +which will have the Spring AI module. + +4 +00:00:08,000 --> 00:00:10,000 +Okay, to do that, let me go to the Spring Initializr, + +5 +00:00:10,000 --> 00:00:12,000 +which is start.spring.io. + +6 +00:00:12,000 --> 00:00:15,000 +And of course if you have the ultimate version of IntelliJ, + +7 +00:00:15,000 --> 00:00:18,000 +you can directly create from there or Eclipse. + +8 +00:00:18,000 --> 00:00:20,000 +But here we are using community version. + +9 +00:00:20,000 --> 00:00:22,000 +So let's do that from this place. + +10 +00:00:22,000 --> 00:00:24,000 +And the project type I'm going to create is MAVEN, + +11 +00:00:24,000 --> 00:00:27,000 +language is Java, Spring Boot version is this, + +12 +00:00:27,000 --> 00:00:30,000 +and the group ID is com.telusko. + +13 +00:00:30,000 --> 00:00:33,000 +I will name this project as SpringAICode. + +14 +00:00:33,000 --> 00:00:35,000 +And I'll not change much here, + +15 +00:00:35,000 --> 00:00:37,000 +but what dependency I'm going to add here. + +16 +00:00:37,000 --> 00:00:39,000 +So first of all, I want to make a web application + +17 +00:00:39,000 --> 00:00:42,000 +so it makes sense to add Spring Web there. + +18 +00:00:42,000 --> 00:00:45,000 +Next I want to add the Spring AI. + +19 +00:00:45,000 --> 00:00:48,000 +Now, as we have seen before, we got different models, right? + +20 +00:00:48,000 --> 00:00:49,000 +And for different models, + +21 +00:00:49,000 --> 00:00:52,000 +we have to add different dependencies. + +22 +00:00:52,000 --> 00:00:53,000 +So I'll click on add dependency here. + +23 +00:00:53,000 --> 00:00:55,000 +Now based on which model you're working with, + +24 +00:00:55,000 --> 00:00:57,000 +so if you're working with OpenAI, + +25 +00:00:57,000 --> 00:01:00,000 +you can just type OpenAI and you can select this one. + +26 +00:01:00,000 --> 00:01:03,000 +So make sure that you select OpenAI, not Azure OpenAI. + +27 +00:01:03,000 --> 00:01:06,000 +And let's say in future, if you want to work with Anthropic, + +28 +00:01:06,000 --> 00:01:09,000 +so you will just type Anthropic as well. + +29 +00:01:09,000 --> 00:01:12,000 +And you can add this one if you want to work with Ollama, + +30 +00:01:12,000 --> 00:01:14,000 +you will just type Ollama and you will add this. + +31 +00:01:14,000 --> 00:01:16,000 +So there are different dependencies available, + +32 +00:01:16,000 --> 00:01:17,000 +which you can add here. + +33 +00:01:17,000 --> 00:01:19,000 +Example, if you want to work with Vertex, + +34 +00:01:19,000 --> 00:01:20,000 +we got Vertex as well. + +35 +00:01:20,000 --> 00:01:23,000 +But here we just wanted OpenAI, just add that one. + +36 +00:01:23,000 --> 00:01:26,000 +So two dependencies, Spring Web and OpenAI. + +37 +00:01:26,000 --> 00:01:28,000 +Now, once you add these two things, + +38 +00:01:28,000 --> 00:01:29,000 +in fact, you can also explore + +39 +00:01:29,000 --> 00:01:32,000 +what dependencies it is adding. + +40 +00:01:32,000 --> 00:01:35,000 +And if I go down, so we got these starter-web + +41 +00:01:35,000 --> 00:01:40,000 +and we got the spring-ai-openai-spring-boot-starter. + +42 +00:01:40,000 --> 00:01:42,000 +So we need this two, click on generate. + +43 +00:01:42,000 --> 00:01:44,000 +It'll download the project. + +44 +00:01:44,000 --> 00:01:45,000 +So I've started the project, + +45 +00:01:45,000 --> 00:01:46,000 +I will just open that from here. + +46 +00:01:46,000 --> 00:01:48,000 +We'll click on open and we got our project here. + +47 +00:01:48,000 --> 00:01:49,000 +So if you open the POM file, + +48 +00:01:49,000 --> 00:01:52,000 +this are the two things which we have added. + +49 +00:01:52,000 --> 00:01:54,000 +So you can see we got two dependencies. + +50 +00:01:54,000 --> 00:01:57,000 +So one is the OpenAI's spring-boot-starter + +51 +00:01:57,000 --> 00:01:59,000 +and the spring-starter-web. + +52 +00:02:00,000 --> 00:02:02,000 +Now in this, if you expand this, + +53 +00:02:02,000 --> 00:02:04,000 +in the main, you don't have much code + +54 +00:02:04,000 --> 00:02:06,000 +because this is simple web application, right? + +55 +00:02:06,000 --> 00:02:09,000 +And of course when you run this, it'll run on the Tomcat. + +56 +00:02:09,000 --> 00:02:10,000 +And when you expand the resources, + +57 +00:02:10,000 --> 00:02:13,000 +we got application properties, which has only one line. + +58 +00:02:13,000 --> 00:02:15,000 +Let's see what happens when you try to run this code. + +59 +00:02:15,000 --> 00:02:18,000 +Of course, we have not done any anything much. + +60 +00:02:18,000 --> 00:02:20,000 +Let's right click here and say run. + +61 +00:02:20,000 --> 00:02:25,000 +It's trying to run this and it will fail. + +62 +00:02:25,000 --> 00:02:26,000 +Okay, now there's tricky, right? + +63 +00:02:26,000 --> 00:02:29,000 +We have not done much code, but still it's failing. + +64 +00:02:29,000 --> 00:02:30,000 +What's the reason it is failing? + +65 +00:02:30,000 --> 00:02:33,000 +So, if you go here and if you scroll, + +66 +00:02:33,000 --> 00:02:38,000 +so you can see it says, OpenAI API key must be set. + +67 +00:02:38,000 --> 00:02:40,000 +So this is the property which is missing, + +68 +00:02:40,000 --> 00:02:43,000 +spring.ai.openai.ai-key. + +69 +00:02:43,000 --> 00:02:47,000 +We have to set this property. + +70 +00:02:47,000 --> 00:02:48,000 +So I'll just go to application properties + +71 +00:02:48,000 --> 00:02:52,000 +and let's say paste equal to but, + +72 +00:02:52,000 --> 00:02:54,000 +what is the key? + +73 +00:02:54,000 --> 00:02:56,000 +We have to get this done, right? + +74 +00:02:56,000 --> 00:02:57,000 +Now, how do you get that key? + +75 +00:02:57,000 --> 00:02:59,000 +Let's see, in the next video. + diff --git a/23 - Spring AI/004 Source-code.url b/23 - Spring AI/004 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..78e9f9f55ec821378c5319917bfe7f46ba844bf2 --- /dev/null +++ b/23 - Spring AI/004 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_04 \ No newline at end of file diff --git a/23 - Spring AI/005 Create OPENAI API Key_en.srt b/23 - Spring AI/005 Create OPENAI API Key_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..d4bbee0a8777cadf1557e35b3525e40e3e98a756 --- /dev/null +++ b/23 - Spring AI/005 Create OPENAI API Key_en.srt @@ -0,0 +1,388 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now let's see, how do you create this key? + +3 +00:00:05,000 --> 00:00:08,000 +Now of course, this time we are creating key for OpenAI. + +4 +00:00:08,000 --> 00:00:11,000 +So in future, if you're working with some other model, + +5 +00:00:11,000 --> 00:00:13,000 +you have to specify that key here. + +6 +00:00:13,000 --> 00:00:16,000 +Now, to achieve that, I will go back to my browser + +7 +00:00:16,000 --> 00:00:17,000 +and let's go to OpenAI. + +8 +00:00:17,000 --> 00:00:22,000 +So I'm on the homepage of openai.com + +9 +00:00:22,000 --> 00:00:24,000 +and you will see an option here of login. + +10 +00:00:24,000 --> 00:00:27,000 +And when you do that, it will give you certain options. + +11 +00:00:27,000 --> 00:00:30,000 +And I'll be selecting the API platform, + +12 +00:00:30,000 --> 00:00:32,000 +not the ChatGPT. + +13 +00:00:32,000 --> 00:00:33,000 +And when you click on this, of course it will ask you + +14 +00:00:33,000 --> 00:00:36,000 +for username, password, which is not stored. + +15 +00:00:36,000 --> 00:00:37,000 +I've already done that. + +16 +00:00:37,000 --> 00:00:41,000 +So okay, the first page it gives you code as well. + +17 +00:00:41,000 --> 00:00:43,000 +Okay, we don't have the code for Java. That's weird. + +18 +00:00:43,000 --> 00:00:45,000 +But anyway, we are not going to do that from here. + +19 +00:00:45,000 --> 00:00:47,000 +And where do you see the key? + +20 +00:00:47,000 --> 00:00:50,000 +Now before we do that, I want to show you one more thing. + +21 +00:00:50,000 --> 00:00:51,000 +So here you have to click on settings. + +22 +00:00:51,000 --> 00:00:53,000 +And when you are in settings, + +23 +00:00:53,000 --> 00:00:55,000 +you will be going to billings first. + +24 +00:00:55,000 --> 00:00:57,000 +And when in here, you need to make sure + +25 +00:00:57,000 --> 00:01:00,000 +you have some grades available here. + +26 +00:01:00,000 --> 00:01:01,000 +Now you don't need this much of credit, + +27 +00:01:01,000 --> 00:01:03,000 +we are just doing some other stuff, + +28 +00:01:03,000 --> 00:01:05,000 +which for which we need grades. + +29 +00:01:05,000 --> 00:01:07,000 +If you want to get started, + +30 +00:01:07,000 --> 00:01:09,000 +you can add a few dollars, + +31 +00:01:09,000 --> 00:01:11,000 +what you check, whatever minimum required. + +32 +00:01:11,000 --> 00:01:15,000 +And you can get this, add it, add the balance here + +33 +00:01:15,000 --> 00:01:18,000 +and make sure that you don't enable the auto recharge + +34 +00:01:18,000 --> 00:01:19,000 +otherwise it will start, + +35 +00:01:19,000 --> 00:01:22,000 +keep adding the money there just to be on the safeguard. + +36 +00:01:22,000 --> 00:01:23,000 +And yeah, so add the grid. + +37 +00:01:23,000 --> 00:01:24,000 +That's very important + +38 +00:01:24,000 --> 00:01:26,000 +because these services are not free, right? + +39 +00:01:26,000 --> 00:01:28,000 +Because when you write one prompt + +40 +00:01:28,000 --> 00:01:31,000 +and when you're expecting open AI to give you the answer, + +41 +00:01:31,000 --> 00:01:33,000 +they are running huge amount of servers. + +42 +00:01:33,000 --> 00:01:34,000 +for that, they are charging, + +43 +00:01:34,000 --> 00:01:35,000 +of course they are charging more + +44 +00:01:35,000 --> 00:01:36,000 +than what it should be charged, + +45 +00:01:36,000 --> 00:01:40,000 +but I think day by day, I think the cost will go down. + +46 +00:01:40,000 --> 00:01:42,000 +And how they're getting charged, + +47 +00:01:42,000 --> 00:01:45,000 +it's based on the tokens, okay? + +48 +00:01:45,000 --> 00:01:46,000 +So the more tokens you send + +49 +00:01:46,000 --> 00:01:47,000 +or more token tokens you get, + +50 +00:01:47,000 --> 00:01:50,000 +you basically get charged on that. + +51 +00:01:50,000 --> 00:01:51,000 +So that's one. + +52 +00:01:51,000 --> 00:01:53,000 +Next, once you have your credits here, + +53 +00:01:53,000 --> 00:01:56,000 +you can go to API keys, + +54 +00:01:56,000 --> 00:01:59,000 +and this is where you can create your key. + +55 +00:01:59,000 --> 00:02:01,000 +So, in the API keys, you will see an option + +56 +00:02:01,000 --> 00:02:03,000 +of creating a new secret key. + +57 +00:02:03,000 --> 00:02:05,000 +Or you can just create, click here + +58 +00:02:05,000 --> 00:02:06,000 +and mention the name for the key. + +59 +00:02:06,000 --> 00:02:09,000 +I will say SpringAI-Demo. + +60 +00:02:09,000 --> 00:02:11,000 +So we are doing it for the practice only, + +61 +00:02:11,000 --> 00:02:13,000 +so I will just name this + +62 +00:02:13,000 --> 00:02:15,000 +and in fact, I had multiple keys which we have deleted + +63 +00:02:15,000 --> 00:02:17,000 +before the videos. + +64 +00:02:17,000 --> 00:02:20,000 +And project, I will just click on default project + +65 +00:02:20,000 --> 00:02:22,000 +and click on create. + +66 +00:02:22,000 --> 00:02:23,000 +There are multiple options here, + +67 +00:02:23,000 --> 00:02:24,000 +but just to keep it simple, + +68 +00:02:24,000 --> 00:02:26,000 +I'm just saying give all the permission + +69 +00:02:26,000 --> 00:02:27,000 +and create a secure key. + +70 +00:02:27,000 --> 00:02:29,000 +And once you do that, this is the key. + +71 +00:02:29,000 --> 00:02:33,000 +And you have to make sure that you save this key somewhere. + +72 +00:02:33,000 --> 00:02:35,000 +So have copied it and pasted it somewhere + +73 +00:02:35,000 --> 00:02:36,000 +so that you can see. + +74 +00:02:36,000 --> 00:02:38,000 +And then done. + +75 +00:02:38,000 --> 00:02:40,000 +And make sure that you don't share your key with others. + +76 +00:02:40,000 --> 00:02:41,000 +So that's one. + +77 +00:02:41,000 --> 00:02:43,000 +Now, once you have your key, + +78 +00:02:43,000 --> 00:02:46,000 +let's go back to our project here + +79 +00:02:46,000 --> 00:02:47,000 +and you have pasted the key here. + +80 +00:02:47,000 --> 00:02:49,000 +And I'll make sure that my editor + +81 +00:02:49,000 --> 00:02:51,000 +is hiding this key from you. + +82 +00:02:51,000 --> 00:02:53,000 +I don't wanna delete this code till the end of the course, + +83 +00:02:53,000 --> 00:02:55,000 +or maybe I will delete it later. + +84 +00:02:55,000 --> 00:02:55,000 +Anyway, that's no point. + +85 +00:02:55,000 --> 00:02:57,000 +The point is you need to get the key, + +86 +00:02:57,000 --> 00:02:58,000 +your own key and paste it here. + +87 +00:02:58,000 --> 00:03:01,000 +And once you do that, let's run this. + +88 +00:03:01,000 --> 00:03:04,000 +In fact, you could have added any key and it should work. + +89 +00:03:04,000 --> 00:03:06,000 +But if you really want to work with that, + +90 +00:03:06,000 --> 00:03:08,000 +you need a valid key. + +91 +00:03:08,000 --> 00:03:11,000 +And you can see now there is no error. + +92 +00:03:11,000 --> 00:03:13,000 +So it says it started on port number 80-80, + +93 +00:03:13,000 --> 00:03:16,000 +but you still can't use AI feature + +94 +00:03:16,000 --> 00:03:20,000 +because nowhere we have added the code of Spring AI. + +95 +00:03:20,000 --> 00:03:22,000 +And that will be tricky. + +96 +00:03:22,000 --> 00:03:22,000 +So how do we do that? + +97 +00:03:22,000 --> 00:03:24,000 +Let's see in the upcoming videos. + diff --git a/23 - Spring AI/006 Asking Question to Open AI Model_en.srt b/23 - Spring AI/006 Asking Question to Open AI Model_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..24766bc92c775be6f3b06760f063e1e3db196678 --- /dev/null +++ b/23 - Spring AI/006 Asking Question to Open AI Model_en.srt @@ -0,0 +1,880 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now once we have our project set up done. + +3 +00:00:06,000 --> 00:00:07,000 +So basically we got the project, + +4 +00:00:07,000 --> 00:00:10,000 +we have also created the open API key + +5 +00:00:10,000 --> 00:00:12,000 +and now we want to test it. + +6 +00:00:12,000 --> 00:00:14,000 +But the question is what you're gonna test? + +7 +00:00:14,000 --> 00:00:17,000 +And for that I'm going to open Insomnia, + +8 +00:00:17,000 --> 00:00:19,000 +which is your API client. + +9 +00:00:19,000 --> 00:00:22,000 +And here we are going to send some request. + +10 +00:00:22,000 --> 00:00:24,000 +Okay, so what we are going try to do here, + +11 +00:00:24,000 --> 00:00:26,000 +of course, we are not building a chat application + +12 +00:00:26,000 --> 00:00:28,000 +where a user will type something + +13 +00:00:28,000 --> 00:00:29,000 +AI will give you reply based on that, + +14 +00:00:29,000 --> 00:00:30,000 +you will send something. + +15 +00:00:30,000 --> 00:00:31,000 +It's just one way. + +16 +00:00:31,000 --> 00:00:34,000 +So I want to ask a question, I want a response, that's it. + +17 +00:00:34,000 --> 00:00:36,000 +All, anything you wanna talk to. + +18 +00:00:36,000 --> 00:00:38,000 +So I will create a new request. + +19 +00:00:38,000 --> 00:00:39,000 +You can click on Control, + +20 +00:00:39,000 --> 00:00:41,000 +I mean you can click on the new request + +21 +00:00:41,000 --> 00:00:43,000 +or you can say Control + N. + +22 +00:00:43,000 --> 00:00:46,000 +And you can use any API client, Postman + +23 +00:00:46,000 --> 00:00:49,000 +or you can even use a browser, even that works. + +24 +00:00:49,000 --> 00:00:51,000 +So here I'm going to send a request. + +25 +00:00:51,000 --> 00:00:53,000 +Now I know that this particular server + +26 +00:00:53,000 --> 00:00:56,000 +is going to run on port number 8080. + +27 +00:00:56,000 --> 00:00:57,000 +So I'll just run this to show you. + +28 +00:00:57,000 --> 00:00:59,000 +So if you can see this thing + +29 +00:00:59,000 --> 00:01:02,000 +is working on port number 8080 here. + +30 +00:01:02,000 --> 00:01:04,000 +So what we are gonna do is we'll send a request, + +31 +00:01:04,000 --> 00:01:09,000 +we'll say localhost:8080/, and we need to, + +32 +00:01:09,000 --> 00:01:11,000 +let's say I can type the question here itself. + +33 +00:01:11,000 --> 00:01:14,000 +I can say what is Java. + +34 +00:01:14,000 --> 00:01:16,000 +So this is what you're typing in the URL. + +35 +00:01:16,000 --> 00:01:19,000 +So in the spring you have to accept that with path variable. + +36 +00:01:19,000 --> 00:01:21,000 +And this is what they will, + +37 +00:01:21,000 --> 00:01:24,000 +it'll send you because of the spaces, I will click on Send. + +38 +00:01:24,000 --> 00:01:25,000 +And you can see we got an error. + +39 +00:01:25,000 --> 00:01:29,000 +It says 404 not found is because in the backend + +40 +00:01:29,000 --> 00:01:32,000 +we don't have any controller handling this request. + +41 +00:01:32,000 --> 00:01:33,000 +In fact, to make it more interesting, + +42 +00:01:33,000 --> 00:01:36,000 +I will say API, not interesting but specific, + +43 +00:01:36,000 --> 00:01:39,000 +I will say api/what is Java. Send. + +44 +00:01:39,000 --> 00:01:41,000 +And you can see still not working. + +45 +00:01:41,000 --> 00:01:42,000 +So what we need to do is + +46 +00:01:42,000 --> 00:01:45,000 +we have to basically send this request + +47 +00:01:45,000 --> 00:01:48,000 +and accept it on the clients, on the server side. + +48 +00:01:48,000 --> 00:01:50,000 +So we'll go back to our IntelliJ + +49 +00:01:50,000 --> 00:01:53,000 +and here I need a controller to handle that request. + +50 +00:01:53,000 --> 00:01:58,000 +So I'll say new, class, I will say this is AIController + +51 +00:01:58,000 --> 00:01:59,000 +and I want to be very specific this time, + +52 +00:01:59,000 --> 00:02:01,000 +let's say OpenAIController, + +53 +00:02:01,000 --> 00:02:04,000 +maybe in future I might add Entropik or Ollama, + +54 +00:02:04,000 --> 00:02:07,000 +so I will just change the name to EntropikAIController + +55 +00:02:07,000 --> 00:02:10,000 +or OllamaAIController. + +56 +00:02:10,000 --> 00:02:13,000 +Click on Class, and yeah, so we got a class created. + +57 +00:02:13,000 --> 00:02:15,000 +Let's make this as a REST controller. + +58 +00:02:15,000 --> 00:02:19,000 +And here I need a method which will accept the request. + +59 +00:02:19,000 --> 00:02:21,000 +So I will say public_String, let me return string, + +60 +00:02:21,000 --> 00:02:23,000 +and I'll say getResponse. + +61 +00:02:23,000 --> 00:02:25,000 +I mean we'll say getAnswer instead of saying response. + +62 +00:02:25,000 --> 00:02:27,000 +And that's the method. + +63 +00:02:27,000 --> 00:02:29,000 +And here, let's return "Hello World" + +64 +00:02:29,000 --> 00:02:30,000 +just to test if things are working out. + +65 +00:02:30,000 --> 00:02:34,000 +And I will say rest, okay, you cannot request mapping. + +66 +00:02:34,000 --> 00:02:35,000 +I would say GetMapping. + +67 +00:02:35,000 --> 00:02:38,000 +And the thing here, I will say /api. + +68 +00:02:38,000 --> 00:02:39,000 +So whenever was someone requests for the API, + +69 +00:02:39,000 --> 00:02:42,000 +this is what I want to call. Let's test this. + +70 +00:02:42,000 --> 00:02:45,000 +So I'll just restart the server, + +71 +00:02:45,000 --> 00:02:47,000 +and go back here, click on Send. + +72 +00:02:47,000 --> 00:02:51,000 +Nothing changes. And you can see it's still not working. + +73 +00:02:51,000 --> 00:02:52,000 +The server started, + +74 +00:02:52,000 --> 00:02:54,000 +maybe I'm not accepting, that's the issue. + +75 +00:02:54,000 --> 00:02:56,000 +So I will just accept in a message, + +76 +00:02:56,000 --> 00:02:58,000 +maybe I'll also print the message which we are getting + +77 +00:02:58,000 --> 00:03:00,000 +just to see if things are working out. + +78 +00:03:00,000 --> 00:03:02,000 +I will say @PathVariable + +79 +00:03:03,000 --> 00:03:06,000 +and I will store that in message + +80 +00:03:06,000 --> 00:03:08,000 +and I will print the same message, + +81 +00:03:08,000 --> 00:03:09,000 +whatever we are sending just to see. + +82 +00:03:09,000 --> 00:03:11,000 +Otherwise, if you're doing some AI stuff + +83 +00:03:11,000 --> 00:03:13,000 +and things are not working on, then it's of no use. + +84 +00:03:13,000 --> 00:03:14,000 +Click on Send. + +85 +00:03:14,000 --> 00:03:16,000 +Okay, so it says Hello World what is Java? + +86 +00:03:16,000 --> 00:03:17,000 +Things are working out. + +87 +00:03:17,000 --> 00:03:19,000 +Okay, there's no AI features of now, + +88 +00:03:19,000 --> 00:03:21,000 +we are not talking to the OpenAI server. + +89 +00:03:22,000 --> 00:03:23,000 +And we'll do that now. + +90 +00:03:23,000 --> 00:03:25,000 +So basically if these things are working, + +91 +00:03:25,000 --> 00:03:27,000 +let's add, because see this question, + +92 +00:03:27,000 --> 00:03:28,000 +whatever message you're getting here + +93 +00:03:28,000 --> 00:03:30,000 +need to be sent to the LLM server + +94 +00:03:30,000 --> 00:03:32,000 +and we have to accept the response from there. + +95 +00:03:32,000 --> 00:03:35,000 +And to do that we have to use some chat model. + +96 +00:03:35,000 --> 00:03:37,000 +So basically the Spring AI + +97 +00:03:37,000 --> 00:03:38,000 +provides you different layers, okay? + +98 +00:03:38,000 --> 00:03:40,000 +One of the layer is chat models. + +99 +00:03:40,000 --> 00:03:42,000 +So on top of your LLLM, + +100 +00:03:42,000 --> 00:03:45,000 +so basically let's say you have your Spring application + +101 +00:03:45,000 --> 00:03:46,000 +and you have Spring AI in between, + +102 +00:03:46,000 --> 00:03:48,000 +basically in Spring AI you'll be using certain features. + +103 +00:03:48,000 --> 00:03:50,000 +You got your LLM here. + +104 +00:03:50,000 --> 00:03:53,000 +The way you talk to your LLM is through chat models. + +105 +00:03:53,000 --> 00:03:55,000 +Now different LLM provide different chat models. + +106 +00:03:55,000 --> 00:03:56,000 +And Spring AI basically says, + +107 +00:03:56,000 --> 00:03:59,000 +okay, let me just do the abstraction here + +108 +00:03:59,000 --> 00:04:01,000 +and I will provide you different chat model + +109 +00:04:01,000 --> 00:04:03,000 +for different type of LLMs. + +110 +00:04:03,000 --> 00:04:04,000 +Here we are using OpenAI. + +111 +00:04:04,000 --> 00:04:07,000 +So we have to use OpenAI chat model. + +112 +00:04:07,000 --> 00:04:09,000 +And to use that, + +113 +00:04:09,000 --> 00:04:13,000 +I'll be creating the object for OpenAI chat model. + +114 +00:04:13,000 --> 00:04:15,000 +If you can see, this is what we want. + +115 +00:04:15,000 --> 00:04:18,000 +So it's from springframework.ai.openai. + +116 +00:04:18,000 --> 00:04:20,000 +And we got this because of the package which we have added. + +117 +00:04:20,000 --> 00:04:24,000 +So if you remember in the pom.xml file, + +118 +00:04:24,000 --> 00:04:26,000 +we have added this. + +119 +00:04:26,000 --> 00:04:28,000 +So because of this, we are getting this package here. + +120 +00:04:28,000 --> 00:04:33,000 +So I will say the ChatModel and I'll say chatModel. Okay? + +121 +00:04:33,000 --> 00:04:34,000 +Now once you got this reference, + +122 +00:04:34,000 --> 00:04:36,000 +of course, we want the object + +123 +00:04:36,000 --> 00:04:38,000 +and I don't want to manually create the object. + +124 +00:04:38,000 --> 00:04:39,000 +I will hand over to Spring. + +125 +00:04:41,000 --> 00:04:42,000 +So it'll do the dependency injection. + +126 +00:04:42,000 --> 00:04:43,000 +So this is how you do it. + +127 +00:04:43,000 --> 00:04:46,000 +So in the construct of a controller, you take this object + +128 +00:04:46,000 --> 00:04:50,000 +or you declare your parametrized constructor with chat model + +129 +00:04:50,000 --> 00:04:51,000 +and you assign it here. + +130 +00:04:51,000 --> 00:04:52,000 +So basically we're trying to do, + +131 +00:04:52,000 --> 00:04:54,000 +construct the injection here. + +132 +00:04:54,000 --> 00:04:56,000 +And once you've got this chat model object, + +133 +00:04:56,000 --> 00:04:59,000 +you will come back here and ensure we're doing this. + +134 +00:04:59,000 --> 00:05:00,000 +I will return. + +135 +00:05:00,000 --> 00:05:01,000 +Okay, first of all, I want to get the response. + +136 +00:05:01,000 --> 00:05:04,000 +I will say String response =, + +137 +00:05:04,000 --> 00:05:06,000 +this is a variable I want to store the response. + +138 +00:05:06,000 --> 00:05:08,000 +So based on the question or whatever message you have, + +139 +00:05:08,000 --> 00:05:11,000 +you wanna talk to LLM and you will get the response. + +140 +00:05:11,000 --> 00:05:13,000 +But how you talk to the LLM? It's very simple. + +141 +00:05:13,000 --> 00:05:15,000 +You'll say chatModel., + +142 +00:05:15,000 --> 00:05:17,000 +now when you say ., you will see an option off. + +143 +00:05:17,000 --> 00:05:19,000 +You'll see methods like call. + +144 +00:05:19,000 --> 00:05:21,000 +Now in this call you can pass a message + +145 +00:05:21,000 --> 00:05:23,000 +if you have a message type. + +146 +00:05:23,000 --> 00:05:27,000 +You can send a string, and you can send a prompt itself. + +147 +00:05:27,000 --> 00:05:29,000 +And how do you create this prompt? We'll see that later. + +148 +00:05:29,000 --> 00:05:31,000 +At this point I want to keep it very simple. + +149 +00:05:31,000 --> 00:05:32,000 +We'll go with string. + +150 +00:05:32,000 --> 00:05:34,000 +And here I'm going to pass the message. + +151 +00:05:34,000 --> 00:05:35,000 +So when you pass this message, + +152 +00:05:35,000 --> 00:05:38,000 +you are expecting some response here. + +153 +00:05:38,000 --> 00:05:40,000 +And this is a response + +154 +00:05:40,000 --> 00:05:42,000 +which I want to send instead of printing hello world, + +155 +00:05:42,000 --> 00:05:44,000 +and that I'll say response. + +156 +00:05:44,000 --> 00:05:46,000 +So, so simple, right? Look at the code. + +157 +00:05:46,000 --> 00:05:48,000 +We have hardly written the code of 10 lines, + +158 +00:05:48,000 --> 00:05:50,000 +if I'm not wrong. + +159 +00:05:50,000 --> 00:05:54,000 +And we should be, I mean I'm expecting it'll work. + +160 +00:05:54,000 --> 00:05:55,000 +Let's restart the server. + +161 +00:05:55,000 --> 00:05:57,000 +And you can see there's no error here. + +162 +00:05:57,000 --> 00:05:59,000 +Let me go back to my client + +163 +00:05:59,000 --> 00:06:02,000 +and I will say Send with the same thing. + +164 +00:06:02,000 --> 00:06:03,000 +You can see now it's taking some time. + +165 +00:06:03,000 --> 00:06:05,000 +It's because it is sending the request to the OpenAI server + +166 +00:06:06,000 --> 00:06:07,000 +and waiting for the answer. + +167 +00:06:07,000 --> 00:06:08,000 +It's taking a lot of time, + +168 +00:06:08,000 --> 00:06:10,000 +but ultimately you got the answer. + +169 +00:06:10,000 --> 00:06:12,000 +So you can see this is what it says. + +170 +00:06:12,000 --> 00:06:13,000 +What is Java? + +171 +00:06:13,000 --> 00:06:15,000 +So Java is this, this, this, this, this. + +172 +00:06:15,000 --> 00:06:16,000 +I hope it is correct. + +173 +00:06:16,000 --> 00:06:18,000 +We are not testing the OpenAI server here, + +174 +00:06:18,000 --> 00:06:19,000 +but we got the response. + +175 +00:06:19,000 --> 00:06:24,000 +We can also check, I can also say, tell me a joke, + +176 +00:06:25,000 --> 00:06:27,000 +or maybe I'll say tech joke. + +177 +00:06:27,000 --> 00:06:29,000 +And if I click on Send, it'll take some time, + +178 +00:06:29,000 --> 00:06:32,000 +it says, okay, why do programmers prefer dark mode? + +179 +00:06:32,000 --> 00:06:34,000 +Because light attract bugs. + +180 +00:06:34,000 --> 00:06:37,000 +Okay. Old joke, but works, okay? Whatever works. + +181 +00:06:37,000 --> 00:06:41,000 +Tell me fresh joke. + +182 +00:06:42,000 --> 00:06:45,000 +Maybe it just, this is not the right prompt. + +183 +00:06:46,000 --> 00:06:49,000 +Why did the computer breaks with the the internet? + +184 +00:06:49,000 --> 00:06:51,000 +Because it found someone with a better "bandwidth." + +185 +00:06:51,000 --> 00:06:54,000 +Oh, okay, this is nice. There's nice. Okay. + +186 +00:06:54,000 --> 00:06:55,000 +Anyway, so you've got the point, right? + +187 +00:06:55,000 --> 00:06:56,000 +So you send a message + +188 +00:06:56,000 --> 00:07:00,000 +and then this message goes to the LLM server, the OpenAI, + +189 +00:07:00,000 --> 00:07:02,000 +and then you get a response, okay? + +190 +00:07:02,000 --> 00:07:05,000 +So yeah, the OpenAI is working. Now, is that it? No. + +191 +00:07:05,000 --> 00:07:08,000 +See, with the help of LLMs, you can do a lot of stuff. + +192 +00:07:08,000 --> 00:07:10,000 +Not just this because you're sending a message, + +193 +00:07:10,000 --> 00:07:11,000 +you're getting a response in a string + +194 +00:07:11,000 --> 00:07:12,000 +and you're printing it here. + +195 +00:07:12,000 --> 00:07:14,000 +Simple stuff, right? + +196 +00:07:14,000 --> 00:07:15,000 +But sometime you need more control. + +197 +00:07:15,000 --> 00:07:19,000 +Sometime you just don't want to just send a message here. + +198 +00:07:19,000 --> 00:07:20,000 +Maybe you want to send system message, + +199 +00:07:20,000 --> 00:07:22,000 +maybe you want to create a proper prompt, + +200 +00:07:22,000 --> 00:07:24,000 +maybe you want to play with the metadata, + +201 +00:07:24,000 --> 00:07:26,000 +lot of stuff, right? + +202 +00:07:26,000 --> 00:07:29,000 +And in that case, we will not be just using chat model. + +203 +00:07:29,000 --> 00:07:31,000 +We have one more option, which is ChatClient, + +204 +00:07:31,000 --> 00:07:33,000 +which you're going to see in the upcoming videos. + +205 +00:07:33,000 --> 00:07:36,000 +And also not just with text. + +206 +00:07:36,000 --> 00:07:37,000 +Maybe you want to play with the images, + +207 +00:07:37,000 --> 00:07:39,000 +you want to play with the audio. + +208 +00:07:39,000 --> 00:07:40,000 +How will you do that? + +209 +00:07:40,000 --> 00:07:41,000 +Because LLM provides you all those stuff. + +210 +00:07:41,000 --> 00:07:43,000 +It's not just about text now. + +211 +00:07:43,000 --> 00:07:46,000 +We got multimodal models + +212 +00:07:46,000 --> 00:07:50,000 +where you can work with images, videos, audio, text. + +213 +00:07:50,000 --> 00:07:52,000 +So how do we explore that? + +214 +00:07:52,000 --> 00:07:54,000 +And to do that, we'll do multiple stuff. + +215 +00:07:54,000 --> 00:07:56,000 +And also I will, we'll also see how do you work + +216 +00:07:56,000 --> 00:07:58,000 +with Entropik and all the stuff. + +217 +00:07:58,000 --> 00:08:00,000 +So yeah, we just did the first part + +218 +00:08:00,000 --> 00:08:02,000 +where we're talking to LLM and yeah, + +219 +00:08:02,000 --> 00:08:04,000 +we can have a round of applause, things are working out. + +220 +00:08:04,000 --> 00:08:06,000 +See you in the next video. + diff --git a/23 - Spring AI/006 Source-Code.url b/23 - Spring AI/006 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..6cbf0e5d4d4aebc8ebdbbdb0a2640136cc689f48 --- /dev/null +++ b/23 - Spring AI/006 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_06 \ No newline at end of file diff --git a/23 - Spring AI/007 Source-Code.url b/23 - Spring AI/007 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..d40f63f8fd9ec69980fea0b3a0a1b207df17f4ec --- /dev/null +++ b/23 - Spring AI/007 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_07 \ No newline at end of file diff --git a/23 - Spring AI/007 Working with ChatClient_en.srt b/23 - Spring AI/007 Working with ChatClient_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..2e090d971b776367069e8359b473198912b09223 --- /dev/null +++ b/23 - Spring AI/007 Working with ChatClient_en.srt @@ -0,0 +1,700 @@ +1 +00:00:00,000 --> 00:00:02,000 +(soft music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now let's work with the chat client. + +3 +00:00:06,000 --> 00:00:08,000 +So in the previous video we have work with the ChatModel. + +4 +00:00:08,000 --> 00:00:11,000 +Basically using this you can track with the LLMs + +5 +00:00:11,000 --> 00:00:13,000 +but then it has limited features. + +6 +00:00:13,000 --> 00:00:15,000 +Of course you can talk to the LLM, + +7 +00:00:15,000 --> 00:00:18,000 +but what if you want a more fluent API + +8 +00:00:18,000 --> 00:00:20,000 +or a way to connect with the OpenAI + +9 +00:00:20,000 --> 00:00:23,000 +where you can send the prompt, you can get the response, + +10 +00:00:23,000 --> 00:00:25,000 +and this response can be one single response + +11 +00:00:25,000 --> 00:00:28,000 +or it can be stream of messages. + +12 +00:00:28,000 --> 00:00:31,000 +And to achieve that, we can use chat client. + +13 +00:00:31,000 --> 00:00:35,000 +And this chat client is actually built on ChatModel. Okay? + +14 +00:00:35,000 --> 00:00:37,000 +So even if you're using chat client + +15 +00:00:37,000 --> 00:00:39,000 +behind the scenes, you're using ChatModel. + +16 +00:00:39,000 --> 00:00:40,000 +Now how do you work with chat client? + +17 +00:00:40,000 --> 00:00:42,000 +And also it helps you, one more thing, + +18 +00:00:42,000 --> 00:00:45,000 +you don't have to basically bind to OpenAI + +19 +00:00:45,000 --> 00:00:48,000 +and let me show you how. + +20 +00:00:48,000 --> 00:00:49,000 +So instead of creating a ChatModel here, + +21 +00:00:49,000 --> 00:00:51,000 +I'm going to create a chat client. + +22 +00:00:51,000 --> 00:00:54,000 +So if you observe chat client is coming from + +23 +00:00:54,000 --> 00:00:56,000 +the Spring framework itself + +24 +00:00:56,000 --> 00:00:57,000 +and nowhere we are saying that we are working with + +25 +00:00:57,000 --> 00:00:58,000 +any particular model. + +26 +00:00:58,000 --> 00:00:59,000 +It's just a chat client + +27 +00:00:59,000 --> 00:01:02,000 +and we'll change this to chat client. + +28 +00:01:02,000 --> 00:01:05,000 +Now if you want to work with chat client, + +29 +00:01:05,000 --> 00:01:08,000 +how do you, okay, first of all, let me hide all the, + +30 +00:01:08,000 --> 00:01:09,000 +you can see we have chat client, + +31 +00:01:09,000 --> 00:01:11,000 +but how will you create the object of it? + +32 +00:01:11,000 --> 00:01:13,000 +If you are thinking the same way we had done for the model, + +33 +00:01:13,000 --> 00:01:15,000 +there should be object of chat client + +34 +00:01:15,000 --> 00:01:19,000 +where you will say chat client here and it should assign. + +35 +00:01:19,000 --> 00:01:22,000 +Now the thing is your Spring framework will inject + +36 +00:01:22,000 --> 00:01:23,000 +the chat models. + +37 +00:01:23,000 --> 00:01:27,000 +You have to generate the chat client by yourself. Okay? + +38 +00:01:27,000 --> 00:01:30,000 +So what I'm saying is will not change this will, + +39 +00:01:30,000 --> 00:01:33,000 +this will be OpenAI ChatModel itself. + +40 +00:01:33,000 --> 00:01:35,000 +But here instead of ChatModel, + +41 +00:01:35,000 --> 00:01:37,000 +I will create a chat client object. + +42 +00:01:37,000 --> 00:01:40,000 +And this need to be generated with the help of ChatModel. + +43 +00:01:40,000 --> 00:01:42,000 +So you can't simply ignore ChatModel, + +44 +00:01:42,000 --> 00:01:43,000 +you have to use ChatModel. + +45 +00:01:43,000 --> 00:01:45,000 +Maybe you can have a model, + +46 +00:01:45,000 --> 00:01:47,000 +you can have a separate class + +47 +00:01:47,000 --> 00:01:49,000 +where you can specify different chat models + +48 +00:01:49,000 --> 00:01:51,000 +and you can inject the object. + +49 +00:01:51,000 --> 00:01:53,000 +But here, just to keep it simple, + +50 +00:01:53,000 --> 00:01:54,000 +I got a ChatModel object here. + +51 +00:01:54,000 --> 00:01:57,000 +And using ChatModel, I can create the chat client object. + +52 +00:01:57,000 --> 00:02:00,000 +So you have a type called chat client. + +53 +00:02:00,000 --> 00:02:03,000 +It's an interface and it was introduced in 1.1.0. + +54 +00:02:03,000 --> 00:02:06,000 +because before this they have a different way of doing it. + +55 +00:02:06,000 --> 00:02:11,000 +So you can use ChatClient.create. + +56 +00:02:11,000 --> 00:02:15,000 +This is a method in which you pass a ChatModel. + +57 +00:02:15,000 --> 00:02:17,000 +So what I'm saying is ChatClient.create + +58 +00:02:17,000 --> 00:02:20,000 +and you can pass the ChatModel object. + +59 +00:02:20,000 --> 00:02:22,000 +What this will do is this will create the object + +60 +00:02:22,000 --> 00:02:24,000 +of ChatClient for you. + +61 +00:02:24,000 --> 00:02:26,000 +And once you have ChatClient with you, + +62 +00:02:26,000 --> 00:02:27,000 +you will just change this. + +63 +00:02:27,000 --> 00:02:29,000 +Instead of using ChatModel directly, + +64 +00:02:29,000 --> 00:02:32,000 +you will be using ChatClient. + +65 +00:02:32,000 --> 00:02:33,000 +Now there are different options here. + +66 +00:02:33,000 --> 00:02:34,000 +There's no call method. + +67 +00:02:34,000 --> 00:02:35,000 +We have a method called prompt + +68 +00:02:35,000 --> 00:02:37,000 +because it makes more sense, right? + +69 +00:02:37,000 --> 00:02:39,000 +Because when you talk to the LLM, + +70 +00:02:39,000 --> 00:02:40,000 +you talk to them with the help of prompts. + +71 +00:02:40,000 --> 00:02:42,000 +So it makes sense to send a prompt. + +72 +00:02:42,000 --> 00:02:43,000 +So I can say prompt here. + +73 +00:02:43,000 --> 00:02:45,000 +Now when you say prompt, you have three options. + +74 +00:02:45,000 --> 00:02:48,000 +You can either go for a blank prompt method + +75 +00:02:48,000 --> 00:02:49,000 +where you can create a prompt, + +76 +00:02:49,000 --> 00:02:51,000 +but then you can add the message later + +77 +00:02:51,000 --> 00:02:55,000 +or you can create a prompt with help of string + +78 +00:02:55,000 --> 00:02:56,000 +the way we are going to do now. + +79 +00:02:56,000 --> 00:02:58,000 +Or you can create a prompt object. + +80 +00:02:58,000 --> 00:03:01,000 +But till this point we have not worked with the prompt type. + +81 +00:03:01,000 --> 00:03:03,000 +So let's work with the string and let's use that. + +82 +00:03:03,000 --> 00:03:05,000 +So here I'm going to pass the message. + +83 +00:03:05,000 --> 00:03:07,000 +Now once you have the message, + +84 +00:03:07,000 --> 00:03:09,000 +if you're thinking this will send you response. + +85 +00:03:09,000 --> 00:03:12,000 +No, prompt will simply create the object + +86 +00:03:12,000 --> 00:03:14,000 +of ChatClientRequestSpec. + +87 +00:03:14,000 --> 00:03:17,000 +It will not give you the text. Okay? + +88 +00:03:17,000 --> 00:03:19,000 +And how do you get the text? + +89 +00:03:19,000 --> 00:03:22,000 +In this scenario you're not calling to the LLM. + +90 +00:03:22,000 --> 00:03:24,000 +So if you want to call LLM, you have to say call. + +91 +00:03:24,000 --> 00:03:26,000 +This is where it'll call LLM. + +92 +00:03:26,000 --> 00:03:29,000 +But this call will again return the object off call. + +93 +00:03:29,000 --> 00:03:32,000 +I mean this time it is CallResponseSpec. + +94 +00:03:32,000 --> 00:03:34,000 +So basically you will get some response, + +95 +00:03:34,000 --> 00:03:36,000 +not the string type. + +96 +00:03:36,000 --> 00:03:37,000 +And if you want the string type, + +97 +00:03:37,000 --> 00:03:39,000 +you have to say chat response. + +98 +00:03:39,000 --> 00:03:43,000 +In fact, you can say content, it'll give you the string type + +99 +00:03:43,000 --> 00:03:44,000 +and yeah, so this is how you do it. + +100 +00:03:44,000 --> 00:03:48,000 +So prompt.call.content doesn't look good maybe. + +101 +00:03:48,000 --> 00:03:52,000 +So what you will do is you will use this style + +102 +00:03:52,000 --> 00:03:54,000 +so that it is much easier to read it. + +103 +00:03:54,000 --> 00:03:56,000 +So what we are saying is we got a ChatClient + +104 +00:03:56,000 --> 00:03:58,000 +we are creating a prompt here. + +105 +00:03:58,000 --> 00:04:00,000 +We are calling the prompt. + +106 +00:04:00,000 --> 00:04:03,000 +And then are fetching the content out of it. + +107 +00:04:03,000 --> 00:04:05,000 +Now when you face the content that goes into response + +108 +00:04:05,000 --> 00:04:07,000 +and you are returning a response here. + +109 +00:04:07,000 --> 00:04:09,000 +Now you'll be saying, okay, that was better, right? + +110 +00:04:09,000 --> 00:04:11,000 +ChatModel, you simply say call. + +111 +00:04:11,000 --> 00:04:12,000 +But see you get a more control here. + +112 +00:04:12,000 --> 00:04:15,000 +You can change the prompt as you want. + +113 +00:04:15,000 --> 00:04:17,000 +Instead of just fetching a content + +114 +00:04:17,000 --> 00:04:18,000 +you can fetch other stuff as well, + +115 +00:04:18,000 --> 00:04:19,000 +like metadata and other stuff. + +116 +00:04:19,000 --> 00:04:21,000 +I will show you that in some time. + +117 +00:04:21,000 --> 00:04:23,000 +But you've got a content here. + +118 +00:04:23,000 --> 00:04:24,000 +And here we are returning a response. + +119 +00:04:24,000 --> 00:04:26,000 +I hope this will work. + +120 +00:04:26,000 --> 00:04:29,000 +And this time I'll say with Chat Client + +121 +00:04:29,000 --> 00:04:30,000 +and just to prove that it is coming + +122 +00:04:30,000 --> 00:04:31,000 +with the help of ChatClient + +123 +00:04:31,000 --> 00:04:34,000 +And now after doing that, let me restart the server. + +124 +00:04:34,000 --> 00:04:35,000 +I'll go back to Insomnia. + +125 +00:04:35,000 --> 00:04:38,000 +I will just click on send with the same message + +126 +00:04:38,000 --> 00:04:39,000 +and you can start executing. + +127 +00:04:39,000 --> 00:04:40,000 +And we got it. + +128 +00:04:40,000 --> 00:04:42,000 +So it says with ChatClient + +129 +00:04:42,000 --> 00:04:43,000 +why did the computer go to the therapy? + +130 +00:04:43,000 --> 00:04:46,000 +Because it had too many bytes from its past. + +131 +00:04:46,000 --> 00:04:49,000 +Oh, okay, this is also good. + +132 +00:04:49,000 --> 00:04:50,000 +Anyway, so you got this response right? + +133 +00:04:50,000 --> 00:04:51,000 +And things are working out. + +134 +00:04:51,000 --> 00:04:53,000 +So we don't have to actually do this, + +135 +00:04:53,000 --> 00:04:55,000 +just wanted to show you that this works. + +136 +00:04:55,000 --> 00:04:58,000 +And that's how basically you use ChatClient. + +137 +00:04:58,000 --> 00:04:59,000 +As I mentioned before, instead of using content, + +138 +00:04:59,000 --> 00:05:01,000 +you can use other methods as well. + +139 +00:05:01,000 --> 00:05:03,000 +We got chat response, which will help you + +140 +00:05:03,000 --> 00:05:05,000 +to get better data. + +141 +00:05:05,000 --> 00:05:09,000 +And what if in future you want to get a structured output? + +142 +00:05:09,000 --> 00:05:12,000 +In that case also you can use ChatClient. + +143 +00:05:12,000 --> 00:05:14,000 +So at this point we are just saying content. + +144 +00:05:14,000 --> 00:05:16,000 +Now before ending this video, I want to make one change. + +145 +00:05:16,000 --> 00:05:17,000 +If you observe normally + +146 +00:05:17,000 --> 00:05:20,000 +when you work with Spring framework, + +147 +00:05:20,000 --> 00:05:21,000 +normally when you work with the APIs + +148 +00:05:21,000 --> 00:05:23,000 +you normally send the status code as well. + +149 +00:05:23,000 --> 00:05:27,000 +It doesn't matter what happens here, it'll always send 200 + +150 +00:05:27,000 --> 00:05:28,000 +or whatever the error you get. + +151 +00:05:28,000 --> 00:05:29,000 +I want to change it. + +152 +00:05:29,000 --> 00:05:31,000 +So instead of sending response, + +153 +00:05:31,000 --> 00:05:36,000 +I want to send the response entity of type string. + +154 +00:05:36,000 --> 00:05:37,000 +Why I've not done that before + +155 +00:05:37,000 --> 00:05:39,000 +is because I wanted to show you + +156 +00:05:39,000 --> 00:05:41,000 +how simple it can be to work with. + +157 +00:05:41,000 --> 00:05:43,000 +So ResponseEntity.ok. + +158 +00:05:45,000 --> 00:05:47,000 +Okay, ResponseEntity of not type AI. + +159 +00:05:49,000 --> 00:05:51,000 +It should be from Spring. + +160 +00:05:51,000 --> 00:05:52,000 +.ok. + +161 +00:05:52,000 --> 00:05:54,000 +And here we are going to pass the response. + +162 +00:05:54,000 --> 00:05:56,000 +That's it, nothing fancy. + +163 +00:05:56,000 --> 00:05:59,000 +Did some small change and now let's test it. + +164 +00:05:59,000 --> 00:06:00,000 +Still working. + +165 +00:06:00,000 --> 00:06:02,000 +Why did the company, okay, same question, + +166 +00:06:02,000 --> 00:06:04,000 +but because it has too many bites + +167 +00:06:04,000 --> 00:06:05,000 +of emotional baggage, okay. + +168 +00:06:05,000 --> 00:06:07,000 +Works, works. + +169 +00:06:07,000 --> 00:06:07,000 +I should do that. + +170 +00:06:07,000 --> 00:06:10,000 +I should use these jokes in this session. + +171 +00:06:10,000 --> 00:06:11,000 +Anyway, but you've got the point, right? + +172 +00:06:11,000 --> 00:06:14,000 +So these things are working out. + +173 +00:06:14,000 --> 00:06:15,000 +So that's it from this video. + +174 +00:06:15,000 --> 00:06:15,000 +In the next video, + +175 +00:06:15,000 --> 00:06:18,000 +we'll try to explore this and see more examples. + diff --git a/23 - Spring AI/008 ChatResponse and MetaData_en.srt b/23 - Spring AI/008 ChatResponse and MetaData_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..d5becf345df31ae2f2ee4aa9e2c0bbd6c57ce496 --- /dev/null +++ b/23 - Spring AI/008 ChatResponse and MetaData_en.srt @@ -0,0 +1,572 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now let's explore + +3 +00:00:05,000 --> 00:00:07,000 +what else you can do with the chat client. + +4 +00:00:07,000 --> 00:00:08,000 +Now basically, in this video, + +5 +00:00:08,000 --> 00:00:10,000 +we'll focus on the chat response. + +6 +00:00:10,000 --> 00:00:12,000 +Now, if you observe here, + +7 +00:00:12,000 --> 00:00:15,000 +basically what we have done is we got a chatClient + +8 +00:00:15,000 --> 00:00:17,000 +and we are getting a prompt first + +9 +00:00:17,000 --> 00:00:18,000 +and then we are calling it + +10 +00:00:18,000 --> 00:00:21,000 +so that it will send a request to the OpenAI server. + +11 +00:00:21,000 --> 00:00:24,000 +And then whatever content you received, + +12 +00:00:24,000 --> 00:00:27,000 +this particular method will return that in a string format. + +13 +00:00:27,000 --> 00:00:29,000 +And that's what we are returning, right? + +14 +00:00:29,000 --> 00:00:33,000 +Now, if you observe, if you remove the content here, + +15 +00:00:33,000 --> 00:00:34,000 +let's see what other methods we have. + +16 +00:00:34,000 --> 00:00:38,000 +So when I say dot, there's a method called chatResponse. + +17 +00:00:38,000 --> 00:00:40,000 +Now, if you go to chatResponse, + +18 +00:00:40,000 --> 00:00:43,000 +chatResponse return the object of chatResponse, + +19 +00:00:43,000 --> 00:00:45,000 +and that makes sense. + +20 +00:00:45,000 --> 00:00:46,000 +Let's go here. + +21 +00:00:46,000 --> 00:00:50,000 +And basically, this is a chat completion response + +22 +00:00:50,000 --> 00:00:51,000 +returned by AI provider. + +23 +00:00:51,000 --> 00:00:52,000 +Okay, nothing much, + +24 +00:00:52,000 --> 00:00:54,000 +but it has multiple methods to work with. + +25 +00:00:54,000 --> 00:00:58,000 +And one of the methods which I'm interested in is this, + +26 +00:00:58,000 --> 00:01:00,000 +which is getMetadata. + +27 +00:01:00,000 --> 00:01:03,000 +So basically, so content will give you data, right? + +28 +00:01:03,000 --> 00:01:06,000 +But what about if you want to know something extra? + +29 +00:01:06,000 --> 00:01:08,000 +And that's where you will get the metadata. + +30 +00:01:08,000 --> 00:01:10,000 +So let's go back here and let's say chatResponse. + +31 +00:01:10,000 --> 00:01:14,000 +Now, chatResponse will not give you string, right? + +32 +00:01:14,000 --> 00:01:18,000 +It will give you the object type of chatResponse itself. + +33 +00:01:18,000 --> 00:01:20,000 +And I'll name this instead of response, + +34 +00:01:20,000 --> 00:01:22,000 +I will say this is chatResponse + +35 +00:01:22,000 --> 00:01:23,000 +because that's what you're getting. + +36 +00:01:23,000 --> 00:01:26,000 +And, of course, I'm not going to print response here. + +37 +00:01:26,000 --> 00:01:27,000 +What else we can send? + +38 +00:01:27,000 --> 00:01:29,000 +In fact, let's say I don't want to go for metadata, + +39 +00:01:29,000 --> 00:01:31,000 +I just want to print response. + +40 +00:01:31,000 --> 00:01:33,000 +So what you can do is you can create another string object, + +41 +00:01:33,000 --> 00:01:37,000 +which is response, and from this chatResponse, + +42 +00:01:37,000 --> 00:01:39,000 +because in this you have everything, + +43 +00:01:39,000 --> 00:01:41,000 +you got your response in a text format, + +44 +00:01:41,000 --> 00:01:43,000 +also you got metadata. + +45 +00:01:43,000 --> 00:01:45,000 +But if I want to fetch the string, + +46 +00:01:45,000 --> 00:01:48,000 +in that case, you have to say getResult dot + +47 +00:01:48,000 --> 00:01:51,000 +because getResult will not give you a string. + +48 +00:01:51,000 --> 00:01:52,000 +getResult will give you a generation, + +49 +00:01:52,000 --> 00:01:56,000 +which we'll talk about that later in case. + +50 +00:01:56,000 --> 00:01:59,000 +But at this point, I want to say getResult. + +51 +00:01:59,000 --> 00:02:02,000 +And from that, I want to get the output. + +52 +00:02:02,000 --> 00:02:04,000 +And from that, I want to get the text. + +53 +00:02:04,000 --> 00:02:07,000 +I know multiple methods just to get the content. + +54 +00:02:07,000 --> 00:02:08,000 +The content method was working, + +55 +00:02:08,000 --> 00:02:11,000 +but in case if you want to get something extra + +56 +00:02:11,000 --> 00:02:13,000 +because in between, from the getResult, + +57 +00:02:13,000 --> 00:02:15,000 +you can get something extra as well. + +58 +00:02:15,000 --> 00:02:18,000 +From here, you can get metadata and other stuff. + +59 +00:02:18,000 --> 00:02:22,000 +In fact, we also have getResults, + +60 +00:02:22,000 --> 00:02:23,000 +in case if you observed that. + +61 +00:02:23,000 --> 00:02:25,000 +Now, it will give you different ways + +62 +00:02:25,000 --> 00:02:27,000 +you can stream on different data. + +63 +00:02:27,000 --> 00:02:29,000 +You can also check if it contains. + +64 +00:02:29,000 --> 00:02:30,000 +So we have a lot of options. + +65 +00:02:30,000 --> 00:02:32,000 +You can say if you want first output, + +66 +00:02:32,000 --> 00:02:35,000 +you can say getFirst, getLast. + +67 +00:02:35,000 --> 00:02:36,000 +So you get a lot of options. + +68 +00:02:36,000 --> 00:02:38,000 +But at this point, I just want one thing. + +69 +00:02:38,000 --> 00:02:41,000 +So I'll say getResult.getOutput.getText. + +70 +00:02:43,000 --> 00:02:45,000 +And this should give me the response. + +71 +00:02:45,000 --> 00:02:47,000 +And just to make it look good, + +72 +00:02:47,000 --> 00:02:49,000 +I will just Enter that instead of one line. + +73 +00:02:49,000 --> 00:02:50,000 +Easier to read. + +74 +00:02:50,000 --> 00:02:52,000 +And here, I will send the response. + +75 +00:02:52,000 --> 00:02:53,000 +The response will work. + +76 +00:02:53,000 --> 00:02:57,000 +But apart from response, I also want to print metadata. + +77 +00:02:57,000 --> 00:02:59,000 +And I'll just put that here. + +78 +00:02:59,000 --> 00:03:00,000 +And if you want to get, + +79 +00:03:00,000 --> 00:03:02,000 +in fact, you can also check with the chatResponse, + +80 +00:03:02,000 --> 00:03:04,000 +and that's the situation we got. + +81 +00:03:04,000 --> 00:03:07,000 +So before you send this data, you can even check, + +82 +00:03:07,000 --> 00:03:08,000 +do you have anything in chatResponse? + +83 +00:03:08,000 --> 00:03:11,000 +If it is null, then you can respond to the client by saying, + +84 +00:03:11,000 --> 00:03:13,000 +I have not received any input + +85 +00:03:13,000 --> 00:03:15,000 +or output from the AI model. + +86 +00:03:15,000 --> 00:03:16,000 +Okay, but here I don't wanna do that. + +87 +00:03:16,000 --> 00:03:19,000 +I will just try to fetch the metadata. + +88 +00:03:19,000 --> 00:03:22,000 +And to do that, I will say chatResponse object + +89 +00:03:22,000 --> 00:03:26,000 +dot getMetadata dot + +90 +00:03:26,000 --> 00:03:28,000 +and this will return a lot of stuff. + +91 +00:03:28,000 --> 00:03:32,000 +So getMetadata will return chatResponseMetadata. + +92 +00:03:32,000 --> 00:03:33,000 +So what else you want to print here? + +93 +00:03:33,000 --> 00:03:37,000 +So you can get the prompt metadata. + +94 +00:03:37,000 --> 00:03:38,000 +You can get the model which you're working with. + +95 +00:03:38,000 --> 00:03:41,000 +You can get the rate limit. You can check the usage. + +96 +00:03:41,000 --> 00:03:43,000 +So at this point, let's say I want to get the model, + +97 +00:03:43,000 --> 00:03:44,000 +which is running, + +98 +00:03:44,000 --> 00:03:46,000 +and I don't want to return to the user, + +99 +00:03:46,000 --> 00:03:48,000 +I want to print on the console. + +100 +00:03:48,000 --> 00:03:50,000 +So I can do that here. + +101 +00:03:50,000 --> 00:03:52,000 +And this is what we are doing. + +102 +00:03:52,000 --> 00:03:56,000 +Now, getModel returns a string, so looks good. + +103 +00:03:56,000 --> 00:03:58,000 +We should be able to print that on the console. + +104 +00:03:58,000 --> 00:04:00,000 +And there's a warning here. + +105 +00:04:00,000 --> 00:04:01,000 +It says, "Method invocation getMetadata' + +106 +00:04:01,000 --> 00:04:03,000 +may produce NullPointerException." + +107 +00:04:03,000 --> 00:04:05,000 +Okay, that's fine. + +108 +00:04:05,000 --> 00:04:07,000 +At this point, I know it will not return + +109 +00:04:07,000 --> 00:04:09,000 +NullPointerException if everything is working. + +110 +00:04:09,000 --> 00:04:12,000 +You should check that, okay? That's the ideal way. + +111 +00:04:12,000 --> 00:04:13,000 +But I just want to keep the code clean + +112 +00:04:13,000 --> 00:04:14,000 +so that you can understand. + +113 +00:04:14,000 --> 00:04:16,000 +You can go by step by step. + +114 +00:04:16,000 --> 00:04:17,000 +You know, when you're in learning phase, + +115 +00:04:17,000 --> 00:04:19,000 +you should not be worrying about all the stuff. + +116 +00:04:19,000 --> 00:04:21,000 +When you start development, + +117 +00:04:21,000 --> 00:04:23,000 +of course, those things need to be taken care of. + +118 +00:04:23,000 --> 00:04:25,000 +Okay, so let's restart the application. + +119 +00:04:25,000 --> 00:04:28,000 +I hope this will work and there is no error. + +120 +00:04:28,000 --> 00:04:29,000 +Let me go to Insomnia + +121 +00:04:29,000 --> 00:04:31,000 +and I will just use the same prompt, + +122 +00:04:31,000 --> 00:04:33,000 +click on Send and it says, + +123 +00:04:33,000 --> 00:04:34,000 +why did the computer go to therapy? + +124 +00:04:34,000 --> 00:04:36,000 +Because it has too many bytes. + +125 +00:04:36,000 --> 00:04:37,000 +Okay, so we got the same joke, + +126 +00:04:37,000 --> 00:04:38,000 +but that's not the main thing. + +127 +00:04:38,000 --> 00:04:40,000 +The main thing is if you see the console, + +128 +00:04:40,000 --> 00:04:43,000 +it is printing which model it is running. + +129 +00:04:43,000 --> 00:04:45,000 +So it's only GPT‑4o mini. + +130 +00:04:45,000 --> 00:04:48,000 +Okay, so that's good, things are working out + +131 +00:04:48,000 --> 00:04:49,000 +and that's why you got the model. + +132 +00:04:49,000 --> 00:04:51,000 +So using metadata, you can get multiple stuff + +133 +00:04:51,000 --> 00:04:52,000 +and the response which you're getting + +134 +00:04:52,000 --> 00:04:53,000 +is because of this thing. + +135 +00:04:53,000 --> 00:04:55,000 +So what we have done is we got the result, + +136 +00:04:55,000 --> 00:04:57,000 +we got the output and we got text out of it. + +137 +00:04:57,000 --> 00:04:59,000 +If you are getting multiple responses, + +138 +00:04:59,000 --> 00:05:01,000 +let's say if you say tell me 10 jokes, + +139 +00:05:01,000 --> 00:05:02,000 +there's a different way of handling it + +140 +00:05:02,000 --> 00:05:03,000 +and maybe we'll see that later, + +141 +00:05:03,000 --> 00:05:05,000 +but at this point, things are working out. + +142 +00:05:05,000 --> 00:05:08,000 +Okay, we are happy. And that's how you use chatResponse. + +143 +00:05:08,000 --> 00:05:09,000 +That's how you get metadata. + diff --git a/23 - Spring AI/008 Source-Code.url b/23 - Spring AI/008 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..f28d766908bc29bb8ed2916ba6fa83d006301160 --- /dev/null +++ b/23 - Spring AI/008 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_08 \ No newline at end of file diff --git a/23 - Spring AI/009 ChatClient Builder_en.srt b/23 - Spring AI/009 ChatClient Builder_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..2e4d2261e76ed7f77ae674aee2229ce070d19359 --- /dev/null +++ b/23 - Spring AI/009 ChatClient Builder_en.srt @@ -0,0 +1,444 @@ +1 +00:00:00,000 --> 00:00:02,000 +(soft music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now, let's go back to our ChatClient. + +3 +00:00:06,000 --> 00:00:07,000 +It's working, right? + +4 +00:00:07,000 --> 00:00:09,000 +And normally, we have this tendency, + +5 +00:00:09,000 --> 00:00:10,000 +if something is working, don't change it. + +6 +00:00:10,000 --> 00:00:13,000 +But if you go to the documentation of ChatClient, + +7 +00:00:13,000 --> 00:00:16,000 +here on the Spring AI page, + +8 +00:00:16,000 --> 00:00:18,000 +basically it says creating a ChatClient, + +9 +00:00:18,000 --> 00:00:21,000 +you can use something called a Chat Builder + +10 +00:00:21,000 --> 00:00:22,000 +or ChatClient.Builder. + +11 +00:00:22,000 --> 00:00:24,000 +We have not used it, okay? + +12 +00:00:24,000 --> 00:00:25,000 +Because there are two options. + +13 +00:00:25,000 --> 00:00:28,000 +One, you can use the ChatClient.Builder, + +14 +00:00:28,000 --> 00:00:30,000 +which is Spring Boot autoconfigure. + +15 +00:00:30,000 --> 00:00:32,000 +Otherwise you can also do it programmatically. + +16 +00:00:32,000 --> 00:00:35,000 +So what we have done is we have done it programmatically, + +17 +00:00:35,000 --> 00:00:36,000 +but then, which is better? + +18 +00:00:36,000 --> 00:00:38,000 +Now, if you observe the code, + +19 +00:00:38,000 --> 00:00:40,000 +okay, so what we have done is different. + +20 +00:00:40,000 --> 00:00:41,000 +If you observe this code, + +21 +00:00:41,000 --> 00:00:42,000 +so if you have a controller, + +22 +00:00:42,000 --> 00:00:44,000 +and instead of creating a chatModel object + +23 +00:00:44,000 --> 00:00:46,000 +and then using that to create a ChatClient, + +24 +00:00:46,000 --> 00:00:48,000 +which we have done in our code. + +25 +00:00:48,000 --> 00:00:50,000 +So if I show you the code, this is what we have done, right? + +26 +00:00:50,000 --> 00:00:53,000 +So we have used something called a ChatClient.create + +27 +00:00:53,000 --> 00:00:55,000 +with the help of chatModel. + +28 +00:00:55,000 --> 00:00:58,000 +Now they are suggesting, no, that's not the only way. + +29 +00:00:58,000 --> 00:01:01,000 +There's also a ChatClient.Builder, + +30 +00:01:01,000 --> 00:01:02,000 +and you can use this. + +31 +00:01:02,000 --> 00:01:04,000 +Now see, this is autoconfigured, right? + +32 +00:01:04,000 --> 00:01:05,000 +So in your application, + +33 +00:01:05,000 --> 00:01:07,000 +if you have only one model feature you're using, + +34 +00:01:07,000 --> 00:01:10,000 +so let's say if you're using open AI or Entropic or Llama, + +35 +00:01:10,000 --> 00:01:11,000 +whichever which you're using, + +36 +00:01:11,000 --> 00:01:14,000 +basically you can use ChatClient.Builder + +37 +00:01:14,000 --> 00:01:15,000 +so it will automatically detect, okay, + +38 +00:01:15,000 --> 00:01:17,000 +which model you have in the library. + +39 +00:01:17,000 --> 00:01:18,000 +Okay, so you got OpenAI. + +40 +00:01:18,000 --> 00:01:21,000 +So it will configure it and it will inject that. + +41 +00:01:21,000 --> 00:01:24,000 +So whenever you try to use a ChatClient directly, + +42 +00:01:24,000 --> 00:01:27,000 +it will simply call OpenAI or whatever model you're using. + +43 +00:01:27,000 --> 00:01:29,000 +But when I started this application I thought, + +44 +00:01:29,000 --> 00:01:31,000 +okay, let me use multiple models. + +45 +00:01:31,000 --> 00:01:33,000 +And when you use multiple models, that's where you have + +46 +00:01:33,000 --> 00:01:36,000 +to use ChatClient programmatically. + +47 +00:01:36,000 --> 00:01:37,000 +In that case, + +48 +00:01:37,000 --> 00:01:39,000 +you have to also disable the ChatClient.Builder. + +49 +00:01:39,000 --> 00:01:42,000 +Not compulsory, but it's an ideal way. + +50 +00:01:42,000 --> 00:01:44,000 +Now we have used this approach, okay? + +51 +00:01:44,000 --> 00:01:46,000 +So we have to create a ChatClient. + +52 +00:01:46,000 --> 00:01:47,000 +I mean, I'm getting ChatClient + +53 +00:01:47,000 --> 00:01:50,000 +with the help of ChatClient.create + +54 +00:01:50,000 --> 00:01:52,000 +and we are passing the model. + +55 +00:01:52,000 --> 00:01:53,000 +But let's try to use the builder + +56 +00:01:53,000 --> 00:01:54,000 +and let's see how that works. + +57 +00:01:54,000 --> 00:01:57,000 +Now, builder works only when you have only one model, + +58 +00:01:57,000 --> 00:01:59,000 +so that it will not get confused. + +59 +00:01:59,000 --> 00:02:02,000 +So here in show of this, I will just commend this part now + +60 +00:02:02,000 --> 00:02:03,000 +and still we'll make it work. + +61 +00:02:03,000 --> 00:02:05,000 +So I'll just, when you commend this, + +62 +00:02:05,000 --> 00:02:07,000 +of course, when you try to restart this, + +63 +00:02:07,000 --> 00:02:11,000 +and if you know Spring, it will give you error. + +64 +00:02:11,000 --> 00:02:12,000 +I hope. + +65 +00:02:12,000 --> 00:02:15,000 +I'm just hoping when you use insomnia, + +66 +00:02:15,000 --> 00:02:16,000 +when you click send + +67 +00:02:16,000 --> 00:02:17,000 +and you can see we got an error, + +68 +00:02:17,000 --> 00:02:19,000 +it is internal server error. + +69 +00:02:19,000 --> 00:02:22,000 +If you go back here, + +70 +00:02:22,000 --> 00:02:25,000 +it should say that the ChatClient is not instantiated. + +71 +00:02:25,000 --> 00:02:27,000 +Yeah, so we got null pointer exception. + +72 +00:02:27,000 --> 00:02:28,000 +Expected. + +73 +00:02:28,000 --> 00:02:29,000 +But I want to inject that. + +74 +00:02:29,000 --> 00:02:32,000 +So this time also I will use the constructor, + +75 +00:02:32,000 --> 00:02:34,000 +but in the ChatClient here, + +76 +00:02:34,000 --> 00:02:35,000 +I will not be using ChatClient directly + +77 +00:02:35,000 --> 00:02:37,000 +because it will not inject that. + +78 +00:02:37,000 --> 00:02:41,000 +So as a ChatClient.Builder, + +79 +00:02:41,000 --> 00:02:43,000 +and I will name this as builder itself. + +80 +00:02:43,000 --> 00:02:45,000 +And then how do you create the object? + +81 +00:02:45,000 --> 00:02:46,000 +And we have seen the documentation. + +82 +00:02:46,000 --> 00:02:50,000 +You can simply say ChatClient, builder.build. + +83 +00:02:50,000 --> 00:02:52,000 +So this is one way, but this makes sense + +84 +00:02:52,000 --> 00:02:53,000 +when you have multiple models to work with + +85 +00:02:53,000 --> 00:02:54,000 +because there's no confusion. + +86 +00:02:54,000 --> 00:02:56,000 +We are specifying OpenAI. + +87 +00:02:56,000 --> 00:02:57,000 +But in this case, it will detect automatically. + +88 +00:02:57,000 --> 00:03:00,000 +So you can simply use ChatClient.Builder builder, + +89 +00:03:01,000 --> 00:03:02,000 +and this should work. + +90 +00:03:02,000 --> 00:03:06,000 +So let me just start with this code and go back to insomnia + +91 +00:03:06,000 --> 00:03:07,000 +and let's try + +92 +00:03:08,000 --> 00:03:09,000 +and we got the answer. + +93 +00:03:09,000 --> 00:03:10,000 +It says, + +94 +00:03:10,000 --> 00:03:12,000 +okay, so now we have seen a lot of jokes. + +95 +00:03:12,000 --> 00:03:13,000 +I don't know, + +96 +00:03:13,000 --> 00:03:14,000 +even if I say fresh jokes, + +97 +00:03:14,000 --> 00:03:15,000 +it is giving the same joke every time + +98 +00:03:15,000 --> 00:03:17,000 +or the old jokes. + +99 +00:03:18,000 --> 00:03:19,000 +Radio programmers... + +100 +00:03:19,000 --> 00:03:21,000 +Okay, same old jokes. + +101 +00:03:21,000 --> 00:03:23,000 +Maybe AI need to be trained properly here. + +102 +00:03:23,000 --> 00:03:25,000 +So if you go back, there's no problem. + +103 +00:03:25,000 --> 00:03:27,000 +It is printing this and you got the output as well. + +104 +00:03:27,000 --> 00:03:30,000 +So this how you basically use the ChatClient.Builder. + +105 +00:03:30,000 --> 00:03:33,000 +Now, if you only want to use one model, use this. + +106 +00:03:33,000 --> 00:03:35,000 +Maybe next time we'll also use, + +107 +00:03:35,000 --> 00:03:37,000 +we'll only use this, not this one, okay? + +108 +00:03:37,000 --> 00:03:39,000 +But now you know the difference, right? + +109 +00:03:39,000 --> 00:03:39,000 +Which one to use. + +110 +00:03:39,000 --> 00:03:43,000 +Okay, so that's about the ChatClient Builder. + +111 +00:03:43,000 --> 00:03:46,000 +We'll focus on the other topics in the upcoming videos. + diff --git a/23 - Spring AI/009 Source-Code.url b/23 - Spring AI/009 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..1c97413e767f490b625272432754239ef740c0c5 --- /dev/null +++ b/23 - Spring AI/009 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_09 \ No newline at end of file diff --git a/23 - Spring AI/010 Source-Code.url b/23 - Spring AI/010 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..4de5de9c3c770cb425fe2fafe0b9a5c6c595bccd --- /dev/null +++ b/23 - Spring AI/010 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_10 \ No newline at end of file diff --git a/23 - Spring AI/010 Spring AI Memory Advisor_en.srt b/23 - Spring AI/010 Spring AI Memory Advisor_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..0e5f587b0b29edb33d281be14bf2975d35d99f2d --- /dev/null +++ b/23 - Spring AI/010 Spring AI Memory Advisor_en.srt @@ -0,0 +1,628 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:04,000 +-: So things are working fine. + +3 +00:00:04,000 --> 00:00:07,000 +So in fact, I will just run this project once again, + +4 +00:00:07,000 --> 00:00:09,000 +and let's go back to Insomnia, + +5 +00:00:09,000 --> 00:00:11,000 +and, here, let's ask for... + +6 +00:00:11,000 --> 00:00:13,000 +Let's go for the jokes again. + +7 +00:00:13,000 --> 00:00:15,000 +So I will say, no, Telusko every time, + +8 +00:00:15,000 --> 00:00:20,000 +"Tell me a joke," and if I click on this, + +9 +00:00:20,000 --> 00:00:21,000 +it will send a request. + +10 +00:00:21,000 --> 00:00:22,000 +It will wait for the response, + +11 +00:00:22,000 --> 00:00:24,000 +and you've got a joke here, right? + +12 +00:00:24,000 --> 00:00:26,000 +And maybe I like the joke. + +13 +00:00:26,000 --> 00:00:29,000 +In this case, I want to know more jokes. + +14 +00:00:29,000 --> 00:00:30,000 +So when I send more here, + +15 +00:00:30,000 --> 00:00:33,000 +of course, that's what you do on ChatGPT + +16 +00:00:33,000 --> 00:00:35,000 +or any other AI tool. + +17 +00:00:35,000 --> 00:00:37,000 +You say more, and when you click on this, + +18 +00:00:37,000 --> 00:00:38,000 +now, it's confused. + +19 +00:00:38,000 --> 00:00:41,000 +It says, "It seems like your request is a bit vague. + +20 +00:00:41,000 --> 00:00:43,000 +Could you please provide more context + +21 +00:00:43,000 --> 00:00:44,000 +or specify what you..." + +22 +00:00:44,000 --> 00:00:46,000 +Okay, so basically, it has no idea + +23 +00:00:46,000 --> 00:00:48,000 +when I say more, what I'm talking about. + +24 +00:00:48,000 --> 00:00:51,000 +Now, this is where your LLM is memoryless. + +25 +00:00:51,000 --> 00:00:54,000 +So basically, if you are thinking, by default, + +26 +00:00:54,000 --> 00:00:59,000 +your LLM knows you or it remembers the old conversation. + +27 +00:00:59,000 --> 00:01:00,000 +That's not the case. + +28 +00:01:00,000 --> 00:01:02,000 +Now, maybe you are thinking, + +29 +00:01:02,000 --> 00:01:05,000 +"Okay, have used OpenAI before or maybe Gemini?" + +30 +00:01:05,000 --> 00:01:06,000 +And when you talk to them, + +31 +00:01:06,000 --> 00:01:08,000 +basically, when you get the response, + +32 +00:01:08,000 --> 00:01:12,000 +when you say more, it gives you more details. + +33 +00:01:12,000 --> 00:01:13,000 +It's not happening here. + +34 +00:01:13,000 --> 00:01:17,000 +So maybe those tools provide the memory, and that's right. + +35 +00:01:17,000 --> 00:01:21,000 +So it's not the LLM feature to maintain your conversation. + +36 +00:01:21,000 --> 00:01:23,000 +It's, basically, this wrapper around those LLMs. + +37 +00:01:23,000 --> 00:01:25,000 +For example, if we talk about ChatGPT, + +38 +00:01:25,000 --> 00:01:29,000 +it has its own feature where it remembers your old chat. + +39 +00:01:29,000 --> 00:01:31,000 +In fact, to demonstrate this, I have a UI ready, + +40 +00:01:31,000 --> 00:01:34,000 +which is a clone of ChatGPT. + +41 +00:01:34,000 --> 00:01:35,000 +So if I run this, and you should be able + +42 +00:01:35,000 --> 00:01:38,000 +to find this in resources, + +43 +00:01:38,000 --> 00:01:40,000 +and if I go back to the page where it has, + +44 +00:01:40,000 --> 00:01:44,000 +so this is Telusko GPT, copy of ChatGPT, + +45 +00:01:44,000 --> 00:01:46,000 +but, anyway, I will say, "Tell me a joke," + +46 +00:01:46,000 --> 00:01:48,000 +or maybe, a tech joke. + +47 +00:01:48,000 --> 00:01:50,000 +I may do that, of course, it will do the same thing. + +48 +00:01:50,000 --> 00:01:52,000 +It will say, "Okay, why do programmers prefer dark mode? + +49 +00:01:52,000 --> 00:01:55,000 +Because light attract bugs," a common thing, + +50 +00:01:55,000 --> 00:01:57,000 +and if I say, "more," and click, + +51 +00:01:57,000 --> 00:01:58,000 +it will give the same response, + +52 +00:01:58,000 --> 00:02:00,000 +which is vague or incomplete, + +53 +00:02:00,000 --> 00:02:01,000 +but I want to make this work. + +54 +00:02:01,000 --> 00:02:03,000 +So what I want to achieve is + +55 +00:02:03,000 --> 00:02:06,000 +I want my core or Spring AI application + +56 +00:02:06,000 --> 00:02:08,000 +to remember the earlier chat + +57 +00:02:08,000 --> 00:02:10,000 +so that next time when I talk to it, + +58 +00:02:10,000 --> 00:02:11,000 +it should know what I'm talking about. + +59 +00:02:11,000 --> 00:02:12,000 +Okay, now how do we do this? + +60 +00:02:12,000 --> 00:02:14,000 +Now, by default, LLM will not provide this, + +61 +00:02:14,000 --> 00:02:16,000 +and, by default, whatever code you have written, + +62 +00:02:16,000 --> 00:02:18,000 +it will not provide those features. + +63 +00:02:18,000 --> 00:02:19,000 +Now, in this case, in Spring AI, + +64 +00:02:19,000 --> 00:02:21,000 +we got a concept of advisors. + +65 +00:02:21,000 --> 00:02:23,000 +Now, using advisors, you can do + +66 +00:02:23,000 --> 00:02:25,000 +multiple stuff with the Spring. + +67 +00:02:25,000 --> 00:02:29,000 +Now, imagine Spring advisors or Spring AI advisors as this. + +68 +00:02:29,000 --> 00:02:32,000 +When you send the request which goes to LLM, + +69 +00:02:32,000 --> 00:02:34,000 +now, in between, of course, if you don't specify anything, + +70 +00:02:34,000 --> 00:02:36,000 +the request will go to LLM through Spring AI, + +71 +00:02:36,000 --> 00:02:39,000 +and things are working out, but in case, + +72 +00:02:39,000 --> 00:02:42,000 +if you want to change what you're sending + +73 +00:02:42,000 --> 00:02:45,000 +or if you want to monitor what you're sending, + +74 +00:02:45,000 --> 00:02:48,000 +so maybe you want to put a advisor there, + +75 +00:02:48,000 --> 00:02:49,000 +which will check what you're sending + +76 +00:02:49,000 --> 00:02:51,000 +and maybe you want to modify it, + +77 +00:02:51,000 --> 00:02:56,000 +and, also, when you get the response from the LLM model + +78 +00:02:56,000 --> 00:02:58,000 +and that if you want to observe what model, + +79 +00:02:58,000 --> 00:03:00,000 +what answer it is giving you, + +80 +00:03:00,000 --> 00:03:02,000 +even there, you can put an advice. + +81 +00:03:02,000 --> 00:03:03,000 +Now, this is helpful if you want to, + +82 +00:03:03,000 --> 00:03:05,000 +let's say, safeguard your application. + +83 +00:03:05,000 --> 00:03:08,000 +Maybe you want to censor some text from your application. + +84 +00:03:08,000 --> 00:03:10,000 +You can do that with advisors. + +85 +00:03:10,000 --> 00:03:11,000 +Maybe you want to, remember, + +86 +00:03:11,000 --> 00:03:14,000 +you want to provide the memory, you can use advisor. + +87 +00:03:14,000 --> 00:03:16,000 +There are multiple things, but, at this point, + +88 +00:03:16,000 --> 00:03:17,000 +let's keep it very simple. + +89 +00:03:17,000 --> 00:03:20,000 +In this example, I want to use advisors + +90 +00:03:20,000 --> 00:03:24,000 +where you can remember my chat history or the conversation. + +91 +00:03:24,000 --> 00:03:26,000 +Now, to achieve that, what we can do is + +92 +00:03:26,000 --> 00:03:29,000 +after this builder, in fact, before you do build, + +93 +00:03:29,000 --> 00:03:31,000 +I will add our advisor, and we can do that + +94 +00:03:31,000 --> 00:03:34,000 +with the help of default advisors. + +95 +00:03:34,000 --> 00:03:36,000 +So as I mentioned before, the different advisors available, + +96 +00:03:36,000 --> 00:03:39,000 +one of them is safeguard, and then we've got memory, + +97 +00:03:39,000 --> 00:03:42,000 +and list goes on, but, here, I want to use + +98 +00:03:42,000 --> 00:03:42,000 +for memory, right? + +99 +00:03:42,000 --> 00:03:44,000 +So in this case, you want to specify + +100 +00:03:44,000 --> 00:03:46,000 +which advisors you want to use. + +101 +00:03:46,000 --> 00:03:49,000 +So I want to use this MessageChatMemoryAdvisor + +102 +00:03:49,000 --> 00:03:51,000 +because I want to provide the memory for the chat, + +103 +00:03:51,000 --> 00:03:52,000 +okay, and that's it. + +104 +00:03:52,000 --> 00:03:54,000 +Okay, there's one more thing. + +105 +00:03:54,000 --> 00:03:56,000 +It will say, "Okay, I'll provide the advisor, + +106 +00:03:56,000 --> 00:03:58,000 +but where you want to store it?" + +107 +00:03:58,000 --> 00:04:00,000 +So I will say, okay, this time, let's go + +108 +00:04:00,000 --> 00:04:02,000 +for the InMemoryChatMemory. + +109 +00:04:02,000 --> 00:04:04,000 +Okay, I know there are certain time + +110 +00:04:04,000 --> 00:04:07,000 +there are things when, in Java, where you have + +111 +00:04:07,000 --> 00:04:08,000 +weird names, but that's okay. + +112 +00:04:08,000 --> 00:04:11,000 +It helps you to know what it is doing, right? + +113 +00:04:11,000 --> 00:04:14,000 +So it's InMemoryChatMemory, because you're providing + +114 +00:04:14,000 --> 00:04:16,000 +the message chat memory advisor here. + +115 +00:04:16,000 --> 00:04:18,000 +So if you go to memory chat advisor + +116 +00:04:18,000 --> 00:04:21,000 +or MessageChatMemoryAdvisor, + +117 +00:04:21,000 --> 00:04:23,000 +it has multiple features. + +118 +00:04:23,000 --> 00:04:26,000 +So we can see we have around, before. + +119 +00:04:26,000 --> 00:04:28,000 +So if you want to check, before you send to LLM, + +120 +00:04:28,000 --> 00:04:29,000 +you'll be using before. + +121 +00:04:29,000 --> 00:04:33,000 +If you want to do it after, then we got after as well. + +122 +00:04:33,000 --> 00:04:36,000 +So there are multiple options, and InMemoryChatMemory, + +123 +00:04:36,000 --> 00:04:39,000 +this implements chat memory, + +124 +00:04:39,000 --> 00:04:40,000 +and it provides multiple options. + +125 +00:04:40,000 --> 00:04:42,000 +You can add the conversation. + +126 +00:04:42,000 --> 00:04:43,000 +You can get the conversation. + +127 +00:04:43,000 --> 00:04:45,000 +You can clear the memory. + +128 +00:04:45,000 --> 00:04:46,000 +So I can do multiple stuff. + +129 +00:04:46,000 --> 00:04:48,000 +At this point, I don't want to do all those stuff. + +130 +00:04:48,000 --> 00:04:49,000 +Let's keep it very simple. + +131 +00:04:49,000 --> 00:04:52,000 +I want to enable the memory there, + +132 +00:04:52,000 --> 00:04:54,000 +and by doing this, I hope it will work. + +133 +00:04:54,000 --> 00:04:56,000 +Let's save this and restart. + +134 +00:04:56,000 --> 00:04:59,000 +Let's go back to our code here, + +135 +00:04:59,000 --> 00:05:01,000 +and this time, I will say the same joke. + +136 +00:05:01,000 --> 00:05:03,000 +In fact, I will just copy this, restart. + +137 +00:05:03,000 --> 00:05:04,000 +Just to keep it clean. + +138 +00:05:04,000 --> 00:05:08,000 +Enter, and it will tell me a joke, same poor joke. + +139 +00:05:08,000 --> 00:05:11,000 +I would say, "more," and, this time, + +140 +00:05:11,000 --> 00:05:13,000 +you can see it is giving you more jokes. + +141 +00:05:13,000 --> 00:05:16,000 +It says, "Why do Java developer wear glasses?" + +142 +00:05:16,000 --> 00:05:17,000 +And blah, blah, blah. + +143 +00:05:17,000 --> 00:05:19,000 +There there are five jokes here. + +144 +00:05:19,000 --> 00:05:20,000 +Let's try one more. + +145 +00:05:20,000 --> 00:05:23,000 +I will say, "What is Spring AI?" + +146 +00:05:23,000 --> 00:05:24,000 +Okay, blah, blah, blah, + +147 +00:05:24,000 --> 00:05:29,000 +and I will say, okay, "Explain in one line." + +148 +00:05:29,000 --> 00:05:31,000 +So it should know, right, what I'm talking about, + +149 +00:05:31,000 --> 00:05:32,000 +what to explain in one line. + +150 +00:05:32,000 --> 00:05:34,000 +So now, it knows what I'm talking about. + +151 +00:05:34,000 --> 00:05:36,000 +Okay, this is what you get from the advisors. + +152 +00:05:36,000 --> 00:05:39,000 +Also, if I want to test it with this, + +153 +00:05:39,000 --> 00:05:42,000 +"Tell me I tech joke," Enter, and you got a joke, + +154 +00:05:42,000 --> 00:05:45,000 +and if I say, "more," for the next request, + +155 +00:05:45,000 --> 00:05:46,000 +oh, yeah, it knows what I'm talking about. + +156 +00:05:46,000 --> 00:05:50,000 +So this is how, basically, you can use the advisors. + +157 +00:05:50,000 --> 00:05:52,000 +So yeah, that's from this video, see you in the next video. + diff --git a/23 - Spring AI/011 Running Model Locally with Ollama_en.srt b/23 - Spring AI/011 Running Model Locally with Ollama_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..3de49bfa0c90d4be7501c19062702d5af56b7b9b --- /dev/null +++ b/23 - Spring AI/011 Running Model Locally with Ollama_en.srt @@ -0,0 +1,560 @@ +1 +00:00:00,000 --> 00:00:03,000 +(light music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now till this point, we were able to run the application + +3 +00:00:05,000 --> 00:00:10,000 +and we were using the OpenAI model as a service. + +4 +00:00:10,000 --> 00:00:11,000 +So basically we got the API. + +5 +00:00:11,000 --> 00:00:14,000 +And then whenever you want to ask something, + +6 +00:00:14,000 --> 00:00:16,000 +basically the request goes to the OpenAI server + +7 +00:00:16,000 --> 00:00:18,000 +and then you get the response. + +8 +00:00:18,000 --> 00:00:20,000 +Of course, you need to have account there, you have + +9 +00:00:20,000 --> 00:00:24,000 +to add money, because things are running on their server. + +10 +00:00:24,000 --> 00:00:25,000 +But then you decided, "Hey, you know, + +11 +00:00:25,000 --> 00:00:27,000 +I got a laptop, I got a desktop. + +12 +00:00:27,000 --> 00:00:29,000 +I want to run this model in my machine + +13 +00:00:29,000 --> 00:00:31,000 +so that I don't have to pay for it." + +14 +00:00:31,000 --> 00:00:32,000 +And maybe you want to modify it. + +15 +00:00:32,000 --> 00:00:36,000 +Maybe you want to do the stuff you want. Is it possible? + +16 +00:00:36,000 --> 00:00:38,000 +Of course, there are certain models which are open source, + +17 +00:00:38,000 --> 00:00:39,000 +which are free to use, which you can run + +18 +00:00:39,000 --> 00:00:41,000 +on your local machine. + +19 +00:00:41,000 --> 00:00:44,000 +Now this is done with the help of Ollama. + +20 +00:00:44,000 --> 00:00:45,000 +Now there are multiple options, + +21 +00:00:45,000 --> 00:00:47,000 +but then Ollama is very famous. + +22 +00:00:47,000 --> 00:00:50,000 +Now what Ollama gives you is it says, okay, get up + +23 +00:00:50,000 --> 00:00:52,000 +and running with large language model. + +24 +00:00:52,000 --> 00:00:55,000 +Okay, so basically you can run this on your local machine. + +25 +00:00:55,000 --> 00:00:59,000 +You can run Llama, you can run DeepSeek, Phi, Mistral, + +26 +00:00:59,000 --> 00:01:01,000 +Gemma, and other models. + +27 +00:01:01,000 --> 00:01:02,000 +And in fact, if you want to know which models, + +28 +00:01:02,000 --> 00:01:04,000 +you can just click here and there's a list + +29 +00:01:04,000 --> 00:01:06,000 +of models available. + +30 +00:01:06,000 --> 00:01:08,000 +You can even search for your favorite one. + +31 +00:01:08,000 --> 00:01:11,000 +I have tried Gemma, I've tried DeepSeek, + +32 +00:01:11,000 --> 00:01:13,000 +Llama, I've tried, and multiple things. + +33 +00:01:13,000 --> 00:01:15,000 +Mistral is something which by default picks up + +34 +00:01:16,000 --> 00:01:19,000 +by Spring AI, and I will show you that in some time. + +35 +00:01:19,000 --> 00:01:21,000 +So first of all, let's go to the website. + +36 +00:01:21,000 --> 00:01:23,000 +And before you run your model, you need + +37 +00:01:23,000 --> 00:01:24,000 +to get Ollama on your machine. + +38 +00:01:24,000 --> 00:01:26,000 +So it's very simple. You can click on Download. + +39 +00:01:26,000 --> 00:01:28,000 +It will take some time to download. + +40 +00:01:28,000 --> 00:01:30,000 +You can see it says download for Windows based + +41 +00:01:30,000 --> 00:01:32,000 +on which OS you're using. + +42 +00:01:32,000 --> 00:01:34,000 +I'm using Windows here, so I'll just click on this. + +43 +00:01:34,000 --> 00:01:35,000 +It will take some time to download, + +44 +00:01:35,000 --> 00:01:37,000 +because the file is a bit heavy. + +45 +00:01:37,000 --> 00:01:40,000 +It's around 1 GB, but I will just stop it, + +46 +00:01:40,000 --> 00:01:41,000 +because I already have Ollama set up. + +47 +00:01:41,000 --> 00:01:43,000 +It's very simple to download. + +48 +00:01:43,000 --> 00:01:44,000 +Next, next, next, finish. Okay. + +49 +00:01:44,000 --> 00:01:47,000 +In between you have to say, I agree, don't have a choice. + +50 +00:01:47,000 --> 00:01:49,000 +But once you've got Ollama, + +51 +00:01:49,000 --> 00:01:53,000 +you should see that in your softwares. + +52 +00:01:53,000 --> 00:01:56,000 +And the way you can use that is by opening a terminal. + +53 +00:01:56,000 --> 00:01:59,000 +And you can use certain commands here to check that. + +54 +00:01:59,000 --> 00:02:02,000 +So you will say ollama to check if Ollama is there. + +55 +00:02:02,000 --> 00:02:04,000 +And when you see these things here, + +56 +00:02:04,000 --> 00:02:06,000 +that means Ollama is there. + +57 +00:02:06,000 --> 00:02:09,000 +And if I want to check what are the models I have + +58 +00:02:09,000 --> 00:02:11,000 +in this machine, if I click on Enter, + +59 +00:02:11,000 --> 00:02:12,000 +so you have to say ollama list, + +60 +00:02:12,000 --> 00:02:15,000 +it will give the list of models you got. + +61 +00:02:15,000 --> 00:02:17,000 +Now, when you try this on your machine, + +62 +00:02:17,000 --> 00:02:18,000 +you will not be having any, + +63 +00:02:18,000 --> 00:02:21,000 +because when you download for Ollama for the first time, + +64 +00:02:21,000 --> 00:02:22,000 +you will not get the models by default, + +65 +00:02:22,000 --> 00:02:25,000 +because each model is heavy. + +66 +00:02:25,000 --> 00:02:27,000 +In fact, there are light with models as well. + +67 +00:02:27,000 --> 00:02:29,000 +But since I want to test out different models, + +68 +00:02:29,000 --> 00:02:30,000 +I have tried all these things. + +69 +00:02:30,000 --> 00:02:32,000 +But then let's say if you don't have any of this, + +70 +00:02:32,000 --> 00:02:35,000 +how do you get this in your machine? + +71 +00:02:35,000 --> 00:02:37,000 +It's quite simple. You just go back to the homepage. + +72 +00:02:37,000 --> 00:02:40,000 +And okay, in fact, you can go to Models, + +73 +00:02:40,000 --> 00:02:42,000 +and which model you want to work with. + +74 +00:02:42,000 --> 00:02:45,000 +So let's say if you want to work with Mistral, + +75 +00:02:45,000 --> 00:02:47,000 +you'll click on Mistral here, + +76 +00:02:47,000 --> 00:02:50,000 +and based on how many parameters you need, + +77 +00:02:50,000 --> 00:02:52,000 +in fact, they got only one. + +78 +00:02:52,000 --> 00:02:54,000 +I think? No, they've got multiple options. + +79 +00:02:54,000 --> 00:02:57,000 +But let's say if you want to go for 7b, + +80 +00:02:57,000 --> 00:02:58,000 +you just copy this command here. + +81 +00:02:58,000 --> 00:03:02,000 +It says ollama run mistral, and go back here and say Enter. + +82 +00:03:02,000 --> 00:03:05,000 +Now, if you run this command, it will first check, + +83 +00:03:05,000 --> 00:03:07,000 +do you have this model available? + +84 +00:03:07,000 --> 00:03:10,000 +If you have it, that's great. It will run this. + +85 +00:03:10,000 --> 00:03:12,000 +If not now, since in this machine I already have Mistral, + +86 +00:03:12,000 --> 00:03:16,000 +that's why it was quick, because it just running it now. + +87 +00:03:16,000 --> 00:03:17,000 +But if you don't have it, + +88 +00:03:17,000 --> 00:03:21,000 +it will download the new version on your machine. + +89 +00:03:21,000 --> 00:03:23,000 +Now I'm running Mistral. Okay, so this says send a message. + +90 +00:03:23,000 --> 00:03:27,000 +I will say, what is Spring AI? + +91 +00:03:27,000 --> 00:03:28,000 +In fact, I've entered the wrong one. + +92 +00:03:28,000 --> 00:03:30,000 +It says Spring All-In-One. No, no. + +93 +00:03:30,000 --> 00:03:33,000 +What is spring AI? Enter. + +94 +00:03:33,000 --> 00:03:36,000 +And it will give you what Spring AI is, + +95 +00:03:36,000 --> 00:03:38,000 +does not appear to be officially recognized term. + +96 +00:03:38,000 --> 00:03:41,000 +Now that's the problem with the local things. + +97 +00:03:42,000 --> 00:03:44,000 +It's been trained on the old data. + +98 +00:03:44,000 --> 00:03:45,000 +And when you're using locally, + +99 +00:03:45,000 --> 00:03:48,000 +you have to train it with your own data + +100 +00:03:48,000 --> 00:03:50,000 +so that it will know what you are talking about. + +101 +00:03:51,000 --> 00:03:55,000 +But if I say, tell me a joke, it will do that. Okay. + +102 +00:03:55,000 --> 00:03:58,000 +So you can see it's giving programming jokes by default. + +103 +00:03:58,000 --> 00:04:01,000 +I will say more. So yeah, by default conversation is there. + +104 +00:04:01,000 --> 00:04:03,000 +Okay, so we don't have to worry about the model. + +105 +00:04:03,000 --> 00:04:04,000 +It is working. + +106 +00:04:04,000 --> 00:04:07,000 +Now, once you know it is running on this thing, + +107 +00:04:07,000 --> 00:04:09,000 +how will you run it on the code? + +108 +00:04:09,000 --> 00:04:12,000 +I mean, you want to use this in the code. + +109 +00:04:12,000 --> 00:04:13,000 +So let's go back to the code, + +110 +00:04:13,000 --> 00:04:15,000 +and there are things which we have to change here. + +111 +00:04:15,000 --> 00:04:18,000 +Okay, first of all, I'll just close all these things. + +112 +00:04:18,000 --> 00:04:20,000 +Things like you have to change the key. + +113 +00:04:20,000 --> 00:04:21,000 +Okay, do we need a key? + +114 +00:04:21,000 --> 00:04:24,000 +See, we need a key when you're running a model on the cloud. + +115 +00:04:24,000 --> 00:04:26,000 +Here, we are running on this local machine. + +116 +00:04:26,000 --> 00:04:27,000 +How will you check it? + +117 +00:04:28,000 --> 00:04:30,000 +Also, I would just want to point out one more thing. + +118 +00:04:30,000 --> 00:04:32,000 +These models are a bit heavy, okay? + +119 +00:04:32,000 --> 00:04:36,000 +Mistral is seven B parameters. + +120 +00:04:36,000 --> 00:04:38,000 +So I think it's not that heavy, + +121 +00:04:38,000 --> 00:04:41,000 +but if you see the other models example, okay, + +122 +00:04:41,000 --> 00:04:43,000 +so this is 4.1, still manageable. + +123 +00:04:43,000 --> 00:04:45,000 +But if you go to let's say DeepSeek, + +124 +00:04:46,000 --> 00:04:47,000 +which has multiple options, + +125 +00:04:47,000 --> 00:04:49,000 +and if you see here, this is 4.7, + +126 +00:04:49,000 --> 00:04:51,000 +but if you go for 32b, it is 20 GB. + +127 +00:04:51,000 --> 00:04:53,000 +So you need a good amount of RAM to run this + +128 +00:04:53,000 --> 00:04:55,000 +and good amount of CPU power, GPU power. + +129 +00:04:55,000 --> 00:04:58,000 +So based on your system configuration right out, + +130 +00:04:58,000 --> 00:05:00,000 +try a bigger one first, and it's not working, + +131 +00:05:00,000 --> 00:05:02,000 +then try the smaller one, it should work. + +132 +00:05:02,000 --> 00:05:05,000 +Provided your laptop is safe after running the big model. + +133 +00:05:05,000 --> 00:05:07,000 +I'm just kidding. Nowadays laptops are safe. + +134 +00:05:07,000 --> 00:05:10,000 +Anyway, so let's go back to our code. + +135 +00:05:10,000 --> 00:05:12,000 +And there's certain things which we have to change. + +136 +00:05:12,000 --> 00:05:14,000 +And also we need to create a controller, + +137 +00:05:14,000 --> 00:05:16,000 +because this controller works with the OpenAI. + +138 +00:05:16,000 --> 00:05:19,000 +We want a controller which will work with the DeepSeek, + +139 +00:05:19,000 --> 00:05:21,000 +or maybe the local model, basically Ollama. + +140 +00:05:21,000 --> 00:05:23,000 +And we'll do that in the next video. + diff --git a/23 - Spring AI/012 Source-Code.url b/23 - Spring AI/012 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..7e3aa577f1da061ab9769f1ab0ee7117a1d23385 --- /dev/null +++ b/23 - Spring AI/012 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_12 \ No newline at end of file diff --git a/23 - Spring AI/012 Spring AI with Ollama_en.srt b/23 - Spring AI/012 Spring AI with Ollama_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..23d4c9164755a4fbd297a6a74bb55aa12ea0f4ed --- /dev/null +++ b/23 - Spring AI/012 Spring AI with Ollama_en.srt @@ -0,0 +1,544 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So now let's try to configure + +3 +00:00:05,000 --> 00:00:07,000 +our Spring AI application + +4 +00:00:07,000 --> 00:00:08,000 +to use Ollama. + +5 +00:00:08,000 --> 00:00:11,000 +So the way you can do that, first of all, + +6 +00:00:11,000 --> 00:00:12,000 +let's not change the properties. + +7 +00:00:12,000 --> 00:00:15,000 +I will show you what property we have to change, + +8 +00:00:15,000 --> 00:00:17,000 +but I want to create the controller first + +9 +00:00:17,000 --> 00:00:19,000 +because it is using the OpenAI controller. + +10 +00:00:19,000 --> 00:00:20,000 +I want to use Ollama + +11 +00:00:20,000 --> 00:00:22,000 +or maybe I can change the code here itself, + +12 +00:00:22,000 --> 00:00:25,000 +but since I wanted to have a different repo + +13 +00:00:25,000 --> 00:00:29,000 +or a different code file, I will say OllamaController + +14 +00:00:29,000 --> 00:00:30,000 +and this will have the same code. + +15 +00:00:30,000 --> 00:00:33,000 +Okay? Nothing will, I mean not much will change. + +16 +00:00:33,000 --> 00:00:36,000 +First of all, this chat builder will create an issue + +17 +00:00:36,000 --> 00:00:38,000 +because now we have two controllers. + +18 +00:00:38,000 --> 00:00:40,000 +We got, I mean we got two things. + +19 +00:00:40,000 --> 00:00:42,000 +We got OllamaController, we got OpenAI controller, + +20 +00:00:42,000 --> 00:00:44,000 +and somewhere we have to configure that, + +21 +00:00:44,000 --> 00:00:45,000 +which one you want to use. + +22 +00:00:45,000 --> 00:00:47,000 +But what if I say I can just simply + +23 +00:00:47,000 --> 00:00:49,000 +stop the mapping, my job is done, right? + +24 +00:00:49,000 --> 00:00:52,000 +When you stop the mapping, it'll not even go here. + +25 +00:00:52,000 --> 00:00:54,000 +I mean, that's what I'm thinking. + +26 +00:00:54,000 --> 00:00:59,000 +And also I want to use this thing here, not this part. + +27 +00:01:00,000 --> 00:01:03,000 +So just comment this part here + +28 +00:01:03,000 --> 00:01:07,000 +and let's try this one. + +29 +00:01:07,000 --> 00:01:11,000 +So this is not OpenAI now, this is OllamaController + +30 +00:01:12,000 --> 00:01:16,000 +and we'll need to use OllamaChat model. + +31 +00:01:16,000 --> 00:01:18,000 +Okay. Now it's not there. + +32 +00:01:18,000 --> 00:01:18,000 +Now why it's not there? + +33 +00:01:18,000 --> 00:01:20,000 +Because in the configuration + +34 +00:01:20,000 --> 00:01:23,000 +or in the pom.xml, we have only working + +35 +00:01:23,000 --> 00:01:26,000 +with the OpenAI one, but not the Ollama one. + +36 +00:01:26,000 --> 00:01:28,000 +So we have multiple ways to do that. + +37 +00:01:28,000 --> 00:01:30,000 +One is we'll go to start.spring.io, + +38 +00:01:30,000 --> 00:01:31,000 +of course I have a code with me. + +39 +00:01:31,000 --> 00:01:33,000 +I can just copy paste it. + +40 +00:01:33,000 --> 00:01:35,000 +But if you want to search where you will find it, + +41 +00:01:35,000 --> 00:01:40,000 +you'll just go to add dependency and search for Ollama. + +42 +00:01:40,000 --> 00:01:43,000 +And this will, when you click on explore, + +43 +00:01:43,000 --> 00:01:46,000 +they will give you this. + +44 +00:01:46,000 --> 00:01:48,000 +So you can just copy this dependency. + +45 +00:01:48,000 --> 00:01:49,000 +You don't have to create a new project, don't worry. + +46 +00:01:49,000 --> 00:01:54,000 +And go back here and paste this dependency. + +47 +00:01:54,000 --> 00:01:56,000 +Now you've got two, so you've got OpenAI + +48 +00:01:56,000 --> 00:01:57,000 +and you've got Ollama. + +49 +00:01:58,000 --> 00:02:00,000 +Just reload your Maven + +50 +00:02:00,000 --> 00:02:02,000 +because you have made some changes in the Maven file + +51 +00:02:02,000 --> 00:02:06,000 +or pom.xml file, and go back to OllamaController. + +52 +00:02:06,000 --> 00:02:08,000 +And if you say control space, now, + +53 +00:02:08,000 --> 00:02:11,000 +okay, it looks like there's some issue with the ID. + +54 +00:02:11,000 --> 00:02:15,000 +Let me just manually do that. I would say ORG dot this. + +55 +00:02:15,000 --> 00:02:16,000 +Okay, problem solved, there was some issue + +56 +00:02:16,000 --> 00:02:17,000 +with the loading. + +57 +00:02:17,000 --> 00:02:18,000 +In case if you find this issue, + +58 +00:02:18,000 --> 00:02:20,000 +you can manually type it. + +59 +00:02:20,000 --> 00:02:22,000 +Not normally the case, but it happened now. + +60 +00:02:22,000 --> 00:02:24,000 +Anyway, once you've got to Ollama chat model, + +61 +00:02:24,000 --> 00:02:25,000 +you can just pass it here + +62 +00:02:25,000 --> 00:02:28,000 +and this will assign to the chat line. + +63 +00:02:28,000 --> 00:02:30,000 +So things should work out now. + +64 +00:02:30,000 --> 00:02:32,000 +And if I restart + +65 +00:02:32,000 --> 00:02:34,000 +after making these changes, nothing much will change + +66 +00:02:34,000 --> 00:02:36,000 +because here we are not even dependent on the model, + +67 +00:02:36,000 --> 00:02:38,000 +it's just that we're specifying that here. + +68 +00:02:38,000 --> 00:02:41,000 +And there is an issue. + +69 +00:02:41,000 --> 00:02:42,000 +It says there is a problem + +70 +00:02:42,000 --> 00:02:44,000 +with the models, we got two models now, + +71 +00:02:44,000 --> 00:02:47,000 +it is injecting both in the chat model. + +72 +00:02:47,000 --> 00:02:48,000 +Okay. + +73 +00:02:48,000 --> 00:02:51,000 +In that case you can just, + +74 +00:02:51,000 --> 00:02:53,000 +because I'm using builder here, that's the issue. + +75 +00:02:53,000 --> 00:02:55,000 +I think I'll just comment this part. + +76 +00:02:57,000 --> 00:02:58,000 +And if you want to use builder, then you have + +77 +00:02:58,000 --> 00:03:00,000 +to manually disable the builder + +78 +00:03:00,000 --> 00:03:03,000 +and then write out, okay, no issue. + +79 +00:03:03,000 --> 00:03:05,000 +I love to resolve the bugs + +80 +00:03:05,000 --> 00:03:07,000 +or issues during the video. + +81 +00:03:07,000 --> 00:03:09,000 +Anyway, so it is working now. + +82 +00:03:09,000 --> 00:03:10,000 +And now let me just test it. + +83 +00:03:10,000 --> 00:03:14,000 +So here I will say, "Tell me a joke." + +84 +00:03:14,000 --> 00:03:16,000 +And when I click on send, + +85 +00:03:16,000 --> 00:03:18,000 +it's executing, and you got the response. + +86 +00:03:18,000 --> 00:03:19,000 +Now this is weird. + +87 +00:03:19,000 --> 00:03:20,000 +Apart from the controller, + +88 +00:03:20,000 --> 00:03:21,000 +I have not gone through anything. + +89 +00:03:21,000 --> 00:03:24,000 +Of course the pom file and the controller, + +90 +00:03:24,000 --> 00:03:26,000 +no way we are mentioning that which model or + +91 +00:03:26,000 --> 00:03:27,000 +because in this machine I got + +92 +00:03:27,000 --> 00:03:29,000 +Ollama, which model I have to run. + +93 +00:03:29,000 --> 00:03:32,000 +And if you go back to your code by default, + +94 +00:03:32,000 --> 00:03:35,000 +as I mentioned before, it goes for Mistral. + +95 +00:03:35,000 --> 00:03:37,000 +So if you don't have Mistral in your machine + +96 +00:03:37,000 --> 00:03:40,000 +or in your model in the list, it'll not work. + +97 +00:03:40,000 --> 00:03:41,000 +It might, let me just try that. + +98 +00:03:41,000 --> 00:03:45,000 +So I would say /bye + +99 +00:03:45,000 --> 00:03:49,000 +and ollama, I think I forgot the command. + +100 +00:03:49,000 --> 00:03:53,000 +It should be ollama remove mistral and non command. + +101 +00:03:53,000 --> 00:03:57,000 +Okay, rm, okay, of course. + +102 +00:03:57,000 --> 00:04:00,000 +How can I say remove, rm mistral. + +103 +00:04:00,000 --> 00:04:01,000 +And you can see Mistral deleted. + +104 +00:04:01,000 --> 00:04:03,000 +And once you delete it, + +105 +00:04:03,000 --> 00:04:05,000 +and if you try the code now, + +106 +00:04:05,000 --> 00:04:08,000 +the same code, you can see it says there's a error. + +107 +00:04:08,000 --> 00:04:11,000 +And if you go back to your code, + +108 +00:04:11,000 --> 00:04:12,000 +you can see we got an error. + +109 +00:04:12,000 --> 00:04:13,000 +So if you don't have Mistral, + +110 +00:04:13,000 --> 00:04:15,000 +it'll by default try to pick up the Mistral. + +111 +00:04:15,000 --> 00:04:17,000 +But let's say if I don't want to use Mistral, + +112 +00:04:17,000 --> 00:04:19,000 +if I want to use some other model, how do I do that? + +113 +00:04:19,000 --> 00:04:21,000 +You just come back here and specify which + +114 +00:04:21,000 --> 00:04:22,000 +model you want to use. + +115 +00:04:22,000 --> 00:04:26,000 +So the spring.ai.ollama.chat.options. + +116 +00:04:27,000 --> 00:04:29,000 +In fact, when you have a ultimate version + +117 +00:04:29,000 --> 00:04:31,000 +or you can just get this property from the internet + +118 +00:04:31,000 --> 00:04:34,000 +or from the documentation, .model equal to, + +119 +00:04:34,000 --> 00:04:37,000 +this is where you specify the actual model name. + +120 +00:04:37,000 --> 00:04:40,000 +And if I show you which models I have, if you scroll up, + +121 +00:04:40,000 --> 00:04:44,000 +which we have seen before, let's say I want to use 7b, + +122 +00:04:44,000 --> 00:04:47,000 +so just copy this and that's the model I'm going to use now. + +123 +00:04:47,000 --> 00:04:50,000 +Let's restart. So there's no error. + +124 +00:04:50,000 --> 00:04:51,000 +Let's go back to insomnia, run. + +125 +00:04:51,000 --> 00:04:53,000 +And you've got the response + +126 +00:04:53,000 --> 00:04:56,000 +and it's doing some thinking here, but you got the joke. + +127 +00:04:56,000 --> 00:04:59,000 +And if I go back to the code, it says deepseek. + +128 +00:04:59,000 --> 00:05:00,000 +So that's how you can configure. + +129 +00:05:00,000 --> 00:05:02,000 +So as I mentioned before, by default is Mistral, + +130 +00:05:02,000 --> 00:05:03,000 +but you need to configure if you want + +131 +00:05:03,000 --> 00:05:05,000 +to use some other model. + +132 +00:05:05,000 --> 00:05:08,000 +And we don't need keys to run it on the local. + +133 +00:05:08,000 --> 00:05:09,000 +And that's fancy, right? + +134 +00:05:09,000 --> 00:05:11,000 +So yeah, that's it from how do you run + +135 +00:05:11,000 --> 00:05:14,000 +or how do you connect your Spring with the local one, + +136 +00:05:14,000 --> 00:05:16,000 +and things are working out. + diff --git a/23 - Spring AI/external-links.txt b/23 - Spring AI/external-links.txt new file mode 100644 index 0000000000000000000000000000000000000000..fe8b9e0c014e26a04724189136732aa107b91173 --- /dev/null +++ b/23 - Spring AI/external-links.txt @@ -0,0 +1,21 @@ + +004 Source-code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_04 + +006 Source-Code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_06 + +007 Source-Code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_07 + +008 Source-Code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_08 + +009 Source-Code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_09 + +010 Source-Code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_10 + +012 Source-Code +https://github.com/navinreddy20/SpringAIUdemy/tree/master/SpringAICode_Le_12 diff --git a/24 - Docker/001 Docker Introduction_en.srt b/24 - Docker/001 Docker Introduction_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..40bd2911f0d76531d19f22d1be97a632f0859c1a --- /dev/null +++ b/24 - Docker/001 Docker Introduction_en.srt @@ -0,0 +1,96 @@ +1 +00:00:00,000 --> 00:00:02,000 +(bright music) + +2 +00:00:02,000 --> 00:00:04,000 +-: Welcome to another section. + +3 +00:00:04,000 --> 00:00:06,000 +In this section we're gonna talk about Docker. + +4 +00:00:06,000 --> 00:00:08,000 +Now, Docker is very important tool + +5 +00:00:08,000 --> 00:00:10,000 +when it comes to software development in the industry. + +6 +00:00:10,000 --> 00:00:11,000 +And in this entire section + +7 +00:00:11,000 --> 00:00:13,000 +we're gonna talk about what is Docker. + +8 +00:00:13,000 --> 00:00:17,000 +In fact, we'll start with why in this world we need Docker + +9 +00:00:17,000 --> 00:00:18,000 +and what are solutions we have. + +10 +00:00:18,000 --> 00:00:21,000 +Then we'll move towards doing the setup. + +11 +00:00:21,000 --> 00:00:23,000 +How do you run a particular container? + +12 +00:00:23,000 --> 00:00:25,000 +How do you build a Spring boot application, + +13 +00:00:25,000 --> 00:00:28,000 +which you can run on a Docker container, + +14 +00:00:28,000 --> 00:00:30,000 +and also we'll focus on multi containers. + +15 +00:00:30,000 --> 00:00:32,000 +What if you want to use multiple containers? + +16 +00:00:32,000 --> 00:00:35,000 +And for that, we are going to use Docker Compose. + +17 +00:00:35,000 --> 00:00:36,000 +And in this series + +18 +00:00:36,000 --> 00:00:37,000 +we are going to talk about all these things. + +19 +00:00:37,000 --> 00:00:39,000 +So make sure when you're learning, + +20 +00:00:39,000 --> 00:00:41,000 +also practice the things which I'm doing, + +21 +00:00:41,000 --> 00:00:44,000 +because when you do it badly, you will learn more. + +22 +00:00:44,000 --> 00:00:47,000 +Just watching the video, sometime it will not make sense. + +23 +00:00:47,000 --> 00:00:49,000 +So I hope you will enjoy the entire section + +24 +00:00:49,000 --> 00:00:51,000 +and see you there. + diff --git a/24 - Docker/002 What Problem We Are Trying To Solve_en.srt b/24 - Docker/002 What Problem We Are Trying To Solve_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..be9f1ac6f3ab6437d2b38c8bddc55aa07435503c --- /dev/null +++ b/24 - Docker/002 What Problem We Are Trying To Solve_en.srt @@ -0,0 +1,600 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So before we talk about what is Docker, + +3 +00:00:05,000 --> 00:00:06,000 +it is important to understand + +4 +00:00:06,000 --> 00:00:08,000 +what problem we are trying to solve here. + +5 +00:00:08,000 --> 00:00:11,000 +Now, as a software developer, what is your job? + +6 +00:00:11,000 --> 00:00:13,000 +Our job here is to build application, + +7 +00:00:13,000 --> 00:00:16,000 +which millions of people are going to use, + +8 +00:00:16,000 --> 00:00:18,000 +and then, they can use it from anywhere. + +9 +00:00:18,000 --> 00:00:22,000 +And nowadays, we make sure that you are building + +10 +00:00:22,000 --> 00:00:25,000 +the web applications which are deployed on the cloud + +11 +00:00:25,000 --> 00:00:27,000 +so that others can use it. + +12 +00:00:27,000 --> 00:00:28,000 +Now, the question is, how do you do it? + +13 +00:00:28,000 --> 00:00:30,000 +Now, first of all, you write the code, + +14 +00:00:30,000 --> 00:00:32,000 +and then you make sure that you connect with database, + +15 +00:00:32,000 --> 00:00:34,000 +and then you also test your project. + +16 +00:00:34,000 --> 00:00:38,000 +You do a lot of configuration, and then you are done, right? + +17 +00:00:38,000 --> 00:00:40,000 +Now, everything what you're doing now + +18 +00:00:40,000 --> 00:00:42,000 +you are doing on your own machine, + +19 +00:00:42,000 --> 00:00:43,000 +and then it's working fine. + +20 +00:00:43,000 --> 00:00:46,000 +And sometime, when it works, you feel very happy, + +21 +00:00:46,000 --> 00:00:48,000 +and that point, let's say, if you're working with your team, + +22 +00:00:48,000 --> 00:00:51,000 +of course you have to give your project to your team. + +23 +00:00:51,000 --> 00:00:52,000 +Now, what may go wrong there? + +24 +00:00:52,000 --> 00:00:56,000 +The thing is, when you give your project to your team, + +25 +00:00:56,000 --> 00:00:58,000 +they need to do all the things. + +26 +00:00:58,000 --> 00:01:00,000 +Now, when I say all the thing, what exactly I mean? + +27 +00:01:00,000 --> 00:01:01,000 +I'm not saying they have to write + +28 +00:01:01,000 --> 00:01:02,000 +the entire code by themselves now + +29 +00:01:02,000 --> 00:01:05,000 +because you have done the code, but the configuration. + +30 +00:01:05,000 --> 00:01:07,000 +Let's say if you want to make a web application. + +31 +00:01:07,000 --> 00:01:09,000 +For that, you need a server, a web server. + +32 +00:01:09,000 --> 00:01:12,000 +So, when you started working on your project, + +33 +00:01:12,000 --> 00:01:14,000 +you have installed a web server. + +34 +00:01:14,000 --> 00:01:16,000 +But what about database? + +35 +00:01:16,000 --> 00:01:19,000 +So let's say if you're using MySQL, or Postgres, or MongoDB, + +36 +00:01:19,000 --> 00:01:23,000 +you will be installing this software in your machine. + +37 +00:01:23,000 --> 00:01:24,000 +And not just this, + +38 +00:01:24,000 --> 00:01:26,000 +there are multiple configuration involved here. + +39 +00:01:26,000 --> 00:01:28,000 +So what you do is, when you started working on a project, + +40 +00:01:28,000 --> 00:01:29,000 +of course you will not complete + +41 +00:01:29,000 --> 00:01:31,000 +the entire project in a week or a day. + +42 +00:01:31,000 --> 00:01:32,000 +So it will take some time, right? + +43 +00:01:32,000 --> 00:01:35,000 +Now, after doing all the hard work, + +44 +00:01:35,000 --> 00:01:37,000 +now, when you are giving this project to your colleague, + +45 +00:01:37,000 --> 00:01:41,000 +or maybe for testing, they have to do all this setup. + +46 +00:01:41,000 --> 00:01:45,000 +And if one of the configuration is not matching, + +47 +00:01:45,000 --> 00:01:46,000 +your project will not work. + +48 +00:01:46,000 --> 00:01:47,000 +And they will complain, + +49 +00:01:47,000 --> 00:01:48,000 +"Hey, you know, the project + +50 +00:01:48,000 --> 00:01:50,000 +which you have worked on is not working." + +51 +00:01:50,000 --> 00:01:52,000 +And then you will say, "It works on my machine." + +52 +00:01:52,000 --> 00:01:54,000 +I mean, that's a common phase which we use, right? + +53 +00:01:54,000 --> 00:01:56,000 +So basically, we have to make sure + +54 +00:01:56,000 --> 00:01:58,000 +that when you are making a project, + +55 +00:01:58,000 --> 00:01:59,000 +which is working on your machine, + +56 +00:01:59,000 --> 00:02:02,000 +should also work on their machine. + +57 +00:02:02,000 --> 00:02:03,000 +Okay, now, maybe your colleagues + +58 +00:02:03,000 --> 00:02:05,000 +or your friends will understand this, + +59 +00:02:05,000 --> 00:02:07,000 +but what about the testing team? + +60 +00:02:07,000 --> 00:02:08,000 +So, normally in the companies, + +61 +00:02:08,000 --> 00:02:10,000 +we have different teams for it, right? + +62 +00:02:10,000 --> 00:02:13,000 +So we have a development team, testing team, ops team. + +63 +00:02:13,000 --> 00:02:14,000 +So, when you give this project + +64 +00:02:14,000 --> 00:02:16,000 +for your testing team to test it, + +65 +00:02:16,000 --> 00:02:19,000 +even they need to have the same configuration. + +66 +00:02:19,000 --> 00:02:21,000 +So they have to install all the softwares, + +67 +00:02:21,000 --> 00:02:22,000 +they have to do the setup, + +68 +00:02:22,000 --> 00:02:24,000 +they will do the configuration as well. + +69 +00:02:24,000 --> 00:02:26,000 +And if something is not working, again, they will blame you + +70 +00:02:26,000 --> 00:02:28,000 +by saying, "Hey, it's not working on my machine." + +71 +00:02:28,000 --> 00:02:30,000 +Now, even if you do + +72 +00:02:30,000 --> 00:02:33,000 +all the configuration correctly, it might not work, + +73 +00:02:33,000 --> 00:02:36,000 +because it depends upon your underlying OS and hardware. + +74 +00:02:36,000 --> 00:02:38,000 +Sometime, the features which you have used + +75 +00:02:38,000 --> 00:02:40,000 +might not work on their OS. + +76 +00:02:40,000 --> 00:02:41,000 +Maybe you are using Windows, + +77 +00:02:41,000 --> 00:02:44,000 +they're using Mac or Linux, it might not work. + +78 +00:02:44,000 --> 00:02:46,000 +And then, that's only testing phase, right? + +79 +00:02:46,000 --> 00:02:47,000 +What about production? + +80 +00:02:47,000 --> 00:02:49,000 +So let's say testing team was able + +81 +00:02:49,000 --> 00:02:51,000 +to solve all the problem by talking to you, + +82 +00:02:51,000 --> 00:02:53,000 +and now you're giving this project to the ops team. + +83 +00:02:53,000 --> 00:02:55,000 +Now, it is the responsibility + +84 +00:02:55,000 --> 00:02:56,000 +of the ops team to deploy the project. + +85 +00:02:56,000 --> 00:02:59,000 +Now, they also need all the setup, all the configuration, + +86 +00:02:59,000 --> 00:03:01,000 +I mean they will do all this work. + +87 +00:03:01,000 --> 00:03:04,000 +Additionally, when you talk about developer's machine, + +88 +00:03:04,000 --> 00:03:05,000 +we are using a standalone laptops, + +89 +00:03:05,000 --> 00:03:07,000 +or normal desktops, right? + +90 +00:03:07,000 --> 00:03:10,000 +But in production you use heavy softwares, + +91 +00:03:10,000 --> 00:03:13,000 +or heavy servers, and they have different OS. + +92 +00:03:13,000 --> 00:03:15,000 +It may happen that, the project which you have built + +93 +00:03:15,000 --> 00:03:17,000 +is not matching with their configuration. + +94 +00:03:17,000 --> 00:03:19,000 +And again, there'll be thing like, + +95 +00:03:19,000 --> 00:03:21,000 +okay, so you have given them the project on Friday, + +96 +00:03:21,000 --> 00:03:24,000 +and they will call you on Saturday to get this resolved. + +97 +00:03:24,000 --> 00:03:26,000 +Okay, not a happy scenario, right? + +98 +00:03:26,000 --> 00:03:28,000 +So how do we solve this problem? + +99 +00:03:28,000 --> 00:03:29,000 +So the problem here is, + +100 +00:03:29,000 --> 00:03:32,000 +we want to make sure that when you build a project, + +101 +00:03:32,000 --> 00:03:34,000 +you just don't give the project, + +102 +00:03:34,000 --> 00:03:36,000 +because they have to do all the setup. + +103 +00:03:36,000 --> 00:03:37,000 +Of course, with the project, + +104 +00:03:37,000 --> 00:03:40,000 +you will also give a guide, the manual, + +105 +00:03:40,000 --> 00:03:42,000 +what are the softwares needed, what are the configuration, + +106 +00:03:42,000 --> 00:03:47,000 +but then what if you can give them the working project? + +107 +00:03:47,000 --> 00:03:49,000 +Of course, you are giving them the working project, + +108 +00:03:49,000 --> 00:03:52,000 +but then you're not giving the setup. + +109 +00:03:52,000 --> 00:03:53,000 +Now how do you give the setup? + +110 +00:03:53,000 --> 00:03:55,000 +Maybe you can just simply copy your C: drive, right? + +111 +00:03:55,000 --> 00:04:00,000 +So, imagine this, you are not just giving them the setup. + +112 +00:04:00,000 --> 00:04:02,000 +You're saying, "Okay, I have a C: drive with me. + +113 +00:04:02,000 --> 00:04:04,000 +I will just give you this C: drive, + +114 +00:04:04,000 --> 00:04:05,000 +and your problem will be solved." + +115 +00:04:05,000 --> 00:04:08,000 +So what they will do is they will use your C: drive, + +116 +00:04:08,000 --> 00:04:10,000 +and they will start working, because in your C: drive, + +117 +00:04:10,000 --> 00:04:12,000 +okay, when I say C: drive, I'm talking about Windows, + +118 +00:04:12,000 --> 00:04:14,000 +where you have your OS in the C: drive, + +119 +00:04:14,000 --> 00:04:16,000 +it changes depending on the people. + +120 +00:04:16,000 --> 00:04:18,000 +But let's go for general scenario. + +121 +00:04:18,000 --> 00:04:19,000 +Now, there are multiple problem with this. + +122 +00:04:19,000 --> 00:04:21,000 +First of all, if you give your C: drive, what you will have? + +123 +00:04:21,000 --> 00:04:24,000 +Second, can you really copy C: drive if you want to do that? + +124 +00:04:24,000 --> 00:04:26,000 +Of course you cannot do that, right? + +125 +00:04:26,000 --> 00:04:27,000 +You cannot simply copy an OS + +126 +00:04:27,000 --> 00:04:29,000 +from one system and give it to the someone else. + +127 +00:04:29,000 --> 00:04:30,000 +It will not work. + +128 +00:04:30,000 --> 00:04:32,000 +So what are the solution we have? + +129 +00:04:32,000 --> 00:04:33,000 +In fact, there's one more problem. + +130 +00:04:33,000 --> 00:04:34,000 +What if they have a different OS altogether, + +131 +00:04:34,000 --> 00:04:36,000 +which will not support your system, + +132 +00:04:36,000 --> 00:04:37,000 +or your hard drive, which you're giving? + +133 +00:04:37,000 --> 00:04:38,000 +A lot of problem, right? + +134 +00:04:38,000 --> 00:04:40,000 +So, and people used to do this, okay, + +135 +00:04:40,000 --> 00:04:42,000 +so way back, people used to work in this fashion. + +136 +00:04:42,000 --> 00:04:44,000 +They used to build a project + +137 +00:04:44,000 --> 00:04:46,000 +and give the project to the testing team, + +138 +00:04:46,000 --> 00:04:49,000 +or the ops team, where they will do all the setup, + +139 +00:04:49,000 --> 00:04:52,000 +all the configuration, and had a lot of process. + +140 +00:04:52,000 --> 00:04:54,000 +Now, we have to find a solution. + +141 +00:04:54,000 --> 00:04:55,000 +Now, what is a solution here? + +142 +00:04:55,000 --> 00:04:57,000 +Now, before Docker, + +143 +00:04:57,000 --> 00:04:59,000 +of course we are learning Docker here, right? + +144 +00:04:59,000 --> 00:05:01,000 +But before Docker, Docker solved this problem, + +145 +00:05:01,000 --> 00:05:03,000 +but then we'll talk about Docker later. + +146 +00:05:03,000 --> 00:05:05,000 +But before Docker, we had one more solution, + +147 +00:05:05,000 --> 00:05:07,000 +which is called virtualization. + +148 +00:05:07,000 --> 00:05:09,000 +Okay, now what is virtualization, + +149 +00:05:09,000 --> 00:05:10,000 +and how it solves this problem? + +150 +00:05:10,000 --> 00:05:12,000 +That, we'll see in the next video. + diff --git a/24 - Docker/003 Solution With Virtualization_en.srt b/24 - Docker/003 Solution With Virtualization_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..63b2bdc4b8a7c5d395751f5af27dd50470bf0c72 --- /dev/null +++ b/24 - Docker/003 Solution With Virtualization_en.srt @@ -0,0 +1,1120 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music) + +2 +00:00:02,000 --> 00:00:05,000 +-: So one solution is virtualization + +3 +00:00:05,000 --> 00:00:07,000 +for our previous problem. + +4 +00:00:07,000 --> 00:00:09,000 +So the problem is, when you build a software + +5 +00:00:09,000 --> 00:00:12,000 +or the application which works on your machine, + +6 +00:00:12,000 --> 00:00:14,000 +it may not work on someone else machine, + +7 +00:00:14,000 --> 00:00:17,000 +is because even if they do the configuration, + +8 +00:00:17,000 --> 00:00:18,000 +which consumes a lot of time + +9 +00:00:18,000 --> 00:00:20,000 +in terms of installation configuration, + +10 +00:00:20,000 --> 00:00:24,000 +even if you do that, it might not work. + +11 +00:00:24,000 --> 00:00:26,000 +And to solve that problem, we are saying, + +12 +00:00:26,000 --> 00:00:28,000 +okay, we can use something called virtualization. + +13 +00:00:28,000 --> 00:00:30,000 +Now, before we understand how we can solve + +14 +00:00:30,000 --> 00:00:32,000 +the problem with virtualization, + +15 +00:00:32,000 --> 00:00:35,000 +let's understand, what is virtualization? + +16 +00:00:35,000 --> 00:00:37,000 +So what happens is, + +17 +00:00:37,000 --> 00:00:38,000 +when you talk about your system, + +18 +00:00:38,000 --> 00:00:40,000 +so let's say any system in which you work, + +19 +00:00:40,000 --> 00:00:41,000 +even your mobile phone, or your desktop, + +20 +00:00:41,000 --> 00:00:44,000 +laptop, servers, they all do the same thing. + +21 +00:00:44,000 --> 00:00:47,000 +What you have is you have a basic hardware, okay? + +22 +00:00:47,000 --> 00:00:49,000 +So you have a hardware layer. + +23 +00:00:49,000 --> 00:00:53,000 +Now this is your actual hardware, your CPU, + +24 +00:00:53,000 --> 00:00:55,000 +RAM, motherboard, all the stuff. + +25 +00:00:55,000 --> 00:00:58,000 +Now, on top of this hardware, to make it work, + +26 +00:00:58,000 --> 00:00:59,000 +of course, as a user, + +27 +00:00:59,000 --> 00:01:01,000 +we will not be interacting with the hardware directly. + +28 +00:01:01,000 --> 00:01:03,000 +What you need in between is OS. + +29 +00:01:03,000 --> 00:01:05,000 +Now, this is your operating system, + +30 +00:01:05,000 --> 00:01:07,000 +which can be Windows, Linux, Mac, doesn't matter. + +31 +00:01:07,000 --> 00:01:09,000 +There are multiple OS options available. + +32 +00:01:09,000 --> 00:01:11,000 +So if you want to use your hardware, + +33 +00:01:11,000 --> 00:01:13,000 +the only way you can do that + +34 +00:01:13,000 --> 00:01:15,000 +is with the help of operating systems. + +35 +00:01:15,000 --> 00:01:17,000 +And that's why when you buy a laptop, + +36 +00:01:17,000 --> 00:01:18,000 +you get two things, not one. + +37 +00:01:18,000 --> 00:01:19,000 +You're not just buying a laptop, + +38 +00:01:19,000 --> 00:01:21,000 +you're buying Windows with it. + +39 +00:01:21,000 --> 00:01:23,000 +Yeah, they say they give it for free, + +40 +00:01:23,000 --> 00:01:24,000 +but you do pay for it. + +41 +00:01:24,000 --> 00:01:25,000 +Okay, keeping that aside, + +42 +00:01:25,000 --> 00:01:28,000 +you got a hardware and you got an OS. + +43 +00:01:28,000 --> 00:01:31,000 +Now, in real world, we directly don't use OS, right? + +44 +00:01:31,000 --> 00:01:32,000 +We use software, for example, + +45 +00:01:32,000 --> 00:01:34,000 +if you want to add two numbers, + +46 +00:01:34,000 --> 00:01:37,000 +you can't simply ask your OS to add two numbers, + +47 +00:01:37,000 --> 00:01:38,000 +you need a calculator. + +48 +00:01:38,000 --> 00:01:39,000 +Now you might be saying, + +49 +00:01:39,000 --> 00:01:41,000 +hey, you know calculator you'll find in every device? + +50 +00:01:41,000 --> 00:01:44,000 +Not exactly if, I don't know if you have used iPad, + +51 +00:01:44,000 --> 00:01:45,000 +we don't have calculator there. + +52 +00:01:45,000 --> 00:01:47,000 +Anyway, but you can install it. + +53 +00:01:47,000 --> 00:01:49,000 +The point is, if you want to perform any operation, + +54 +00:01:49,000 --> 00:01:52,000 +you'll need a application or a software. + +55 +00:01:52,000 --> 00:01:54,000 +And as a user, you use software, + +56 +00:01:54,000 --> 00:01:57,000 +software talks to OS, OS talks to hardware. + +57 +00:01:57,000 --> 00:01:58,000 +That's how the flow is. + +58 +00:01:58,000 --> 00:01:59,000 +Now, let's say as a developer, + +59 +00:01:59,000 --> 00:02:01,000 +how will you use virtualization? + +60 +00:02:01,000 --> 00:02:03,000 +So what you do is, if you want to use virtualization, + +61 +00:02:03,000 --> 00:02:06,000 +you will be installing another OS. + +62 +00:02:06,000 --> 00:02:08,000 +Okay, now that's tricky. + +63 +00:02:08,000 --> 00:02:10,000 +How will you install two OS on the same machine? + +64 +00:02:10,000 --> 00:02:12,000 +I mean, of course we can do dual boot, + +65 +00:02:12,000 --> 00:02:14,000 +where you have two OS in the same system, + +66 +00:02:14,000 --> 00:02:17,000 +but at one time you can use only one. + +67 +00:02:17,000 --> 00:02:19,000 +Something like if you have a laptop, + +68 +00:02:19,000 --> 00:02:21,000 +you can install Windows, or you can use Ubuntu, + +69 +00:02:21,000 --> 00:02:25,000 +you can install both as a dual setup, or dual OS, + +70 +00:02:25,000 --> 00:02:27,000 +but then you can use only one at a time. + +71 +00:02:27,000 --> 00:02:30,000 +In virtualization, you use two OS at the same time. + +72 +00:02:30,000 --> 00:02:32,000 +What we do is, we have a hardware, + +73 +00:02:32,000 --> 00:02:34,000 +and then we install our OS. + +74 +00:02:34,000 --> 00:02:37,000 +Now this is your host OS, the main OS. + +75 +00:02:37,000 --> 00:02:40,000 +Now on top of it, you can install one more OS. + +76 +00:02:40,000 --> 00:02:41,000 +But then how exactly it will work? + +77 +00:02:41,000 --> 00:02:43,000 +Because if you want to install, + +78 +00:02:43,000 --> 00:02:44,000 +let's say you have a hardware, + +79 +00:02:44,000 --> 00:02:45,000 +you have a Windows on top of it, + +80 +00:02:45,000 --> 00:02:48,000 +and now you want to use Ubuntu. + +81 +00:02:48,000 --> 00:02:50,000 +Now, when you try to install Ubuntu onto Windows, + +82 +00:02:50,000 --> 00:02:52,000 +Windows will say what are you doing? + +83 +00:02:52,000 --> 00:02:54,000 +You can't install OS and on our OS. + +84 +00:02:54,000 --> 00:02:58,000 +So Ubuntu says, even if you install me, you need a hardware. + +85 +00:02:58,000 --> 00:03:00,000 +So what you do is you fake it. + +86 +00:03:00,000 --> 00:03:03,000 +You say, okay, what if you can fool Ubuntu by saying, + +87 +00:03:03,000 --> 00:03:06,000 +you know, let me give you something in between, + +88 +00:03:06,000 --> 00:03:09,000 +which is not a hardware, but it will behave like a hardware. + +89 +00:03:09,000 --> 00:03:13,000 +Now, that is your virtual hardware, + +90 +00:03:13,000 --> 00:03:15,000 +or we can say virtual machine. + +91 +00:03:15,000 --> 00:03:18,000 +So you have a hardware, on top of that, you got a OS, + +92 +00:03:18,000 --> 00:03:21,000 +on top of that, you got a machine, + +93 +00:03:21,000 --> 00:03:24,000 +which is not a physical machine, but a virtual machine, + +94 +00:03:24,000 --> 00:03:25,000 +which is actually a software + +95 +00:03:25,000 --> 00:03:26,000 +which fakes like it's a hardware. + +96 +00:03:26,000 --> 00:03:29,000 +And then on top of that you can install Ubuntu. + +97 +00:03:29,000 --> 00:03:32,000 +Now this is called virtualization, + +98 +00:03:32,000 --> 00:03:34,000 +where you are installing an OS + +99 +00:03:34,000 --> 00:03:36,000 +on a virtual hardware or a virtual machine. + +100 +00:03:36,000 --> 00:03:37,000 +And this concept is very famous, okay? + +101 +00:03:37,000 --> 00:03:38,000 +So if you talk about virtual machine, + +102 +00:03:38,000 --> 00:03:40,000 +it's very famous, but the question is why. + +103 +00:03:40,000 --> 00:03:41,000 +Now think about this. + +104 +00:03:41,000 --> 00:03:42,000 +If you go back to our previous example + +105 +00:03:42,000 --> 00:03:44,000 +of a previous problem of docker, + +106 +00:03:44,000 --> 00:03:46,000 +or not docker, but application. + +107 +00:03:46,000 --> 00:03:48,000 +When you build application on your machine, + +108 +00:03:48,000 --> 00:03:51,000 +which is there in your C: drive let's say, + +109 +00:03:51,000 --> 00:03:54,000 +or maybe on your OS, it's working on your machine + +110 +00:03:54,000 --> 00:03:57,000 +is because you have the required software, + +111 +00:03:57,000 --> 00:03:59,000 +like a web server, database, + +112 +00:03:59,000 --> 00:04:01,000 +configuration, network components, + +113 +00:04:01,000 --> 00:04:02,000 +everything they're configured, + +114 +00:04:02,000 --> 00:04:04,000 +and then you have application as well. + +115 +00:04:04,000 --> 00:04:06,000 +Now when you want to give this application + +116 +00:04:06,000 --> 00:04:08,000 +to your colleague, or to your testing team, + +117 +00:04:08,000 --> 00:04:09,000 +or to your ops team, + +118 +00:04:09,000 --> 00:04:13,000 +you can only give the software, not the entire OS. + +119 +00:04:13,000 --> 00:04:15,000 +But what if you can do that? + +120 +00:04:15,000 --> 00:04:17,000 +Virtualization says you can do it. + +121 +00:04:17,000 --> 00:04:18,000 +You know how? + +122 +00:04:18,000 --> 00:04:19,000 +We have hardware, we got host OS, + +123 +00:04:19,000 --> 00:04:22,000 +on top of that we have virtual machine OS, + +124 +00:04:22,000 --> 00:04:23,000 +or virtual machine. + +125 +00:04:23,000 --> 00:04:25,000 +So normally we can implement virtual machine + +126 +00:04:25,000 --> 00:04:26,000 +with help of hypervisor. + +127 +00:04:26,000 --> 00:04:29,000 +And then on top of it, we can install Ubuntu, right? + +128 +00:04:29,000 --> 00:04:32,000 +And on Ubuntu, you can build your application. + +129 +00:04:32,000 --> 00:04:33,000 +It's OS right? + +130 +00:04:33,000 --> 00:04:36,000 +So, open Ubuntu, do all the things which you're doing, + +131 +00:04:36,000 --> 00:04:39,000 +you know, install your web server, install database, + +132 +00:04:39,000 --> 00:04:42,000 +do all the configuration, you have your running app there. + +133 +00:04:42,000 --> 00:04:46,000 +Now, you don't have to give just the software. + +134 +00:04:46,000 --> 00:04:50,000 +What you do is you give the entire OS, not the Windows. + +135 +00:04:50,000 --> 00:04:53,000 +The Ubuntu, the guest OS which you talk about. + +136 +00:04:53,000 --> 00:04:54,000 +Now, can you really copy guest OS? + +137 +00:04:54,000 --> 00:04:56,000 +Yes, you know, because it is not dependent + +138 +00:04:56,000 --> 00:04:58,000 +on a physical hardware, + +139 +00:04:58,000 --> 00:05:00,000 +it is dependent on the virtual hardware. + +140 +00:05:00,000 --> 00:05:02,000 +Now of course, internally it uses the physical hardware. + +141 +00:05:02,000 --> 00:05:03,000 +So let's say if you want to do + +142 +00:05:03,000 --> 00:05:07,000 +even add two numbers on a calculator, which is on Ubuntu, + +143 +00:05:07,000 --> 00:05:09,000 +your calc app talks to Ubuntu, + +144 +00:05:09,000 --> 00:05:11,000 +Ubuntu talks to your hypervisor, + +145 +00:05:11,000 --> 00:05:13,000 +hypervisor talks to your host OS, which is Windows, + +146 +00:05:13,000 --> 00:05:15,000 +and Windows talks to your hardware. + +147 +00:05:15,000 --> 00:05:18,000 +So ultimately, when you're performing any operation + +148 +00:05:18,000 --> 00:05:19,000 +that goes through your physical hardware, + +149 +00:05:19,000 --> 00:05:22,000 +but you can actually copy the guest OS, + +150 +00:05:22,000 --> 00:05:25,000 +because it is working on a software. + +151 +00:05:25,000 --> 00:05:26,000 +So in this hypervisors, + +152 +00:05:26,000 --> 00:05:29,000 +or all these virtual machine softwares, + +153 +00:05:29,000 --> 00:05:31,000 +they have a concept called creating an image. + +154 +00:05:31,000 --> 00:05:33,000 +So what you do is, you have a running OS, + +155 +00:05:33,000 --> 00:05:36,000 +the Ubuntu OS, you can simply save it, + +156 +00:05:36,000 --> 00:05:40,000 +literally save it, it'll create a image out of it. + +157 +00:05:40,000 --> 00:05:42,000 +Now, at this point it is difficult to understand image, + +158 +00:05:42,000 --> 00:05:44,000 +but if you try to understand this, + +159 +00:05:44,000 --> 00:05:47,000 +when you run your OS, it is running on your RAM, right? + +160 +00:05:47,000 --> 00:05:49,000 +So all the components are open. + +161 +00:05:49,000 --> 00:05:51,000 +When you want to give the OS to someone else, + +162 +00:05:51,000 --> 00:05:53,000 +you create a image out of it. + +163 +00:05:53,000 --> 00:05:55,000 +So you remove all the running part, + +164 +00:05:55,000 --> 00:05:58,000 +you simply save a copy it, this snapshot, + +165 +00:05:58,000 --> 00:06:02,000 +the running snapshot of that OS, which is a file. + +166 +00:06:02,000 --> 00:06:05,000 +And then you send a file to your, + +167 +00:06:05,000 --> 00:06:07,000 +any of your team, testing team. + +168 +00:06:07,000 --> 00:06:10,000 +or your colleague, or your ops team. + +169 +00:06:10,000 --> 00:06:12,000 +So make sure that they also have a hypervisor. + +170 +00:06:12,000 --> 00:06:15,000 +Now, these hypervisors are basically software like VMware. + +171 +00:06:15,000 --> 00:06:17,000 +There are multiple options. VirtualVM. + +172 +00:06:17,000 --> 00:06:19,000 +So there are so many softwares. + +173 +00:06:19,000 --> 00:06:20,000 +You can simply use those softwares. + +174 +00:06:20,000 --> 00:06:22,000 +So let's say if you're using VMware, + +175 +00:06:22,000 --> 00:06:24,000 +even they will be having VMware on their machine. + +176 +00:06:24,000 --> 00:06:27,000 +They can simply copy this image, + +177 +00:06:27,000 --> 00:06:28,000 +which you have given to them, + +178 +00:06:28,000 --> 00:06:31,000 +and they can run it on their machine. + +179 +00:06:31,000 --> 00:06:32,000 +Now, you don't have to send them + +180 +00:06:32,000 --> 00:06:34,000 +the project or the configuration + +181 +00:06:34,000 --> 00:06:37,000 +because they got the actual OS itself. + +182 +00:06:37,000 --> 00:06:39,000 +That's how you can solve the problem. + +183 +00:06:39,000 --> 00:06:40,000 +So problem solved, right? + +184 +00:06:40,000 --> 00:06:42,000 +Now, the thing is, when you give the OS + +185 +00:06:42,000 --> 00:06:44,000 +to your testing team, they will test it. + +186 +00:06:44,000 --> 00:06:46,000 +If something is wrong, they will give the project back + +187 +00:06:46,000 --> 00:06:48,000 +and then you can make some changes. + +188 +00:06:48,000 --> 00:06:51,000 +But what if everything is passing from testing team? + +189 +00:06:51,000 --> 00:06:54,000 +Now, you will send it to the production team. + +190 +00:06:54,000 --> 00:06:56,000 +Now, even they don't have to install any software, + +191 +00:06:56,000 --> 00:06:58,000 +they have a VM software, like any hypervisor, + +192 +00:06:58,000 --> 00:07:02,000 +and then you can simply run this on this server, + +193 +00:07:02,000 --> 00:07:05,000 +on the production server, and it will work. + +194 +00:07:05,000 --> 00:07:06,000 +If it works on your machine, + +195 +00:07:06,000 --> 00:07:08,000 +it'll also work on their machine. + +196 +00:07:08,000 --> 00:07:09,000 +No problem. + +197 +00:07:09,000 --> 00:07:10,000 +It's because now your software + +198 +00:07:10,000 --> 00:07:13,000 +is not dependent upon the underlying hardware, + +199 +00:07:13,000 --> 00:07:16,000 +it's dependent upon the underlying virtual hardware, + +200 +00:07:16,000 --> 00:07:17,000 +which is same everywhere. + +201 +00:07:17,000 --> 00:07:18,000 +Now, you might be thinking, okay, + +202 +00:07:18,000 --> 00:07:20,000 +so is it virtualization just came + +203 +00:07:20,000 --> 00:07:23,000 +to solve your problem as developer? + +204 +00:07:23,000 --> 00:07:24,000 +Say, not exactly. + +205 +00:07:24,000 --> 00:07:27,000 +Now if you go back to 1960s or 1970s, + +206 +00:07:27,000 --> 00:07:31,000 +if you want to make application to run on a server, + +207 +00:07:31,000 --> 00:07:32,000 +now servers are huge, right? + +208 +00:07:32,000 --> 00:07:35,000 +They have huge amount of CPU, they have huge amount of RAM. + +209 +00:07:35,000 --> 00:07:38,000 +Now if you run one particular application on that server, + +210 +00:07:38,000 --> 00:07:40,000 +don't you think we are wasting + +211 +00:07:40,000 --> 00:07:42,000 +so much of ram, so much of CPU? + +212 +00:07:42,000 --> 00:07:44,000 +So the solution here is, + +213 +00:07:44,000 --> 00:07:46,000 +what if you can install multiple apps + +214 +00:07:46,000 --> 00:07:48,000 +on one server, one physical server? + +215 +00:07:48,000 --> 00:07:49,000 +But the problem is, + +216 +00:07:49,000 --> 00:07:51,000 +these apps can actually talk to each other. + +217 +00:07:51,000 --> 00:07:52,000 +It's not safe, right? + +218 +00:07:52,000 --> 00:07:55,000 +Example, let's say you're building a banking application + +219 +00:07:55,000 --> 00:07:56,000 +which runs on one server. + +220 +00:07:56,000 --> 00:07:57,000 +On the other hand, + +221 +00:07:57,000 --> 00:07:59,000 +your friend is working on some other type of application, + +222 +00:07:59,000 --> 00:08:01,000 +let's say crypto exchange. + +223 +00:08:01,000 --> 00:08:02,000 +And then, the both the softwares + +224 +00:08:02,000 --> 00:08:04,000 +are installed on the same server, same OS. + +225 +00:08:04,000 --> 00:08:06,000 +Don't you think it'll be a risky part? + +226 +00:08:06,000 --> 00:08:08,000 +So you need isolation there. + +227 +00:08:08,000 --> 00:08:10,000 +So what we can do is, on normal servers, + +228 +00:08:10,000 --> 00:08:13,000 +like we have physical servers, which are huge, + +229 +00:08:13,000 --> 00:08:15,000 +you don't run one application, you run multiple. + +230 +00:08:15,000 --> 00:08:17,000 +But then if you don't have isolation + +231 +00:08:17,000 --> 00:08:18,000 +between application, it is risky. + +232 +00:08:18,000 --> 00:08:20,000 +So what you do is, on all the servers, + +233 +00:08:20,000 --> 00:08:23,000 +they install the hardware, the physical hardware, + +234 +00:08:23,000 --> 00:08:25,000 +then they install a host OS, + +235 +00:08:25,000 --> 00:08:27,000 +then they install a hypervisor. + +236 +00:08:27,000 --> 00:08:28,000 +And then, on this hypervisor, + +237 +00:08:28,000 --> 00:08:31,000 +you can run multiple virtual machines, not one. + +238 +00:08:31,000 --> 00:08:33,000 +Multiple guest OS, okay? + +239 +00:08:33,000 --> 00:08:36,000 +And in each guest OS you can install one app. + +240 +00:08:36,000 --> 00:08:38,000 +Now since the OS are different, + +241 +00:08:38,000 --> 00:08:41,000 +you got the isolation and your app are saved. + +242 +00:08:41,000 --> 00:08:44,000 +So that's how basically virtualization got famous. + +243 +00:08:44,000 --> 00:08:46,000 +But we can also use it for our purpose, right? + +244 +00:08:46,000 --> 00:08:48,000 +Where you don't have multiple guest OS, + +245 +00:08:48,000 --> 00:08:50,000 +of course you can use it for the production server, + +246 +00:08:50,000 --> 00:08:52,000 +but as a developer also you can build + +247 +00:08:52,000 --> 00:08:55,000 +your application on a guest OS, + +248 +00:08:55,000 --> 00:08:57,000 +and then you can give the guest OS + +249 +00:08:57,000 --> 00:08:59,000 +to the other teams, other members. + +250 +00:08:59,000 --> 00:09:01,000 +Do we have any problem here? + +251 +00:09:01,000 --> 00:09:02,000 +Yes, there's a little bit of problem. + +252 +00:09:02,000 --> 00:09:03,000 +Now, think about this. + +253 +00:09:03,000 --> 00:09:05,000 +To run your application, + +254 +00:09:05,000 --> 00:09:08,000 +you are installing the entire new OS, + +255 +00:09:08,000 --> 00:09:11,000 +which you're giving to your team, or some other team. + +256 +00:09:11,000 --> 00:09:13,000 +Now don't you think you are wasting + +257 +00:09:13,000 --> 00:09:17,000 +so much of RAM and CPU just to run that guest OS? + +258 +00:09:17,000 --> 00:09:20,000 +I mean, your little bit of small machine, + +259 +00:09:20,000 --> 00:09:21,000 +even if you're using I7 machines, + +260 +00:09:21,000 --> 00:09:25,000 +running two OS will be bit slow, right? + +261 +00:09:25,000 --> 00:09:27,000 +So virtualization is good, + +262 +00:09:27,000 --> 00:09:31,000 +but it consumes lot of memory and CPU power, + +263 +00:09:31,000 --> 00:09:32,000 +because you're not just giving your software, + +264 +00:09:32,000 --> 00:09:34,000 +you're giving the entire OS. + +265 +00:09:34,000 --> 00:09:35,000 +There's additionally one problem, + +266 +00:09:35,000 --> 00:09:37,000 +which is if you are using a host OS, + +267 +00:09:37,000 --> 00:09:39,000 +you are paid for that Windows, + +268 +00:09:39,000 --> 00:09:41,000 +and if you're using a guest OS, which is also a Windows, + +269 +00:09:41,000 --> 00:09:43,000 +then you're again paying for the license for it. + +270 +00:09:43,000 --> 00:09:45,000 +You need different license. + +271 +00:09:45,000 --> 00:09:46,000 +Plus, if you're using a proprietary + +272 +00:09:46,000 --> 00:09:48,000 +Linux-based distribution, + +273 +00:09:48,000 --> 00:09:50,000 +again you have to pay for this services and stuff. + +274 +00:09:50,000 --> 00:09:52,000 +So that's a big problem, right? + +275 +00:09:52,000 --> 00:09:54,000 +So, it's good, virtualization is good, + +276 +00:09:54,000 --> 00:09:55,000 +but there are some problems. + +277 +00:09:55,000 --> 00:09:57,000 +Do we have a solution for this? + +278 +00:09:57,000 --> 00:09:58,000 +And the answer is yes, we have a solution + +279 +00:09:58,000 --> 00:10:00,000 +called containerization. + +280 +00:10:00,000 --> 00:10:03,000 +And that will see in the next video. + diff --git a/24 - Docker/004 Solution With Containerization_en.srt b/24 - Docker/004 Solution With Containerization_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..2dc1e0b68939c7eda516d975463d23d07752d3af --- /dev/null +++ b/24 - Docker/004 Solution With Containerization_en.srt @@ -0,0 +1,640 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle tones) + +2 +00:00:02,000 --> 00:00:04,000 +-: Now let's try to solve the problem + +3 +00:00:04,000 --> 00:00:06,000 +with the help of containerization. + +4 +00:00:06,000 --> 00:00:08,000 +The thing is, if you talk about containerization, + +5 +00:00:08,000 --> 00:00:09,000 +what we have talked about is, + +6 +00:00:09,000 --> 00:00:10,000 +we have a hardware on top of that. + +7 +00:00:10,000 --> 00:00:13,000 +We have a host OS, then we have a hypervisor, + +8 +00:00:13,000 --> 00:00:15,000 +which is your virtual machine software. + +9 +00:00:15,000 --> 00:00:16,000 +And then on top of that you have a guest OS, + +10 +00:00:16,000 --> 00:00:19,000 +and on this guest OS, basically, + +11 +00:00:19,000 --> 00:00:21,000 +you have your application requirements. + +12 +00:00:21,000 --> 00:00:24,000 +Like, say you have web server, databases, your compilers, + +13 +00:00:24,000 --> 00:00:26,000 +everything installed on this OS. + +14 +00:00:26,000 --> 00:00:28,000 +And then when you want to give this to someone else, + +15 +00:00:28,000 --> 00:00:30,000 +you don't give the software, + +16 +00:00:30,000 --> 00:00:33,000 +you give the entire guest OS image, okay? + +17 +00:00:33,000 --> 00:00:35,000 +Now to solve this problem, + +18 +00:00:35,000 --> 00:00:37,000 +because when you say the entire guest OS, + +19 +00:00:37,000 --> 00:00:38,000 +it's heavy right, to run it? + +20 +00:00:38,000 --> 00:00:39,000 +Now the additional problem with the guest OS is, + +21 +00:00:39,000 --> 00:00:42,000 +what if you have multiple apps to run? + +22 +00:00:42,000 --> 00:00:44,000 +So you have to install multiple guest OS, right? + +23 +00:00:44,000 --> 00:00:47,000 +So on the hypervisor you're not running one guest OS, + +24 +00:00:47,000 --> 00:00:48,000 +but multiple. + +25 +00:00:48,000 --> 00:00:51,000 +And don't you think there are multiple guest OS, + +26 +00:00:51,000 --> 00:00:53,000 +which will be very heavy, let's say five to six guest OS + +27 +00:00:53,000 --> 00:00:55,000 +or five to six applications. + +28 +00:00:55,000 --> 00:00:57,000 +Now to solve this problem, we can use containers. + +29 +00:00:57,000 --> 00:00:59,000 +Now, if you talk about the shipping industry, again, + +30 +00:00:59,000 --> 00:01:00,000 +I'm not an expert in shipping industry, + +31 +00:01:00,000 --> 00:01:03,000 +but let's say if you want to move some things + +32 +00:01:03,000 --> 00:01:06,000 +from one location to other location through a sea route. + +33 +00:01:06,000 --> 00:01:09,000 +Now, of course you can just take this item, + +34 +00:01:09,000 --> 00:01:11,000 +pack it in a box and then send it to other place. + +35 +00:01:11,000 --> 00:01:14,000 +Again in the ship, when you want to carry it, + +36 +00:01:14,000 --> 00:01:14,000 +you want to ship it. + +37 +00:01:14,000 --> 00:01:16,000 +I know it's a very complex English here, + +38 +00:01:16,000 --> 00:01:19,000 +which is shipping the ships, but the point is, + +39 +00:01:19,000 --> 00:01:21,000 +when you want to again ship it on the ship, + +40 +00:01:21,000 --> 00:01:24,000 +you have to box it again so that it'll waterproof so + +41 +00:01:24,000 --> 00:01:25,000 +that you know, no water will get in + +42 +00:01:25,000 --> 00:01:28,000 +and then you will ship it to the other port, + +43 +00:01:28,000 --> 00:01:30,000 +the destination port and then from there on the trucks, + +44 +00:01:30,000 --> 00:01:32,000 +you will load it again and then you will ship it. + +45 +00:01:32,000 --> 00:01:35,000 +The easier way would be, what if you can use containers? + +46 +00:01:35,000 --> 00:01:37,000 +Now you have seen this containers a lot, right? + +47 +00:01:37,000 --> 00:01:40,000 +So in fact, in one of my first project in a company, + +48 +00:01:40,000 --> 00:01:41,000 +the client was a shipping company, + +49 +00:01:41,000 --> 00:01:45,000 +so I don't want to name it, so what they do is, + +50 +00:01:45,000 --> 00:01:48,000 +from the source location, when they want to pack things, + +51 +00:01:48,000 --> 00:01:51,000 +they directly put the things inside a container. + +52 +00:01:51,000 --> 00:01:52,000 +So let's say they have 10 boxes. + +53 +00:01:52,000 --> 00:01:54,000 +All these 10 boxes goes inside the container. + +54 +00:01:54,000 --> 00:01:57,000 +Now this exact container, the entire container, + +55 +00:01:57,000 --> 00:02:01,000 +goes on a truck, reaches to the source port, + +56 +00:02:01,000 --> 00:02:05,000 +and then from this port they will load it to the ship + +57 +00:02:05,000 --> 00:02:08,000 +and then they will dock it to a ship and then from there, + +58 +00:02:08,000 --> 00:02:10,000 +the ship will carry the same container, okay? + +59 +00:02:10,000 --> 00:02:12,000 +There's no unboxing and boxing happening here. + +60 +00:02:12,000 --> 00:02:14,000 +The same container goes to the destination port, + +61 +00:02:14,000 --> 00:02:17,000 +and then from there, the same container goes + +62 +00:02:17,000 --> 00:02:21,000 +on to undock and then it'll go to the truck + +63 +00:02:21,000 --> 00:02:23,000 +and this truck goes to the destination. + +64 +00:02:23,000 --> 00:02:25,000 +Now in the entire process, the things are moving + +65 +00:02:25,000 --> 00:02:27,000 +inside a container. + +66 +00:02:27,000 --> 00:02:28,000 +That is much easier, right? + +67 +00:02:28,000 --> 00:02:29,000 +Now that's where this concept + +68 +00:02:29,000 --> 00:02:31,000 +of containers came into picture. + +69 +00:02:31,000 --> 00:02:32,000 +Now can we do that in software? + +70 +00:02:32,000 --> 00:02:33,000 +Now think about this. + +71 +00:02:33,000 --> 00:02:35,000 +What if in the virtualization itself, + +72 +00:02:35,000 --> 00:02:37,000 +or maybe let's remove virtualization, + +73 +00:02:37,000 --> 00:02:39,000 +you have your hardware, physical hardware, + +74 +00:02:39,000 --> 00:02:41,000 +and then you have your host OS. + +75 +00:02:41,000 --> 00:02:45,000 +On top of it, you don't have to install any virtual machine, + +76 +00:02:45,000 --> 00:02:48,000 +but let's say you use some containerization softwares. + +77 +00:02:48,000 --> 00:02:51,000 +Now we have an option of like Docker, Podman, + +78 +00:02:51,000 --> 00:02:52,000 +there are multiple options. + +79 +00:02:52,000 --> 00:02:54,000 +So what you do is you install this software, + +80 +00:02:54,000 --> 00:02:55,000 +let's say Docker. + +81 +00:02:55,000 --> 00:02:57,000 +Docker is powerful enough to run the container, + +82 +00:02:57,000 --> 00:03:00,000 +not one container, but multiple containers. + +83 +00:03:00,000 --> 00:03:04,000 +So instead of running your software on the OS, + +84 +00:03:04,000 --> 00:03:06,000 +you are running your software inside the container. + +85 +00:03:06,000 --> 00:03:08,000 +Now from the developer's perspective, + +86 +00:03:08,000 --> 00:03:10,000 +when you are building a project, you will not be building + +87 +00:03:10,000 --> 00:03:12,000 +a project directly on your OS. + +88 +00:03:12,000 --> 00:03:14,000 +You will make sure that your app is running + +89 +00:03:14,000 --> 00:03:17,000 +on the container as in software, maybe let's say Docker. + +90 +00:03:17,000 --> 00:03:19,000 +Now in this container, which you're getting + +91 +00:03:19,000 --> 00:03:21,000 +on your machine, you have everything. + +92 +00:03:21,000 --> 00:03:25,000 +You have your compiler, web server, database connections + +93 +00:03:25,000 --> 00:03:28,000 +or database software, everything in this container. + +94 +00:03:28,000 --> 00:03:30,000 +Now, when you want to give this project + +95 +00:03:30,000 --> 00:03:33,000 +to your testing team, you will not give the project. + +96 +00:03:33,000 --> 00:03:35,000 +What you will give is the container itself. + +97 +00:03:35,000 --> 00:03:38,000 +So this container goes from here, goes to your testing team + +98 +00:03:38,000 --> 00:03:40,000 +and then from there it goes to the production. + +99 +00:03:40,000 --> 00:03:42,000 +You're not giving the project, you're giving the containers. + +100 +00:03:42,000 --> 00:03:46,000 +And the beauty is, now since you are doing this on Docker, + +101 +00:03:46,000 --> 00:03:50,000 +the same container will go from your machine to testing team + +102 +00:03:50,000 --> 00:03:53,000 +or testers machine and even they will have a Docker, + +103 +00:03:53,000 --> 00:03:54,000 +of course they have to install Docker, + +104 +00:03:54,000 --> 00:03:55,000 +and then you can run it. + +105 +00:03:55,000 --> 00:03:57,000 +Now installing Docker is not a tedious process. + +106 +00:03:57,000 --> 00:03:59,000 +You just have to install one software + +107 +00:03:59,000 --> 00:04:00,000 +and then the exact container, + +108 +00:04:00,000 --> 00:04:02,000 +the way it is working in your machine will work + +109 +00:04:02,000 --> 00:04:04,000 +on their machine, in the same way. + +110 +00:04:04,000 --> 00:04:07,000 +Its same goes for the production server and also cloud + +111 +00:04:07,000 --> 00:04:09,000 +because most of the cloud applications supports Docker + +112 +00:04:09,000 --> 00:04:10,000 +or cloud services. + +113 +00:04:10,000 --> 00:04:13,000 +So basically what I'm saying is, when you build a software, + +114 +00:04:13,000 --> 00:04:15,000 +build it in the container, the same container goes on there + +115 +00:04:15,000 --> 00:04:17,000 +and then from there and then it works everywhere. + +116 +00:04:17,000 --> 00:04:18,000 +Now what are the advantages? + +117 +00:04:18,000 --> 00:04:21,000 +First of all, containers is the isolation part. + +118 +00:04:21,000 --> 00:04:24,000 +So if you run one application, one container, + +119 +00:04:24,000 --> 00:04:25,000 +it'll not interact with the other containers, okay? + +120 +00:04:25,000 --> 00:04:28,000 +So you have different containers running on the same OS. + +121 +00:04:28,000 --> 00:04:30,000 +Now the advantage is, you only have one OS. + +122 +00:04:30,000 --> 00:04:32,000 +So the load on your machine will be less. + +123 +00:04:32,000 --> 00:04:34,000 +Additionally, when you talk about virtualization, + +124 +00:04:34,000 --> 00:04:35,000 +you have OS there, right? + +125 +00:04:35,000 --> 00:04:37,000 +Starting an OS takes time. + +126 +00:04:37,000 --> 00:04:40,000 +So let's say you are giving this OS open to image + +127 +00:04:40,000 --> 00:04:41,000 +to your testing team. + +128 +00:04:41,000 --> 00:04:45,000 +When they're run open from this image, it will take time. + +129 +00:04:45,000 --> 00:04:47,000 +But what about containers? + +130 +00:04:47,000 --> 00:04:48,000 +They're very fast to run, + +131 +00:04:48,000 --> 00:04:49,000 +because they're not running the entire OS, + +132 +00:04:49,000 --> 00:04:51,000 +you're just running a container. + +133 +00:04:51,000 --> 00:04:53,000 +And that's the benefit you get there. + +134 +00:04:53,000 --> 00:04:55,000 +And by by doing this, we are also making sure + +135 +00:04:55,000 --> 00:04:57,000 +that you have safety, which is security. + +136 +00:04:57,000 --> 00:04:59,000 +Also, what about portability? + +137 +00:04:59,000 --> 00:05:00,000 +Now, since we are getting containers, + +138 +00:05:00,000 --> 00:05:03,000 +it is much more portable, just like shipping industry. + +139 +00:05:03,000 --> 00:05:05,000 +So you can basically port this container + +140 +00:05:05,000 --> 00:05:06,000 +from one machine to other machine easily. + +141 +00:05:06,000 --> 00:05:08,000 +What about scalability? + +142 +00:05:08,000 --> 00:05:11,000 +Since you can run one container on one machine, + +143 +00:05:11,000 --> 00:05:14,000 +you can run the same container in multiple instances + +144 +00:05:14,000 --> 00:05:15,000 +if you want to scale it. + +145 +00:05:15,000 --> 00:05:18,000 +And by doing this, we are also achieving consistency, + +146 +00:05:18,000 --> 00:05:21,000 +which means if it works in your machine, + +147 +00:05:21,000 --> 00:05:24,000 +the same way it'll work on other machines as well. + +148 +00:05:24,000 --> 00:05:27,000 +And that's the beauty of containerization. + +149 +00:05:27,000 --> 00:05:28,000 +So basically if you want to understand containerization + +150 +00:05:28,000 --> 00:05:31,000 +what it means, it means simply, creating a package + +151 +00:05:31,000 --> 00:05:34,000 +of your software with all the configuration in a container + +152 +00:05:34,000 --> 00:05:37,000 +and ship it to the other teams, other machines. + +153 +00:05:37,000 --> 00:05:39,000 +Now question arise, how will you implement containers? + +154 +00:05:39,000 --> 00:05:42,000 +That will be done with the help of Docker? + +155 +00:05:42,000 --> 00:05:44,000 +So of course there are multiple applications + +156 +00:05:44,000 --> 00:05:46,000 +or multiple softwares which can do this, + +157 +00:05:46,000 --> 00:05:47,000 +but Docker is very famous, so we're gonna talk + +158 +00:05:47,000 --> 00:05:49,000 +about that in this course. + +159 +00:05:49,000 --> 00:05:50,000 +And also the course name itself is Docker, + +160 +00:05:50,000 --> 00:05:52,000 +so let's talk about that in the next video. + diff --git a/24 - Docker/005 What is Docker_en.srt b/24 - Docker/005 What is Docker_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..5dcb2929dd67d0bb015d1e3d8c4b246a13a03dbc --- /dev/null +++ b/24 - Docker/005 What is Docker_en.srt @@ -0,0 +1,524 @@ +1 +00:00:00,000 --> 00:00:02,000 +(bright music) + +2 +00:00:02,000 --> 00:00:04,000 +-: So what is Docker? + +3 +00:00:04,000 --> 00:00:06,000 +Now, Docker is a platform and a set of tools + +4 +00:00:06,000 --> 00:00:08,000 +basically to achieve containerization. + +5 +00:00:08,000 --> 00:00:10,000 +So we have talked about containers, right? + +6 +00:00:10,000 --> 00:00:11,000 +How it can be helpful, + +7 +00:00:11,000 --> 00:00:13,000 +and if you want to achieve that, we can use Docker. + +8 +00:00:13,000 --> 00:00:15,000 +Now, how Docker does this? + +9 +00:00:15,000 --> 00:00:18,000 +So Docker uses some components to achieve this, + +10 +00:00:18,000 --> 00:00:19,000 +there are different components available. + +11 +00:00:19,000 --> 00:00:21,000 +And of course, once we go ahead with the course, + +12 +00:00:21,000 --> 00:00:24,000 +we'll understand all these components in detail, + +13 +00:00:24,000 --> 00:00:26,000 +how it works, how do we create those, + +14 +00:00:26,000 --> 00:00:27,000 +how do we configure them? + +15 +00:00:27,000 --> 00:00:28,000 +But what are these components? + +16 +00:00:28,000 --> 00:00:31,000 +Now, the first component we have here is a Docker engine. + +17 +00:00:31,000 --> 00:00:33,000 +Now, this engine is basically responsible + +18 +00:00:33,000 --> 00:00:35,000 +to do the stuff which we're talking about, right? + +19 +00:00:35,000 --> 00:00:37,000 +Creating the containers, managing the containers. + +20 +00:00:37,000 --> 00:00:41,000 +And as a user, how will you interact with the containers? + +21 +00:00:41,000 --> 00:00:44,000 +So basically we have to use some command line prompt, + +22 +00:00:44,000 --> 00:00:45,000 +which you can pass some commands + +23 +00:00:45,000 --> 00:00:47,000 +to interact with the Docker. + +24 +00:00:47,000 --> 00:00:49,000 +Next, it provides you something called containers. + +25 +00:00:49,000 --> 00:00:51,000 +And obviously, right, when you talk about Docker, + +26 +00:00:51,000 --> 00:00:52,000 +it should have containers. + +27 +00:00:52,000 --> 00:00:55,000 +So Docker provides you that containers concept + +28 +00:00:55,000 --> 00:00:58,000 +so that it can run your softwares inside Docker. + +29 +00:00:58,000 --> 00:00:59,000 +Next we have an image. + +30 +00:00:59,000 --> 00:01:00,000 +Now, of course, right, + +31 +00:01:00,000 --> 00:01:02,000 +If you want to run a particular container. + +32 +00:01:02,000 --> 00:01:03,000 +So as I mentioned, as a developer, + +33 +00:01:03,000 --> 00:01:06,000 +when you do all the setup on your machine + +34 +00:01:06,000 --> 00:01:09,000 +and you have installed the compiler, the web server, + +35 +00:01:09,000 --> 00:01:11,000 +the database, all the configuration, + +36 +00:01:11,000 --> 00:01:14,000 +and now you want to give this container to some other team + +37 +00:01:14,000 --> 00:01:16,000 +or you want to install this on some other machine. + +38 +00:01:16,000 --> 00:01:17,000 +Now, what you can do is, + +39 +00:01:17,000 --> 00:01:20,000 +you cannot simply give a running container, of course. + +40 +00:01:20,000 --> 00:01:22,000 +So you have to give a image. + +41 +00:01:22,000 --> 00:01:24,000 +Now, this image, image is like a blueprint of it. + +42 +00:01:24,000 --> 00:01:27,000 +It's a lightweight file of your container, + +43 +00:01:27,000 --> 00:01:28,000 +running container, which you can simply save it, + +44 +00:01:28,000 --> 00:01:30,000 +it will create a image, + +45 +00:01:30,000 --> 00:01:32,000 +and then you can simply use the image + +46 +00:01:32,000 --> 00:01:34,000 +to run the containers on some other machine. + +47 +00:01:34,000 --> 00:01:37,000 +So let's say if you get this image on your machine + +48 +00:01:37,000 --> 00:01:39,000 +from someone else, you can simply run your Docker + +49 +00:01:39,000 --> 00:01:42,000 +and load this image, and it will run the container. + +50 +00:01:42,000 --> 00:01:44,000 +And with one image, you can run multiple containers, + +51 +00:01:44,000 --> 00:01:46,000 +not just one, if you want to scale it. + +52 +00:01:46,000 --> 00:01:48,000 +Now, our question guys, how will you create this image? + +53 +00:01:48,000 --> 00:01:51,000 +And that's where in Docker we have to create a Docker file. + +54 +00:01:51,000 --> 00:01:53,000 +How do we do it? That's different things. + +55 +00:01:53,000 --> 00:01:54,000 +We'll talk about that later. + +56 +00:01:54,000 --> 00:01:57,000 +But we have to create a Docker file where you mention, + +57 +00:01:57,000 --> 00:01:59,000 +hey, how do you want to create this image? + +58 +00:01:59,000 --> 00:02:01,000 +Docker is good, it's very smart, + +59 +00:02:01,000 --> 00:02:02,000 +but it's not that smart + +60 +00:02:02,000 --> 00:02:05,000 +it will create a Docker file for you automatically. + +61 +00:02:05,000 --> 00:02:07,000 +So you have to mention what are the things we needed, + +62 +00:02:07,000 --> 00:02:09,000 +what are the steps to run this project, + +63 +00:02:09,000 --> 00:02:11,000 +and what configuration you need. + +64 +00:02:11,000 --> 00:02:13,000 +So you can specify everything in a Docker file. + +65 +00:02:13,000 --> 00:02:15,000 +Next we have is Docker hub. + +66 +00:02:15,000 --> 00:02:17,000 +Now, from start, I'm saying that in this container + +67 +00:02:17,000 --> 00:02:20,000 +you can install the compilers, the installer, + +68 +00:02:20,000 --> 00:02:23,000 +all the stuff, or web servers. + +69 +00:02:23,000 --> 00:02:25,000 +But the question is, from where you will get it? + +70 +00:02:25,000 --> 00:02:28,000 +And what if you want to run the OS inside a container? + +71 +00:02:28,000 --> 00:02:30,000 +Yes, you can do that. How will you do it? + +72 +00:02:30,000 --> 00:02:32,000 +So maybe you for everything + +73 +00:02:32,000 --> 00:02:33,000 +you have to create your own image. + +74 +00:02:33,000 --> 00:02:34,000 +Not exactly. + +75 +00:02:34,000 --> 00:02:35,000 +So all these big companies, + +76 +00:02:35,000 --> 00:02:38,000 +what they have done is they have created a image + +77 +00:02:38,000 --> 00:02:39,000 +for their own softwares. + +78 +00:02:39,000 --> 00:02:42,000 +Let's say if you want a web server, let's say Tomcat, + +79 +00:02:42,000 --> 00:02:43,000 +you will get a Tomcat image. + +80 +00:02:43,000 --> 00:02:46,000 +If you want MongoDB, you will get a MongoDB image. + +81 +00:02:46,000 --> 00:02:48,000 +But question arise, from where you will find it? + +82 +00:02:48,000 --> 00:02:51,000 +Do we have to go to a individual website to download it? + +83 +00:02:51,000 --> 00:02:53,000 +Not exactly. See, we are working with Docker, right? + +84 +00:02:53,000 --> 00:02:55,000 +So I think it's Docker's responsibility + +85 +00:02:55,000 --> 00:02:56,000 +to have that repository + +86 +00:02:56,000 --> 00:02:57,000 +where you will find all the images. + +87 +00:02:57,000 --> 00:03:00,000 +And we do have it. There's something called Docker hub. + +88 +00:03:00,000 --> 00:03:03,000 +Imagine this as a cloud storage + +89 +00:03:03,000 --> 00:03:06,000 +or the cloud service where you can get all the images. + +90 +00:03:06,000 --> 00:03:08,000 +I mean, when I say all the images, most of the images. + +91 +00:03:08,000 --> 00:03:10,000 +So if you want a particular image, + +92 +00:03:10,000 --> 00:03:13,000 +just go there on Docker hub and just download the image. + +93 +00:03:13,000 --> 00:03:15,000 +Now, what about the networking? + +94 +00:03:15,000 --> 00:03:18,000 +Now, when you are building application on your own machine, + +95 +00:03:18,000 --> 00:03:20,000 +of course your own OS has a networking component, right? + +96 +00:03:20,000 --> 00:03:23,000 +So if you want to connect your two different services, + +97 +00:03:23,000 --> 00:03:25,000 +and if you want them to communicate, + +98 +00:03:25,000 --> 00:03:28,000 +you can use your network protocols and services, right? + +99 +00:03:28,000 --> 00:03:29,000 +What about containers? + +100 +00:03:29,000 --> 00:03:33,000 +Containers themself have their own networking components. + +101 +00:03:33,000 --> 00:03:35,000 +So by default, everything works inside the container. + +102 +00:03:35,000 --> 00:03:37,000 +But what if you want to connect + +103 +00:03:37,000 --> 00:03:38,000 +your application which is running + +104 +00:03:38,000 --> 00:03:40,000 +or some application running on your local machine? + +105 +00:03:40,000 --> 00:03:42,000 +Let's say your Chrome, to the container. + +106 +00:03:42,000 --> 00:03:45,000 +And that's why you have to expose some network ports + +107 +00:03:45,000 --> 00:03:46,000 +from the container. + +108 +00:03:46,000 --> 00:03:48,000 +So it provides you a network component as well. + +109 +00:03:48,000 --> 00:03:50,000 +Apart from this, what about the storage? + +110 +00:03:50,000 --> 00:03:53,000 +So Docker provides you something called volumes. + +111 +00:03:53,000 --> 00:03:55,000 +If you want to store data permanently, + +112 +00:03:55,000 --> 00:03:57,000 +you can use the volumes of Docker. + +113 +00:03:57,000 --> 00:03:59,000 +Now, what if you are building an application + +114 +00:03:59,000 --> 00:04:01,000 +which will need multiple containers? + +115 +00:04:01,000 --> 00:04:04,000 +So, of course, you can build application one container, + +116 +00:04:04,000 --> 00:04:06,000 +but sometimes you need multiple containers. + +117 +00:04:06,000 --> 00:04:07,000 +Now, how will you do it? + +118 +00:04:07,000 --> 00:04:09,000 +Now, that's where Docker compose comes in the picture. + +119 +00:04:09,000 --> 00:04:11,000 +So again, how do we compose it? + +120 +00:04:11,000 --> 00:04:13,000 +That we'll see later, but we have this component. + +121 +00:04:13,000 --> 00:04:16,000 +So all these components makes Docker, Docker. + +122 +00:04:16,000 --> 00:04:18,000 +So what is Docker? Docker is basically a platform. + +123 +00:04:18,000 --> 00:04:21,000 +And the set of tools which we have talked about + +124 +00:04:21,000 --> 00:04:24,000 +to run the containers so that it will be portable, + +125 +00:04:24,000 --> 00:04:27,000 +it will be secure, and it will be scalable. + +126 +00:04:27,000 --> 00:04:29,000 +Now question, guys, how can we get started? + +127 +00:04:29,000 --> 00:04:31,000 +So that we'll see in the next video + +128 +00:04:31,000 --> 00:04:32,000 +where we'll do the setup. + +129 +00:04:32,000 --> 00:04:34,000 +And we also see how do you run a particular container + +130 +00:04:34,000 --> 00:04:35,000 +in the upcoming videos. + +131 +00:04:35,000 --> 00:04:36,000 +Bye bye. + diff --git a/24 - Docker/006 Docker Setup_en.srt b/24 - Docker/006 Docker Setup_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..801ada7fdc6c9f9d82cc0138c5e99d72957cf07f --- /dev/null +++ b/24 - Docker/006 Docker Setup_en.srt @@ -0,0 +1,776 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music chiming) + +2 +00:00:02,000 --> 00:00:05,000 +-: In this video, let's try to set up Docker. + +3 +00:00:05,000 --> 00:00:07,000 +Now in this machine, I don't have Docker. + +4 +00:00:07,000 --> 00:00:08,000 +Now how do I verify that? + +5 +00:00:08,000 --> 00:00:11,000 +So what you do is you use your command line. + +6 +00:00:11,000 --> 00:00:12,000 +So basically you can also check from program files, + +7 +00:00:12,000 --> 00:00:15,000 +but let's use the command lines + +8 +00:00:15,000 --> 00:00:17,000 +because that's what we're going to use continuously. + +9 +00:00:17,000 --> 00:00:19,000 +So to check, you can simply enter + +10 +00:00:19,000 --> 00:00:22,000 +this command called docker, and then version. + +11 +00:00:22,000 --> 00:00:25,000 +The moment you do this, when you say enter, + +12 +00:00:25,000 --> 00:00:27,000 +it will give you an error by saying, + +13 +00:00:27,000 --> 00:00:28,000 +"'docker' is not recognized + +14 +00:00:28,000 --> 00:00:31,000 +"as internal or external command." + +15 +00:00:31,000 --> 00:00:33,000 +So that means Docker is not there. + +16 +00:00:33,000 --> 00:00:36,000 +So what we have to do is we have to install Docker. + +17 +00:00:36,000 --> 00:00:38,000 +The way you can do that is very simple, + +18 +00:00:38,000 --> 00:00:40,000 +download the Docker and install it. + +19 +00:00:40,000 --> 00:00:42,000 +I mean, of course, that's the obvious part, right? + +20 +00:00:42,000 --> 00:00:43,000 +But how you get it? + +21 +00:00:43,000 --> 00:00:45,000 +It's so simple, you can just go to Google + +22 +00:00:45,000 --> 00:00:49,000 +and search for Docker download. + +23 +00:00:49,000 --> 00:00:50,000 +It will take you to the official website, + +24 +00:00:50,000 --> 00:00:53,000 +in fact, you can just click on the first link, + +25 +00:00:53,000 --> 00:00:56,000 +and from here you can install Docker. + +26 +00:00:56,000 --> 00:00:59,000 +Now if you can see there's a word which is Docker Desktop. + +27 +00:00:59,000 --> 00:01:01,000 +Now what exactly Docker Desktop is? + +28 +00:01:01,000 --> 00:01:03,000 +The thing is we, when you talk about Docker, + +29 +00:01:03,000 --> 00:01:06,000 +it started with Docker on Linux, right? + +30 +00:01:06,000 --> 00:01:08,000 +So we have talked about the Linux containers, right? + +31 +00:01:08,000 --> 00:01:11,000 +So basically, we had Linux containers and somewhere Docker, + +32 +00:01:11,000 --> 00:01:13,000 +the idea of Docker started with that. + +33 +00:01:13,000 --> 00:01:15,000 +So initially, Docker used to run on Linux, + +34 +00:01:15,000 --> 00:01:16,000 +but then we thought, "Hey, you know, + +35 +00:01:16,000 --> 00:01:17,000 +"we have multiple OS," + +36 +00:01:17,000 --> 00:01:20,000 +not me, but then the Docker team. + +37 +00:01:20,000 --> 00:01:21,000 +They thought, "We have multiple OS, right? + +38 +00:01:21,000 --> 00:01:23,000 +"We got Windows, Mac, + +39 +00:01:23,000 --> 00:01:26,000 +"so how do I do that on this machine?" + +40 +00:01:26,000 --> 00:01:28,000 +So before, what they used to do is they used + +41 +00:01:28,000 --> 00:01:30,000 +to install Linux on Windows + +42 +00:01:30,000 --> 00:01:32,000 +because there was a support + +43 +00:01:32,000 --> 00:01:34,000 +of Linux on Windows, which is WSL, + +44 +00:01:34,000 --> 00:01:36,000 +and then you can install in Windows. + +45 +00:01:36,000 --> 00:01:39,000 +So we have a layer in between for Linux + +46 +00:01:39,000 --> 00:01:40,000 +and it works on Mac as well now. + +47 +00:01:40,000 --> 00:01:43,000 +And to support Docker on this to OS, + +48 +00:01:43,000 --> 00:01:45,000 +we got Docker Desktop. + +49 +00:01:45,000 --> 00:01:46,000 +So the way you can get that is very simple. + +50 +00:01:46,000 --> 00:01:47,000 +When you go to this website, + +51 +00:01:47,000 --> 00:01:49,000 +you can see we have multiple options here. + +52 +00:01:49,000 --> 00:01:52,000 +So if I go here, we have an option of Windows. + +53 +00:01:52,000 --> 00:01:54,000 +So this is a Windows machine, I can use this, + +54 +00:01:54,000 --> 00:01:57,000 +or you can use it for Mac, for Intel chip or Apple chip, + +55 +00:01:57,000 --> 00:02:01,000 +depend upon which laptop you have or which machine you have. + +56 +00:02:01,000 --> 00:02:03,000 +Otherwise you can go for Linux. + +57 +00:02:03,000 --> 00:02:05,000 +Let me show you how it looks for Linux. + +58 +00:02:05,000 --> 00:02:06,000 +For Linux, you have to enter command. + +59 +00:02:06,000 --> 00:02:08,000 +And people who use Linux, + +60 +00:02:08,000 --> 00:02:09,000 +they know how to run commands, right? + +61 +00:02:09,000 --> 00:02:10,000 +So you can just follow the steps + +62 +00:02:10,000 --> 00:02:13,000 +and you should be able to install Linux, it's very easy. + +63 +00:02:13,000 --> 00:02:14,000 +So just follow the steps. + +64 +00:02:14,000 --> 00:02:16,000 +Now since we are not into Linux, + +65 +00:02:16,000 --> 00:02:18,000 +we are using this on Windows, + +66 +00:02:18,000 --> 00:02:21,000 +and same steps applies for Mac as well, + +67 +00:02:21,000 --> 00:02:22,000 +you can see we have an option of download here. + +68 +00:02:22,000 --> 00:02:24,000 +So if I click on this, + +69 +00:02:24,000 --> 00:02:25,000 +now this is not the first time + +70 +00:02:25,000 --> 00:02:27,000 +I'm installing Docker on this machine, + +71 +00:02:27,000 --> 00:02:28,000 +so I should have this setup already. + +72 +00:02:28,000 --> 00:02:31,000 +So you can see I already have a Docker setup. + +73 +00:02:31,000 --> 00:02:32,000 +So I will just cancel this. + +74 +00:02:32,000 --> 00:02:34,000 +In fact, I downloaded it in the morning itself. + +75 +00:02:34,000 --> 00:02:37,000 +So what I will do is I will just open this. + +76 +00:02:37,000 --> 00:02:39,000 +So this is how this setup looks like. + +77 +00:02:39,000 --> 00:02:41,000 +So when I double click this, + +78 +00:02:41,000 --> 00:02:42,000 +the steps are straightforward, okay? + +79 +00:02:42,000 --> 00:02:45,000 +So it will say, "Do you want to install Docker?" + +80 +00:02:45,000 --> 00:02:47,000 +I would say, of course, that's what I'm doing. + +81 +00:02:47,000 --> 00:02:50,000 +Click on Yes, and it should take some time to download + +82 +00:02:50,000 --> 00:02:52,000 +because it's a heavy software. + +83 +00:02:52,000 --> 00:02:54,000 +Now depend upon how much RAM you have in your machine, + +84 +00:02:54,000 --> 00:02:57,000 +because at this point if I open Task Manager, + +85 +00:02:59,000 --> 00:03:01,000 +you can see it is consuming already 73% + +86 +00:03:01,000 --> 00:03:04,000 +because of the recording software which I'm using. + +87 +00:03:04,000 --> 00:03:06,000 +But if I go back here, + +88 +00:03:08,000 --> 00:03:11,000 +okay, yeah, so you can see it is installing. + +89 +00:03:11,000 --> 00:03:13,000 +But after installing, it will take lot of memory + +90 +00:03:13,000 --> 00:03:16,000 +because Docker is heavy software. + +91 +00:03:16,000 --> 00:03:18,000 +Plus the moment you start running your containers, + +92 +00:03:18,000 --> 00:03:20,000 +it will consume a lot of memory. + +93 +00:03:20,000 --> 00:03:24,000 +But what if you don't want to install on your machine? + +94 +00:03:24,000 --> 00:03:25,000 +You can still do that, okay? + +95 +00:03:25,000 --> 00:03:27,000 +So if you don't want to run it on your machine, + +96 +00:03:27,000 --> 00:03:30,000 +there is something called Labs, Docker Labs. + +97 +00:03:30,000 --> 00:03:33,000 +Do I have the option on this? + +98 +00:03:33,000 --> 00:03:34,000 +Let me check. + +99 +00:03:34,000 --> 00:03:38,000 +So they used to have this on the page itself, + +100 +00:03:38,000 --> 00:03:41,000 +but now I don't see that in the page. + +101 +00:03:41,000 --> 00:03:43,000 +So what we can also do is search for + +102 +00:03:45,000 --> 00:03:48,000 +Docker Labs and you can see the first option is that. + +103 +00:03:48,000 --> 00:03:51,000 +So it says Play with Docker, I will just click on this. + +104 +00:03:51,000 --> 00:03:52,000 +So if you don't want to install, + +105 +00:03:52,000 --> 00:03:56,000 +you can directly access Docker from here, you can log in. + +106 +00:03:57,000 --> 00:04:00,000 +Already have an account on Docker, let me use it. + +107 +00:04:07,000 --> 00:04:09,000 +Okay, so I had account on Docker. + +108 +00:04:09,000 --> 00:04:11,000 +You can simply create the account that's free + +109 +00:04:11,000 --> 00:04:13,000 +and you can click on Start, and from this, + +110 +00:04:13,000 --> 00:04:15,000 +you can do whatever you wanna do. + +111 +00:04:15,000 --> 00:04:17,000 +So everything which we are doing, + +112 +00:04:17,000 --> 00:04:18,000 +I mean, most of it which we are doing + +113 +00:04:18,000 --> 00:04:22,000 +on the local machine can run on the Labs as well. + +114 +00:04:22,000 --> 00:04:26,000 +The only thing is I'm not using Labs for this recording is + +115 +00:04:26,000 --> 00:04:28,000 +because one session can only be for four hours. + +116 +00:04:28,000 --> 00:04:30,000 +And of course, I will not be making + +117 +00:04:30,000 --> 00:04:32,000 +the entire course in one go. + +118 +00:04:32,000 --> 00:04:33,000 +Again, I have to do the same steps again + +119 +00:04:33,000 --> 00:04:35,000 +and again for different videos. + +120 +00:04:35,000 --> 00:04:37,000 +But if you wanna try, you can try it out. + +121 +00:04:37,000 --> 00:04:41,000 +So example, one of the command for Docker is, + +122 +00:04:41,000 --> 00:04:44,000 +I want to check how many Docker images I have already. + +123 +00:04:44,000 --> 00:04:45,000 +So when I do that, it says 'empty'. + +124 +00:04:45,000 --> 00:04:47,000 +So yes, you can do the same thing + +125 +00:04:47,000 --> 00:04:49,000 +which you are doing on the local machine. + +126 +00:04:49,000 --> 00:04:51,000 +But let's focus on the local part. + +127 +00:04:51,000 --> 00:04:54,000 +So you can see it says Docker is installed properly. + +128 +00:04:54,000 --> 00:04:57,000 +I will click on close, and I will open Docker now. + +129 +00:04:57,000 --> 00:05:02,000 +So I will say Docker, and you can see we got Docker Desktop, + +130 +00:05:02,000 --> 00:05:04,000 +and look at the memory consumption. + +131 +00:05:04,000 --> 00:05:06,000 +The only thing I'm scared about using Docker + +132 +00:05:06,000 --> 00:05:08,000 +on the local machine is because + +133 +00:05:08,000 --> 00:05:10,000 +of the amount of memory it takes. + +134 +00:05:10,000 --> 00:05:12,000 +So I feel 16 GB RAM is minimum + +135 +00:05:12,000 --> 00:05:14,000 +for Windows if you want to run Docker. + +136 +00:05:14,000 --> 00:05:17,000 +So except, I mean of course, it will work on 8 GB as well, + +137 +00:05:17,000 --> 00:05:19,000 +but it will lag somewhere. + +138 +00:05:19,000 --> 00:05:22,000 +Okay, so you can see we got Docker Desktop. + +139 +00:05:22,000 --> 00:05:24,000 +I would encourage you to log in + +140 +00:05:24,000 --> 00:05:25,000 +so that you will have the same setting + +141 +00:05:25,000 --> 00:05:26,000 +on different machines if you are using. + +142 +00:05:26,000 --> 00:05:28,000 +At this point, I will simply say continue + +143 +00:05:28,000 --> 00:05:29,000 +without signing in, + +144 +00:05:29,000 --> 00:05:32,000 +and it is asking for rules, I will skip this. + +145 +00:05:32,000 --> 00:05:35,000 +And yes, we got our Docker up and running. + +146 +00:05:35,000 --> 00:05:37,000 +So there's something called Docker Engine. + +147 +00:05:37,000 --> 00:05:38,000 +We'll talk about Docker Engine later. + +148 +00:05:38,000 --> 00:05:40,000 +But these are the options. + +149 +00:05:40,000 --> 00:05:41,000 +Remember the theory we talked about, + +150 +00:05:41,000 --> 00:05:43,000 +Docker images, Docker containers. + +151 +00:05:43,000 --> 00:05:46,000 +So at this point, you will see all the containers here, + +152 +00:05:46,000 --> 00:05:50,000 +images, you will see all the downloaded images you have, + +153 +00:05:50,000 --> 00:05:51,000 +volumes for the storage. + +154 +00:05:51,000 --> 00:05:53,000 +If I go back to the images, + +155 +00:05:53,000 --> 00:05:55,000 +you can see I can search + +156 +00:05:55,000 --> 00:05:57,000 +for a particular image online. + +157 +00:05:57,000 --> 00:06:00,000 +So let's search an image. + +158 +00:06:00,000 --> 00:06:03,000 +What I will do is I will search for Linux + +159 +00:06:03,000 --> 00:06:06,000 +and you can see we have an option of Alpine. + +160 +00:06:06,000 --> 00:06:08,000 +I will get Ubuntu, so I will click on Pull, + +161 +00:06:08,000 --> 00:06:11,000 +and now it will download this image. + +162 +00:06:11,000 --> 00:06:16,000 +So we can also use this command here, which is docker pull, + +163 +00:06:16,000 --> 00:06:18,000 +and you can see it says, "Download the newer image." + +164 +00:06:18,000 --> 00:06:21,000 +And now I can just run this, so Run, + +165 +00:06:21,000 --> 00:06:23,000 +and it should be running in the container. + +166 +00:06:23,000 --> 00:06:25,000 +So if I go back to container, you can see it is running now. + +167 +00:06:25,000 --> 00:06:28,000 +And the image, we already have an image now. + +168 +00:06:28,000 --> 00:06:29,000 +So basically, that's how you can get the image + +169 +00:06:29,000 --> 00:06:30,000 +and make it run. + +170 +00:06:30,000 --> 00:06:32,000 +So when you say 'image', you can see + +171 +00:06:32,000 --> 00:06:35,000 +it's very lightweight, it's only 77 MB. + +172 +00:06:35,000 --> 00:06:37,000 +But if I say 'container', + +173 +00:06:37,000 --> 00:06:40,000 +it will consume a lot of memory, okay? + +174 +00:06:40,000 --> 00:06:42,000 +So it's not running, so we'll see + +175 +00:06:42,000 --> 00:06:43,000 +how to run a container later. + +176 +00:06:43,000 --> 00:06:48,000 +But you can see we got the Docker setup done + +177 +00:06:48,000 --> 00:06:51,000 +and we also able to get the image, okay? + +178 +00:06:51,000 --> 00:06:53,000 +So that's how basically you do the setup for the Windows. + +179 +00:06:53,000 --> 00:06:55,000 +The setup is same for the Mac as well + +180 +00:06:55,000 --> 00:06:56,000 +and you can simply do the same step. + +181 +00:06:56,000 --> 00:06:58,000 +And now if I go back to my command line, + +182 +00:06:58,000 --> 00:07:01,000 +let's hit the same one, it is not working. + +183 +00:07:01,000 --> 00:07:03,000 +So let's open our CMD once again. + +184 +00:07:03,000 --> 00:07:04,000 +So sometime you have to, + +185 +00:07:04,000 --> 00:07:06,000 +most of the time you have to restart your CMD + +186 +00:07:06,000 --> 00:07:07,000 +if you install something new. + +187 +00:07:07,000 --> 00:07:09,000 +So if I say Docker Version, + +188 +00:07:09,000 --> 00:07:13,000 +you can see it says Docker version 24.0.4 or six. + +189 +00:07:13,000 --> 00:07:16,000 +So we got our Docker installed on the machine. + +190 +00:07:16,000 --> 00:07:18,000 +So that's it from this video where we were able + +191 +00:07:18,000 --> 00:07:19,000 +to get Docker on the machine. + +192 +00:07:19,000 --> 00:07:21,000 +And in the upcoming videos we'll see + +193 +00:07:21,000 --> 00:07:23,000 +how do you use Docker to get the images, + +194 +00:07:23,000 --> 00:07:25,000 +run the containers, and many more. + diff --git a/24 - Docker/007 Running First Container_en.srt b/24 - Docker/007 Running First Container_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..aedc5b1a44bc0bd2e35e02613000b710d3ae246c --- /dev/null +++ b/24 - Docker/007 Running First Container_en.srt @@ -0,0 +1,1032 @@ +1 +00:00:00,000 --> 00:00:02,000 +(bright music) + +2 +00:00:02,000 --> 00:00:04,000 +Okay, so we are done with this setup + +3 +00:00:04,000 --> 00:00:07,000 +and now, it's time to explore the Docker Desktop + +4 +00:00:07,000 --> 00:00:08,000 +or basically Docker commands. + +5 +00:00:08,000 --> 00:00:10,000 +So what we'll do is, first of all, let me show you + +6 +00:00:10,000 --> 00:00:13,000 +how Docker desktop looks like and this time, I'm using Mac. + +7 +00:00:13,000 --> 00:00:16,000 +The steps are same for Windows and Mac, + +8 +00:00:16,000 --> 00:00:17,000 +so it doesn't matter which one you use, + +9 +00:00:17,000 --> 00:00:19,000 +you'll find the same steps. + +10 +00:00:19,000 --> 00:00:23,000 +Now I have installed Docker on Mac as well. + +11 +00:00:23,000 --> 00:00:24,000 +So there's the Mac screen + +12 +00:00:24,000 --> 00:00:26,000 +and if you can see, it looks similar. + +13 +00:00:26,000 --> 00:00:28,000 +So we have containers here. + +14 +00:00:28,000 --> 00:00:31,000 +So at this point, we are not running any container + +15 +00:00:31,000 --> 00:00:32,000 +and that's why you can say this is empty. + +16 +00:00:32,000 --> 00:00:35,000 +When you download the image, you will find the images here. + +17 +00:00:35,000 --> 00:00:37,000 +Now, basically we have talked about this. + +18 +00:00:37,000 --> 00:00:40,000 +Images are like blue blueprint, they're lightweight, + +19 +00:00:40,000 --> 00:00:43,000 +and when you download the image, you only get this set + +20 +00:00:43,000 --> 00:00:45,000 +of instructions which says, okay, if I want + +21 +00:00:45,000 --> 00:00:48,000 +to run this container, these are the things I need. + +22 +00:00:48,000 --> 00:00:51,000 +The actual working happens in the container, okay? + +23 +00:00:51,000 --> 00:00:53,000 +So container will be quite heavy compared to images + +24 +00:00:53,000 --> 00:00:56,000 +because images are just set of instructions and stuff. + +25 +00:00:56,000 --> 00:00:58,000 +Okay, so we got, we will get the containers here, + +26 +00:00:58,000 --> 00:00:59,000 +we'll get the images here. + +27 +00:00:59,000 --> 00:01:02,000 +If you are working with volumes, you will get it here. + +28 +00:01:02,000 --> 00:01:03,000 +And those are things. + +29 +00:01:03,000 --> 00:01:05,000 +And we'll explore all these things step by step. + +30 +00:01:05,000 --> 00:01:07,000 +So let's focus on images now. + +31 +00:01:07,000 --> 00:01:09,000 +So we want to get a image. + +32 +00:01:09,000 --> 00:01:12,000 +Now of course we can create our own image, + +33 +00:01:12,000 --> 00:01:14,000 +but let's get some images from the internet + +34 +00:01:14,000 --> 00:01:16,000 +or from the Docker hub. + +35 +00:01:16,000 --> 00:01:19,000 +Now you can do this in two ways. + +36 +00:01:19,000 --> 00:01:20,000 +Basically you can get the image by searching this. + +37 +00:01:20,000 --> 00:01:23,000 +So you can say search image to run, + +38 +00:01:23,000 --> 00:01:25,000 +and you can search for a particular image which you want. + +39 +00:01:25,000 --> 00:01:29,000 +Let's say I want mySQL, I will search for mySQL here. + +40 +00:01:29,000 --> 00:01:32,000 +And then you will see we have an optional mySQL here. + +41 +00:01:32,000 --> 00:01:34,000 +Now this is official one, + +42 +00:01:34,000 --> 00:01:36,000 +so you can say it says Docker official image. + +43 +00:01:36,000 --> 00:01:40,000 +The other images, which we have example from circleci. + +44 +00:01:40,000 --> 00:01:42,000 +It's a verified one but not official. + +45 +00:01:42,000 --> 00:01:44,000 +And if you go down, there will be some + +46 +00:01:44,000 --> 00:01:45,000 +which are not even verified. + +47 +00:01:45,000 --> 00:01:47,000 +Example this one, this is sponsored OSS. + +48 +00:01:47,000 --> 00:01:51,000 +And if you, if I go down, so there list, which can find, + +49 +00:01:51,000 --> 00:01:52,000 +we also have mask here, + +50 +00:01:52,000 --> 00:01:55,000 +but it's not even verified, not even official. + +51 +00:01:55,000 --> 00:01:56,000 +So you've got the point, right? + +52 +00:01:56,000 --> 00:01:59,000 +So try to get things official which will make + +53 +00:02:00,000 --> 00:02:01,000 +which will always work. + +54 +00:02:01,000 --> 00:02:04,000 +Now in this, if you can see the number of downloads we have, + +55 +00:02:04,000 --> 00:02:07,000 +the number of stars it got, and the tags. + +56 +00:02:07,000 --> 00:02:09,000 +Now basically these are versions, right? + +57 +00:02:09,000 --> 00:02:11,000 +So example, if whenever you download any software, + +58 +00:02:11,000 --> 00:02:13,000 +you do get the versions of it, right? + +59 +00:02:13,000 --> 00:02:15,000 +So you can specify which version you want. + +60 +00:02:15,000 --> 00:02:19,000 +Now here, when you sell latest, it means the latest release. + +61 +00:02:19,000 --> 00:02:21,000 +When you get the next release, this one will get a number. + +62 +00:02:21,000 --> 00:02:24,000 +Okay, so now, we have an option here. + +63 +00:02:24,000 --> 00:02:26,000 +So if you can see we have two options. + +64 +00:02:26,000 --> 00:02:27,000 +We got pull and run. + +65 +00:02:27,000 --> 00:02:29,000 +So I can simply pull it from here. + +66 +00:02:29,000 --> 00:02:30,000 +Let me just try that first. + +67 +00:02:30,000 --> 00:02:33,000 +So when you say pull, it will take some time to pull + +68 +00:02:33,000 --> 00:02:35,000 +because mySQL is bit heavy compared + +69 +00:02:35,000 --> 00:02:37,000 +to the other images which we have. + +70 +00:02:37,000 --> 00:02:39,000 +So it is pulling the image. + +71 +00:02:39,000 --> 00:02:42,000 +And once the image pulling is done, + +72 +00:02:42,000 --> 00:02:44,000 +you can go back to your images. + +73 +00:02:44,000 --> 00:02:45,000 +This is where you will find it. + +74 +00:02:45,000 --> 00:02:47,000 +So I think it is still downloading. + +75 +00:02:47,000 --> 00:02:50,000 +And yeah, so we got our first image. + +76 +00:02:50,000 --> 00:02:53,000 +So you can see it says mySQL created three months back. + +77 +00:02:53,000 --> 00:02:54,000 +And this is a size. + +78 +00:02:54,000 --> 00:02:57,000 +And you can also run this from here. + +79 +00:02:57,000 --> 00:03:00,000 +I'm not sure if this will work directly without settings, + +80 +00:03:00,000 --> 00:03:02,000 +but yeah, it says it is working. + +81 +00:03:02,000 --> 00:03:04,000 +Let's see if I can see that in the container. + +82 +00:03:04,000 --> 00:03:05,000 +It ran and it exited, + +83 +00:03:05,000 --> 00:03:07,000 +but basically, it was running at one point. + +84 +00:03:07,000 --> 00:03:11,000 +So we got the images here and we got the containers here. + +85 +00:03:11,000 --> 00:03:13,000 +But at this point, I don't want this. + +86 +00:03:13,000 --> 00:03:15,000 +So let me try to delete the image first + +87 +00:03:15,000 --> 00:03:17,000 +and let's see what happens. + +88 +00:03:17,000 --> 00:03:20,000 +When you try to delete the image, let's say delete follower, + +89 +00:03:20,000 --> 00:03:22,000 +it is giving you an error. + +90 +00:03:22,000 --> 00:03:25,000 +It says image mySQL latest is in use. + +91 +00:03:25,000 --> 00:03:26,000 +Of course, right? + +92 +00:03:26,000 --> 00:03:27,000 +We are running a container. + +93 +00:03:27,000 --> 00:03:29,000 +So it says delete the container first + +94 +00:03:29,000 --> 00:03:30,000 +and then I will delete. + +95 +00:03:30,000 --> 00:03:31,000 +Okay, that makes sense + +96 +00:03:31,000 --> 00:03:33,000 +because if your container is running, + +97 +00:03:33,000 --> 00:03:34,000 +how you delete the image? + +98 +00:03:34,000 --> 00:03:37,000 +So what you do is first, you delete the container. + +99 +00:03:37,000 --> 00:03:38,000 +Now that's gone. + +100 +00:03:38,000 --> 00:03:39,000 +If I go back to images + +101 +00:03:39,000 --> 00:03:43,000 +and if I try to delete from here, I can do that, + +102 +00:03:43,000 --> 00:03:45,000 +delete forever and now it'll be deleted. + +103 +00:03:45,000 --> 00:03:47,000 +So again, you can see this is empty. + +104 +00:03:47,000 --> 00:03:49,000 +So this was one way, right? + +105 +00:03:49,000 --> 00:03:51,000 +But most of the time, we use commands to do this, + +106 +00:03:51,000 --> 00:03:54,000 +we use commands to get the image, + +107 +00:03:54,000 --> 00:03:57,000 +we use commands to run the container. + +108 +00:03:57,000 --> 00:04:00,000 +Now the way you can do that is you can open your terminal. + +109 +00:04:00,000 --> 00:04:03,000 +Now depend upon which OS you're working. + +110 +00:04:03,000 --> 00:04:06,000 +For Windows, it is CMD, and for Mac, it is terminal. + +111 +00:04:06,000 --> 00:04:07,000 +So you can see I have a terminal here + +112 +00:04:07,000 --> 00:04:09,000 +and now I can run the Docker commands. + +113 +00:04:09,000 --> 00:04:12,000 +Now first of all, we have to verify if I have docker here. + +114 +00:04:12,000 --> 00:04:14,000 +So I can say docker --version + +115 +00:04:14,000 --> 00:04:18,000 +and the version is 24.0.6, which is the right version. + +116 +00:04:18,000 --> 00:04:22,000 +And now I can basically run a container. + +117 +00:04:22,000 --> 00:04:24,000 +Now instead of going for MySQL, which is quite heavy, + +118 +00:04:24,000 --> 00:04:27,000 +I will go for some different example. + +119 +00:04:27,000 --> 00:04:29,000 +There is one more image which I will first search + +120 +00:04:30,000 --> 00:04:33,000 +and then we'll try to get that from there. + +121 +00:04:33,000 --> 00:04:35,000 +And the image is Hello World. + +122 +00:04:35,000 --> 00:04:38,000 +Again, this is official, so we can use this. + +123 +00:04:38,000 --> 00:04:40,000 +So we have Hello World, which is created by Docker. + +124 +00:04:40,000 --> 00:04:45,000 +And if I go inside this, this is the official image, + +125 +00:04:45,000 --> 00:04:46,000 +and if I scroll down. + +126 +00:04:49,000 --> 00:04:51,000 +So this is the output it'll give. + +127 +00:04:51,000 --> 00:04:55,000 +So you can see example output, this is what you will get + +128 +00:04:55,000 --> 00:04:56,000 +and how this image is created. + +129 +00:04:56,000 --> 00:04:59,000 +So basically, they have used, is it C programming? + +130 +00:04:59,000 --> 00:05:01,000 +I'm not sure, maybe. + +131 +00:05:01,000 --> 00:05:04,000 +Yeah, they have used C programming here + +132 +00:05:04,000 --> 00:05:07,000 +and yeah, that's how they're running the Hello World. + +133 +00:05:07,000 --> 00:05:09,000 +Now how do I get this image on my machine? + +134 +00:05:09,000 --> 00:05:10,000 +Because you can see the image + +135 +00:05:10,000 --> 00:05:12,000 +is only 13 kb virtually faster. + +136 +00:05:12,000 --> 00:05:15,000 +So of course, you can pull it from here, run it from here. + +137 +00:05:15,000 --> 00:05:16,000 +I don't wanna do that from there. + +138 +00:05:16,000 --> 00:05:20,000 +So what I will do is I will go back to my command line + +139 +00:05:20,000 --> 00:05:22,000 +and how do I get the image? + +140 +00:05:22,000 --> 00:05:25,000 +So you can simply say Docker and you can say run. + +141 +00:05:25,000 --> 00:05:27,000 +So this will directly run the image. + +142 +00:05:27,000 --> 00:05:29,000 +So I can say Docker run. + +143 +00:05:29,000 --> 00:05:33,000 +And what I'm going to run is Hello World. + +144 +00:05:33,000 --> 00:05:37,000 +And if I say enter, you can see the steps that is following. + +145 +00:05:37,000 --> 00:05:41,000 +So first, it says I'm not able to find the latest image. + +146 +00:05:41,000 --> 00:05:44,000 +So you can see it here. + +147 +00:05:44,000 --> 00:05:47,000 +So it says, unable to find the image hello-world:latest. + +148 +00:05:47,000 --> 00:05:48,000 +So this is a tag. + +149 +00:05:48,000 --> 00:05:51,000 +So with every image, you will also get a tag. + +150 +00:05:51,000 --> 00:05:54,000 +If you don't mention the tag, it'll go for a default one, + +151 +00:05:54,000 --> 00:05:56,000 +which is most of the time, which is latest. + +152 +00:05:56,000 --> 00:05:57,000 +But if you want a other version, + +153 +00:05:57,000 --> 00:05:59,000 +you can specify with the colon. + +154 +00:05:59,000 --> 00:06:01,000 +Now in this machine, I don't have the hello-world, + +155 +00:06:01,000 --> 00:06:02,000 +and that's what it says. + +156 +00:06:02,000 --> 00:06:05,000 +It says, "Unable to find the image locally." + +157 +00:06:05,000 --> 00:06:07,000 +Now when you are not able to find this locally, + +158 +00:06:07,000 --> 00:06:09,000 +what you do is you pull. + +159 +00:06:09,000 --> 00:06:10,000 +Now that's the step you have to follow. + +160 +00:06:10,000 --> 00:06:13,000 +So if you don't have the image, pull the image. + +161 +00:06:13,000 --> 00:06:14,000 +From where? + +162 +00:06:14,000 --> 00:06:15,000 +From the Docker Hub. + +163 +00:06:15,000 --> 00:06:18,000 +Okay, let me show you how Docker hub looks like first. + +164 +00:06:18,000 --> 00:06:20,000 +You can see I'm searching for Docker Hub. + +165 +00:06:20,000 --> 00:06:24,000 +When I go here, this is where you will get all the image. + +166 +00:06:24,000 --> 00:06:26,000 +Of course, I have to log in from my ID + +167 +00:06:26,000 --> 00:06:27,000 +or do we have an option of skipping? + +168 +00:06:27,000 --> 00:06:28,000 +I think you can even skip, + +169 +00:06:28,000 --> 00:06:30,000 +you can actually search for something here. + +170 +00:06:30,000 --> 00:06:34,000 +So these are the popular images, which we have Nginx, + +171 +00:06:34,000 --> 00:06:39,000 +we have MongoDB, Alpine Linux, Node.js, Redis. + +172 +00:06:39,000 --> 00:06:44,000 +We also have for Java, Ubuntu, and then a lot + +173 +00:06:44,000 --> 00:06:48,000 +of repositories available or images available. + +174 +00:06:48,000 --> 00:06:53,000 +So if I go here and search for, let's say hello world, + +175 +00:06:53,000 --> 00:06:54,000 +this is what I was also getting + +176 +00:06:54,000 --> 00:06:56,000 +in the Docker desktop, right? + +177 +00:06:56,000 --> 00:07:00,000 +So this is all the image which we have and you can get it. + +178 +00:07:00,000 --> 00:07:02,000 +So as I mentioned, we have Docker official images + +179 +00:07:02,000 --> 00:07:04,000 +verified and sponsored. + +180 +00:07:04,000 --> 00:07:07,000 +And if you see the use, a lot of people are learning Docker + +181 +00:07:07,000 --> 00:07:11,000 +by doing Hello World first because that's how you start. + +182 +00:07:11,000 --> 00:07:13,000 +Okay, so this is the actual remote repository + +183 +00:07:13,000 --> 00:07:17,000 +or remote place from where you are getting your images. + +184 +00:07:17,000 --> 00:07:19,000 +And the beauty is you can create your own images + +185 +00:07:19,000 --> 00:07:20,000 +and push it here, okay? + +186 +00:07:20,000 --> 00:07:22,000 +It's that simple. + +187 +00:07:22,000 --> 00:07:25,000 +Okay, let's go back there in our docker + +188 +00:07:25,000 --> 00:07:27,000 +or not here, on command line. + +189 +00:07:27,000 --> 00:07:28,000 +And let's follow more steps. + +190 +00:07:28,000 --> 00:07:30,000 +So it is pulling the image. + +191 +00:07:30,000 --> 00:07:34,000 +Now once the pulling is complete, you can say pull complete, + +192 +00:07:34,000 --> 00:07:37,000 +you will get a ID for base for your image. + +193 +00:07:38,000 --> 00:07:40,000 +I will show you the ID in sometime. + +194 +00:07:40,000 --> 00:07:43,000 +And it is printing some steps to follow. + +195 +00:07:43,000 --> 00:07:45,000 +And then you can also run. + +196 +00:07:45,000 --> 00:07:49,000 +If you want, you can also get Ubuntu in this machine. + +197 +00:07:49,000 --> 00:07:52,000 +But here, if you can see I got the image. + +198 +00:07:52,000 --> 00:07:54,000 +How do I verify if I got the image? + +199 +00:07:54,000 --> 00:07:55,000 +We have multiple ways. + +200 +00:07:55,000 --> 00:07:57,000 +We can use it command line + +201 +00:07:57,000 --> 00:08:00,000 +or we can use the Docker desktop. + +202 +00:08:00,000 --> 00:08:02,000 +So if I go back here + +203 +00:08:02,000 --> 00:08:04,000 +and if I say images, you can see we got Hello World. + +204 +00:08:04,000 --> 00:08:07,000 +It says in use because it is running. + +205 +00:08:07,000 --> 00:08:10,000 +And if I go to the container, + +206 +00:08:10,000 --> 00:08:13,000 +you can see we got Hello World, which has done its work. + +207 +00:08:13,000 --> 00:08:14,000 +And yeah. + +208 +00:08:14,000 --> 00:08:17,000 +And you can see there's some name here, + +209 +00:08:17,000 --> 00:08:18,000 +which is nice_borg. + +210 +00:08:18,000 --> 00:08:20,000 +Now basically every time you run a container, + +211 +00:08:20,000 --> 00:08:23,000 +it'll give a name to it, a random name. + +212 +00:08:23,000 --> 00:08:26,000 +Maybe they have used some algorithm to generate this name. + +213 +00:08:26,000 --> 00:08:30,000 +But when you create your own image, you can give the name + +214 +00:08:30,000 --> 00:08:31,000 +or when you run the containers, + +215 +00:08:31,000 --> 00:08:34,000 +you can also provide the name to it if you want. + +216 +00:08:34,000 --> 00:08:36,000 +Because that makes much more sense, right? + +217 +00:08:36,000 --> 00:08:37,000 +If you're running multiple containers, + +218 +00:08:37,000 --> 00:08:40,000 +how do you know which container you're looking at? + +219 +00:08:40,000 --> 00:08:42,000 +So having a proper name makes sense. + +220 +00:08:42,000 --> 00:08:46,000 +So this is how you can get the details + +221 +00:08:46,000 --> 00:08:49,000 +of which image you got, which containers are running. + +222 +00:08:49,000 --> 00:08:51,000 +But what if you want to get that from here? + +223 +00:08:51,000 --> 00:08:52,000 +From command line. + +224 +00:08:52,000 --> 00:08:55,000 +Now for that, you have to run some commands, okay? + +225 +00:08:55,000 --> 00:08:58,000 +Now if you want to know which containers are running, + +226 +00:08:58,000 --> 00:09:00,000 +we can say Docker ps, + +227 +00:09:00,000 --> 00:09:03,000 +and, okay, none of the containers are actually running now + +228 +00:09:03,000 --> 00:09:07,000 +because Hello World ran and then they have done it. + +229 +00:09:07,000 --> 00:09:09,000 +I mean, the work is done. + +230 +00:09:09,000 --> 00:09:12,000 +But still, if you want to see the containers which are run, + +231 +00:09:12,000 --> 00:09:14,000 +which have completed their work + +232 +00:09:14,000 --> 00:09:16,000 +and exited, you can -a. + +233 +00:09:17,000 --> 00:09:19,000 +-: a means show me all. + +234 +00:09:19,000 --> 00:09:21,000 +And when I say enter, + +235 +00:09:21,000 --> 00:09:24,000 +you can see this is the container which we were running. + +236 +00:09:24,000 --> 00:09:25,000 +So the name is this. + +237 +00:09:25,000 --> 00:09:26,000 +So this is a container ID. + +238 +00:09:26,000 --> 00:09:30,000 +So every time you run the container, you will get an ID. + +239 +00:09:30,000 --> 00:09:31,000 +You will get the name of the image. + +240 +00:09:31,000 --> 00:09:33,000 +That's what we are running. + +241 +00:09:33,000 --> 00:09:36,000 +The command you can use here is hello. + +242 +00:09:36,000 --> 00:09:40,000 +And created four minutes ago, exited also. + +243 +00:09:40,000 --> 00:09:42,000 +And then this is a name for the container. + +244 +00:09:42,000 --> 00:09:44,000 +And I mentioned before, you can change this name, + +245 +00:09:44,000 --> 00:09:46,000 +how we'll see that later. + +246 +00:09:46,000 --> 00:09:48,000 +But at this point we, we got some name. + +247 +00:09:48,000 --> 00:09:51,000 +Okay, what if I want to see the images I have? + +248 +00:09:51,000 --> 00:09:54,000 +So I can simply say docker images and enter. + +249 +00:09:54,000 --> 00:09:56,000 +And you can see this is the image I got + +250 +00:09:56,000 --> 00:09:59,000 +and every image will also have an ID. + +251 +00:09:59,000 --> 00:10:02,000 +Okay, so we got the image ID, we got the learning process + +252 +00:10:02,000 --> 00:10:03,000 +or the learning containers. + +253 +00:10:03,000 --> 00:10:05,000 +But if you want to see the stop containers, + +254 +00:10:05,000 --> 00:10:07,000 +we can say -a. + +255 +00:10:07,000 --> 00:10:10,000 +Now visualize how I delete this image, + +256 +00:10:10,000 --> 00:10:12,000 +or if I wanna delete the container, how would I do that? + +257 +00:10:12,000 --> 00:10:14,000 +Well, let's see that in the next video + +258 +00:10:14,000 --> 00:10:17,000 +where we will focus more commands and see how they work. +