Spring Security JWT: How to secure your Spring Boot REST APIs with JSON Web Tokens

  Рет қаралды 120,185

Dan Vega

Dan Vega

Жыл бұрын

In this tutorial, you are going to learn how to secure your Spring Boot REST APIs using JSON Web Tokens (JWT) with Spring Security.
🔗Resources & Links mentioned in this video:
Blog Post: www.danvega.dev/blog/2022/09/...
WebSercurityConfigurerAdapter: • Spring Security withou...
👋🏻Connect with me:
Website: www.danvega.dev
Twitter: / therealdanvega
Github: github.com/danvega
LinkedIn: / danvega
Newsletter: www.danvega/dev/newsletter
SUBSCRIBE TO MY CHANNEL: bit.ly/2re4GH0 ❤️

Пікірлер: 281
@fabricio.entringer
@fabricio.entringer Жыл бұрын
Hello @Dan, it's amazing! Great video. Please keep producing videos regarding Spring Security, I think it's a black hole in the Spring modules. A lot of specific concepts and it deserves good videos with good explanations like yours. Congratulation and thanks for sharing the content.
@intellopitt
@intellopitt Жыл бұрын
Amazing, how simple it is when explained by experts. Thanks for the great content. Well explained, with the right level of details to understand without getting overwhelmed. I still have to review the blog post if I am not missing any details. Looking forward for the next video :)
@voiceofeverything
@voiceofeverything Жыл бұрын
Great video. This is like the only one guide about this topic that is quite easy to follow and does not break your spirit (I have tried to follow like 2 different videos just to realize half way into 3 hour videos that implementation was changed/got deprecated and I wasted my time).
@ParnianAndIlian
@ParnianAndIlian Жыл бұрын
Hi Dan, This is the tutorial that was missing in 2022. Thanks a lot. I was struggling with those outdated tutorials and dependencies to make a working solution. this saved me lots of time. Looking forward to your next videos.
@kurzgefasst2816
@kurzgefasst2816 Жыл бұрын
Same, Docs mentioned some stuff that are new but honestly Spring Boot Docs is not for Beginner.
@bobgner
@bobgner Жыл бұрын
Thank you for making this tutorial. As you mentioned in the beginning there are so many more complicated ways of doing out there because the are not using what is built in to Spring Security. I unfortunately had used one of those more complicated ways so now I'm going to use what I learned in your tutorial to simplify my project code!
@DanVega
@DanVega Жыл бұрын
Thank you Bob. Glad I could help out.
@kozi-corner
@kozi-corner Жыл бұрын
Thank you so much..I was just working on a project and had a lot of difficulties understanding JWT, I opened youtube and I found your video. How lucky I am!
@DanVega
@DanVega Жыл бұрын
Thank you Abdelhamid. I hope this clears it up for you.
@SD-gw5vm
@SD-gw5vm Жыл бұрын
Thanks for sharing this. I used your example to solve a problem I was working on and it worked. You are a lifesaver
@franciskinyuru3459
@franciskinyuru3459 Жыл бұрын
Great video my start to spring security wouldn't have been great without this. A big salute.
@kaatlev
@kaatlev Жыл бұрын
These videos are so concise and easy to follow, appreciate you.
@DanVega
@DanVega Жыл бұрын
That makes me so happy. Thank you ☺️
@SchefenBaba
@SchefenBaba 6 ай бұрын
Thanks a lot for this tutorial. I have been stuck in other tutorials for hours.
@brethagen7776
@brethagen7776 Жыл бұрын
Straight to the point, no fluff. Looks looks like a bare-minimum implementation.
@jirivrba3800
@jirivrba3800 Жыл бұрын
Thanks Dan, really educative content that's very well and clearly presented. Exactly what I was looking for!
@DanVega
@DanVega Жыл бұрын
Thank you, glad I could help out.
@luisferdev
@luisferdev Жыл бұрын
thank you Dan, this video help me a lot to understand how to generate JWT in Spring. the only one site where I found the explanation with the new version of spring security and works. regards from Colombia
@DanVega
@DanVega Жыл бұрын
Thank you! I'm glad my videos are helping.
@drbulltrader9107
@drbulltrader9107 Жыл бұрын
I really appreciated this video. Wishes your channel get bigger n bigger.
@EduardoSilva-us1jp
@EduardoSilva-us1jp Жыл бұрын
Amazing. I used to secure my smalls projects implementing jwt encoder/decoder with the help of libraries like jjwt directly, as well as overriding filter methods from classes/interfaces such as UsernamePasswordAuthenticationFilter, OncePerRequestFilter. But this way you showed us has simplified it a lot. One more subscriber!
@DanVega
@DanVega Жыл бұрын
Fantastic! Glad I could help out.
@mohamadsharifi2353
@mohamadsharifi2353 5 ай бұрын
I really enjoyed this video. Thank you for providing such great content.
@rajmohanparayil
@rajmohanparayil 3 ай бұрын
Wow. Learned a lot of very relevant security implementation in a very smooth and clean fashion and in such a short time.
@DanVega
@DanVega 3 ай бұрын
Glad you liked it!
@arnaudpoutieu1331
@arnaudpoutieu1331 Жыл бұрын
Many thanks, Dan. Your content is quite valuable for someone like me harnessing input to get better at building Enterprise grade applications. Merci beaucoup!!!
@DanVega
@DanVega Жыл бұрын
I appreciate the kind words Arnaud.
@Justsomeguy492
@Justsomeguy492 Жыл бұрын
finally up to date spring security tutorial :) very good explanation
@alexanderkazeev3729
@alexanderkazeev3729 Жыл бұрын
Thanks for a great tutorial. The article is very useful and helpful.
@sajisanjoris2759
@sajisanjoris2759 Жыл бұрын
Thank you Dan! Great work!
@robertinnoelson6378
@robertinnoelson6378 Ай бұрын
Awesome Dan! Thanks for the rich tutorial
@fjspitz
@fjspitz Жыл бұрын
Nice tutorial Dan! Thanks a lot.
@midewestmond9442
@midewestmond9442 Жыл бұрын
Nice video you just earn a subscriber I actually love the fact you don't define another class just to write another method like other youtubers do
@DanVega
@DanVega Жыл бұрын
Thanks for the sub!
@kasimgul
@kasimgul Жыл бұрын
Thank you for this, Dan. I would love to see a follow up video for implementing "Refresh Token" on top of this :) I know people will love it.
@DanVega
@DanVega Жыл бұрын
Thank you for the suggestion, it's already on the backlog. github.com/danvega/office-hours/discussions/27
@tabancosmos2235
@tabancosmos2235 Жыл бұрын
I am a nodejs and Golang API. I found this tutorial very help for my current work using Spring-boot. One thing about Spring-boot is that, when you use Spring-Boot with higher version some errors like this shows up: This error occurs in the NimbusJwtDecoder.validateJwt method of the org.springframework.security.oauth2.jwt.NimbusJwtDecoder class. The NimbusJwtDecoder class is used to decode JSON Web Tokens (JWTs) and is part of the Spring Security OAuth 2.0 framework.
@behzadfazelasl9581
@behzadfazelasl9581 Жыл бұрын
Thanks Dan. it was crystal clear
@AleksandarT10
@AleksandarT10 Жыл бұрын
Great video! Really helpful to get people started with latest Spring Security stuff and JWT! Few questions/comments though: 1. It would be good if you can extend the github repo and add a branch which shows the symmetric key approach - i guess it would be easy for the Decoder as u mentioned, but would like to see how to change the Encoder 2. Maybe to make it more realistic instead of HttpBasic - it would be good to have a UserNamePassword Authentication where the user calls an endpoint with username/password as body and the token generation happens based on that 3. Building on top of 2), it would be great if this gets connected to a database where hashing + salting is used as this can be used as a starter for real projects 4. Having roles in the example/video would be great Looking forward to your next video Dan!
@DanVega
@DanVega Жыл бұрын
Great suggestions. Thank you Aleksander
@lukamaletic9557
@lukamaletic9557 Жыл бұрын
Is there any tutorial that would build on top of this I need the DB connection for users...
@NARESHBHADKE
@NARESHBHADKE 5 ай бұрын
@@lukamaletic9557 You could inject UserDetailsService in SecurityConfig rather than InMemoryUserDetails @Bean public UserDetailsService userDetailsService() { return username -> userRepository .findByUsername(username) .orElseThrow(() -> new UsernameNotFoundException("user not found")); }
@svalyavasvalyava9867
@svalyavasvalyava9867 11 ай бұрын
wonderful tutorial, thank toy very much 😊
@monjurmorshed2854
@monjurmorshed2854 Жыл бұрын
U have a nice way to explain, great work!!!
@DanVega
@DanVega Жыл бұрын
I appreciate that Monjur. I make videos that I would enjoy watching and I’m glad others are learning from them as well.
@rahanimhand6895
@rahanimhand6895 Жыл бұрын
Amazing !!!! Great video, Thanks 👌
@rajansonvane488
@rajansonvane488 Жыл бұрын
Wonderful. Very helpful. Thanks for sharing!!
@DanVega
@DanVega Жыл бұрын
Glad it was helpful!
@kyriakosmandalas2121
@kyriakosmandalas2121 Жыл бұрын
Great tutorial and topic. Really clears things out. Would be great to show next how to update JWT to include user's roles and permissions. And of course looking forward for Spring Authorization server!
@DanVega
@DanVega Жыл бұрын
I actually set the authority and I believe in the repo there is an example of checking for it at the method level. If there isn’t let me know (I’m away from my computer at the moment)
@jeanaimeravomanana9545
@jeanaimeravomanana9545 Жыл бұрын
Great content, really helpful thank you
@leonzer8257
@leonzer8257 Жыл бұрын
Thank you very much! Greetings from Greece!!!
@DanVega
@DanVega Жыл бұрын
You are welcome!
@benizraadacudao3020
@benizraadacudao3020 Жыл бұрын
Thanks for this Dan.
@DanVega
@DanVega Жыл бұрын
You’re welcome Ben.
@rizkysiregar
@rizkysiregar 2 ай бұрын
Thanks for sharing dan !
@gamires
@gamires 4 ай бұрын
Excellent! Thank you.
@starterdev
@starterdev Жыл бұрын
Thanks for the video ❤
@pavanramagouni6709
@pavanramagouni6709 Жыл бұрын
Thank you Dan. nicely explained and Really helpful.
@DanVega
@DanVega Жыл бұрын
Glad you enjoyed it!
@fahrican9719
@fahrican9719 Жыл бұрын
great tutorial thanks!
@menabebawy390
@menabebawy390 10 ай бұрын
I super like your video, I have learned a lot form it
@SigmaLearningCenter
@SigmaLearningCenter Жыл бұрын
Amazing, thanks a lot!
@bejobarokah3485
@bejobarokah3485 Жыл бұрын
Thank you for the tutorial
@DanVega
@DanVega Жыл бұрын
You’re welcome 😊
@m_jdm357
@m_jdm357 Жыл бұрын
Everything works great!
@aayush481
@aayush481 Жыл бұрын
Hi Dan, can you also please talk about how spring mvc works internally, like dispatcher servlet, how by default exceptions are handled in rest apis etc.
@toan1nguyen674
@toan1nguyen674 9 ай бұрын
Thanks for asymmetric rsakeys knowledge you've shared.
@tipswithnna6670
@tipswithnna6670 Жыл бұрын
Thanks for sharing about JWT
@mdziakhan5950
@mdziakhan5950 9 ай бұрын
Great Explanation
@maxjustmax521
@maxjustmax521 Жыл бұрын
amazing as usual !
@DanVega
@DanVega Жыл бұрын
Thank you my friend 🙏
@dekeyserwilly
@dekeyserwilly Жыл бұрын
Thanks, very nice explaind.
@DanVega
@DanVega Жыл бұрын
Thank you!
@mvlad7402
@mvlad7402 Жыл бұрын
Excellent video! Need to test spring security with Ping Federate.
@illyam689
@illyam689 Жыл бұрын
awesome video! subscribed!
@DanVega
@DanVega Жыл бұрын
Thanks for the sub!
@mehlulinokwara3841
@mehlulinokwara3841 Жыл бұрын
Very good take there.
@alibaba40thvs
@alibaba40thvs 10 ай бұрын
Thanks a lot Dan!
@alibaba40thvs
@alibaba40thvs 2 ай бұрын
Guys take a look at its RFC they have a nice diagrammatic brief of these terms such as Resource Owner, Authorization Server, Resource Server, and Client
@sunhsiang6644
@sunhsiang6644 Жыл бұрын
Good video It's really useful🥰🥰!
@user-zw4ki9tw4e
@user-zw4ki9tw4e 10 ай бұрын
First, thank you for such a comprehensive explanation of the new spring security. I'm going to take minor issue with it because, as with just about every tutorial I've seen for spring boot security, the user logon and Jwt generation is in the same sever as the Jwt consumer for endpoint security. This would never happen in the wild and creates confusion as to which SecurityConfig configurations are needed for each.
@EazzyWizzi
@EazzyWizzi Жыл бұрын
Great Video, keep up the good work
@DanVega
@DanVega Жыл бұрын
Thanks, will do!
@arlekino65
@arlekino65 11 ай бұрын
Thank you Dan. I meant A LOT! Would you consider to create a video for those like me with a title "How to read Spring documentation and connect things together"? Lol. Thanks again!
@mrkostya008
@mrkostya008 Жыл бұрын
finally, an informative tutorial that ACTUALLY uses BUILTIN jwt tools, and not some filters and JwtUtility classes to secure an app
@marekj3759
@marekj3759 10 ай бұрын
Very good video, if anybody haven't mentioned yet, it would be good to replace inMemory user with UserDetailsService on data base. Additionally securing rest api with roles. Video would be a bit longer than 1hours, but woud cover topic from A to Z
@dodgechallenger5606
@dodgechallenger5606 Жыл бұрын
Perfect video. Thank you, Dan! Like+Sub
@rbelatamas
@rbelatamas Жыл бұрын
thank you so much ❤
@neerajagrawal3211
@neerajagrawal3211 3 ай бұрын
Great information. I think a simple video will also be helpful which explains how to protect API using Okta or Keycloak since in most situations you don’t write authorization server yourself.
@edwardm4348
@edwardm4348 11 ай бұрын
Great video! You make it so easy to grasp the concept. A quick question. How would you secure the APIs using JWT if the application is using (username & password) in some cases and also biometrics authentication in other cases.
@alexgutjahr
@alexgutjahr Жыл бұрын
I'm guilty of rolling up my custom solution, pulling in a third party library. Thanks for this video, Dan! Gotta refactor a bit!
@DanVega
@DanVega Жыл бұрын
Thanks Alex. Hope the refactor goes smooth.
@IvanRandomDude
@IvanRandomDude Жыл бұрын
For some reason 99% of tutorials and guides on the internet use that approach instead of resource server.
@rarecase3666
@rarecase3666 Ай бұрын
You are right.... Spring Security tutorials shows that people try to understand but cannot make it working... Spring Security Team also makes it very challenging to build something with it. Thank you, Dave, for your tutorial - it really helped to make it working.
@andrean40
@andrean40 Ай бұрын
is this tutorial outdated?
@victoradepoju5510
@victoradepoju5510 Ай бұрын
No ​@@andrean40
@adamantine001
@adamantine001 12 күн бұрын
@@andrean40 Sort of, but you can implement most of what's being presented here on JDK 21.
@pabloc1519
@pabloc1519 8 ай бұрын
THANK YOU THANK YOU!!!!
@davypaterne
@davypaterne Жыл бұрын
great video very useful
@davidaskatra4140
@davidaskatra4140 Жыл бұрын
You are great man
@DanVega
@DanVega Жыл бұрын
Thank you, David!
@szalaytamas3184
@szalaytamas3184 Жыл бұрын
your outro music is so good
@sakthinivas1840
@sakthinivas1840 9 ай бұрын
Hi Dan, really a good video. One functionality which could be added is adding refresh token feature, thanks
@gerogsg2698
@gerogsg2698 Жыл бұрын
Very good your video!!! I have a question for you: since you said that this is the beginning with jwt and not the goal, what other functions can I do with jwt?
@techystuffs371
@techystuffs371 Жыл бұрын
Awesome tutorial as always. I have quick one... When using assymetric encryption do we use the private key to encrypt the data or the public key? With the little knowledge I have on encryption, I'm pretty sure we use the public key for encryption and the private key for decryption.
@paulo__vieira
@paulo__vieira Жыл бұрын
For me as a complete beginner it was so easy to follow. Thanks for this tutorial, it was really helpful.
@DanVega
@DanVega Жыл бұрын
You're very welcome!
@mattymedia3624
@mattymedia3624 Жыл бұрын
hello dan, thanks for such a good content, this topic is complex but thanks to you I have been able to understand it better. I only have a small question, why is it necessary to disable csrf?
@vintage8
@vintage8 Жыл бұрын
Hey, great video. I learned very valuable things from your videos. I was wondering how can I do the following: I have a 'tokenVersion' column in my users table. Every time User logs out or refreshes the token I'm going to increase it by 1. When the JWT is decoded and if it's valid I want to check if the tokenVersion in the JWT and in the DB matches. If it matches request can continue if not I want to throw 401.
@SD-gw5vm
@SD-gw5vm Жыл бұрын
Are you able to create another video using the other method you mentioned. Where we do not manually create the keys?
@chanchhaya
@chanchhaya Жыл бұрын
Thank you teacher
@lts8683
@lts8683 Жыл бұрын
Thanks you. Can please explain also keycloak with spring.
@x2TruNation
@x2TruNation Жыл бұрын
Hey Dan, New question, obviously us as viewers are following along and just basically copying the code that you write down - but you seem to know exactly what we need and why we need it. Are there any resources you can point me to that could potentially help me understand the architecture of spring security in more detail but also how you learned this to a point where you just know what you need to use? Bit of a loaded question, but i’m keen to learn as much as possible. Right now all it feels like is that i’m copying code from you without truly understanding why we’re doing certain things. Cheers
@user-qd5np7sl2z
@user-qd5np7sl2z Жыл бұрын
First off awesome video Dan. I have seen no code/logic on the resource server side to validate token. Is this optional on resource server end or its a must.
@bartomiejdziadosz8616
@bartomiejdziadosz8616 Жыл бұрын
That was great! What about video about OAuth2 with Auth/Resource/Client?
@AntonioCabralNumberOne
@AntonioCabralNumberOne 3 ай бұрын
Hi Dan. Since we're already on Spring Boot 3.2+ would you mind an update video on this matter? Keep up the good work!
@user-qt6kq8sh8r
@user-qt6kq8sh8r Жыл бұрын
Thank you Dan, it's a greate tutorial for beginners. Can you please make a guide about refreshing jwt please.
@DanVega
@DanVega Жыл бұрын
Noted! I have this suggestion here github.com/danvega/office-hours/discussions/27
@lilinh9631
@lilinh9631 6 ай бұрын
Hey! Great video! But how did you do to autogenerate code just by typing jwt? Thanks a lot!
@subhajitkhasnobish2370
@subhajitkhasnobish2370 10 ай бұрын
Hey Dan, great work, I have just one question that this oAuth2ResourceServer() takes one Customizer but the jwt() referened by method reference is not having void return type as of thr customize() of Customizer..and we are not getting compile time error...how it is possible?
@alx1024
@alx1024 Жыл бұрын
Hi! Great video, like all your videos! Especially now that Spring Security 6 is mixed in with older tutorials on the web this is very helpful. A suggestion: this is now already deprecated: ".oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)" and has to be replaced with ".oauth2ResourceServer((oauth2) -> oauth2.jwt(Customizer.withDefaults())". Also a question, how do you get this snippet-functionality at 30:00?
@rohitbhandari1873
@rohitbhandari1873 10 ай бұрын
can't fix it even using Customizer.withDefaults();
@nooruddinraotiwala353
@nooruddinraotiwala353 5 ай бұрын
dan what is the alternative to csrf should be used as it is deprecated in later spring security version 6.+?
@ValentynHruzytskyi
@ValentynHruzytskyi Жыл бұрын
Great video! Thanks! Could you explain: you have showed the project creation with the spring starter io source. But, after project was created, you show 2 pom files - problem in that the spring.starter actually created only one single pom. How to I have to understand and follow your solution? And the main issue - I have implemented all steps and this solution doesn't work: yes, I received token, but this token doesn't work for other requests - I have receiving 401 error for all following requests. Now I try to understand the difference - and the difference only in the pom files between your and my code. But you are not explained them
@ryanwakabayashi1758
@ryanwakabayashi1758 Жыл бұрын
I love your videos! I had a question on how you would approach deploying this application. I am trying to deploy to AWS beanstalk, but I'm having difficulties with the RSA .pem files. I have been trying to add the public and private keys as an environment property in elastic beanstalk, but am having difficulties because it is a string value and not a file. I also tried to add a key converter with @Component and @ConfigurationPropertiesBinding, but I still get a failed convert from string to RSAPublicKey. What do you think should be the approach/best practice to remedy this?
@user-en7qc9dt8p
@user-en7qc9dt8p Жыл бұрын
저도 이에대한 같은 오류가 발생했습니다 어떻게 해결할 수 있는지 궁금합니다
@budsyremo
@budsyremo Жыл бұрын
Hi Dan , glad i found your tutorial but my problem is the spring's documentation. It is awful . How do you go about in reading the documentation. I have a question , I want to implement this as a microservice , so what should be the logic behind it ? Everytime a user hits my request he/she will be entering username and password and I will be granting a token to the client. Once the client gets that token how will i forward that token from my microservice to this authentication microservice ?
@ankanghosh169
@ankanghosh169 11 ай бұрын
Hii Dan, I love ur tutorials.. my question is how can i create a seperate authentication servuce using jwt. And then use that is a seperate client service to secure endpoint? Thanks..
@Rasmusorum
@Rasmusorum Жыл бұрын
is it ok to use the 6.0.0 dependency version of oauth2 resource server? It doesn't contain spring-security-oauth2-jose
@amirhosseinbayat9657
@amirhosseinbayat9657 2 ай бұрын
Hey Dan, it's amazing, but is there any mechanism in order the user logged out of the system, how we can invalidate the user token?
@mbhambhu3624
@mbhambhu3624 Жыл бұрын
Hi Dan, thanks for video. Once the token is expired, how would i able to get new access token without relogin.. is it possible to implement some refresh token..?
@ubershh
@ubershh Жыл бұрын
Dan, thanks for great video! Can anyone help? How to send response back if request was with invalid credentials? I've added custom entry point, so if the user provided no auth token he gets custom json with error message, but how to handle such exceptions as UsernameNotFoundException and BadCredentialsExceptions?
@pricodebysp5364
@pricodebysp5364 Жыл бұрын
Hi Dan, I did the constructor injection of RSA properties still getting the getting error parameter 0 of construtor and bean not found
@yannickmussche5068
@yannickmussche5068 Жыл бұрын
Dear Dan, I followed your tutorials to set up the new Spring security. Helas, the one thing I cannot get working is that the roles get authenticated. Is it possible to make a video in which you explain the spring security setup with jwt tokens and a hiberante database authentication with roles?
@mohamedibrahim1836
@mohamedibrahim1836 Жыл бұрын
Also to use the authorization as a micro service and export it, import it in multiple application across the company portfolio for a aligned one platform!
What is JWT? JSON Web Tokens Explained (Java Brains)
14:53
Java Brains
Рет қаралды 1 МЛН
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 125 МЛН
Каха и суп
00:39
К-Media
Рет қаралды 6 МЛН
Spring Security, demystified by Daniel Garnier Moiroux
2:36:00
Spring boot + PostgreSQL + Liquibase + Testcontainers
22:01
Sergey Labuzov
Рет қаралды 9 М.
JWT-аутентификация для веб-приложений - Spring Security
39:49
Уголок сельского джависта
Рет қаралды 13 М.
Implementing JWT (JSON Web Tokens) with Spring Security in Springboot App
36:25
Spring boot 3.0 - Secure your API with JWT Token [2023]
2:01:46
Bouali Ali
Рет қаралды 143 М.
АЙФОН 20 С ФУНКЦИЕЙ ВИДЕНИЯ ОГНЯ
0:59
КиноХост
Рет қаралды 1,1 МЛН
Как удвоить напряжение? #электроника #умножитель
1:00
Hi Dev! – Электроника
Рет қаралды 926 М.
Самые крутые школьные гаджеты
0:49
Todos os modelos de smartphone
0:20
Spider Slack
Рет қаралды 59 МЛН