How to Logout from Spring Security - JWT

Buy me a coffee: ko-fi.com/boualiali #spring #learning #springboot #springtutorial #springsecurity #developpement #java #arraylist #linkedlist #springdatajpa #querybuilder #aliboucoding #alibou #validation #mongodb #springboot_3 #oauth2 #security
🤔 When using Spring Security with JWT token, there is no implicit implementation for logout. It's up to the developer to implement the logout functionality, which involves invalidating the JWT token and clearing the authentication information from the Spring Security context.
💡 One recommended way to implement the logout functionality is to use a custom logout handler that invalidates the JWT token and clears the authentication information. This custom logout handler can then be registered with the LogoutConfigurer in your Spring Security configuration, along with any other handlers you want to execute as part of the logout process.
👨‍💻 It's important to ensure that the custom logout handler is executed after the JWT filter in the filter chain. This can be achieved by chaining the logout handlers in the desired order using the addLogoutHandler and logoutSuccessHandler methods of the LogoutConfigurer.
👉 In summary, while Spring Security does not provide an implicit implementation for logout when using JWT tokens, it is possible to implement a custom logout handler that invalidates the token and clears the authentication information, and register it with the LogoutConfigurer in the Spring Security configuration.
🚀 With a custom logout handler, you can take control of the logout process in your Spring Security application and ensure that the JWT token is properly invalidated and the user's authentication information is cleared. So if you're using JWT tokens with Spring Security, consider implementing a custom logout handler for a smoother user experience!
Don't Forget to
===========================================
💯 Source code: github.com/ali-bouali/spring-...
💯 Free courses here: aliboucoding.com
💯 Subscribe to the youtube channel
💯 Join our Discord Community - / discord
💯 Join our Facebook Group - / 589612651142975
💯 Join our Instagram: / alibou_coding
Table of content
00:00 Intro
01:56 Does Spring provide logout mechanism?
03:56 Logout mechanism idea
06:49 Extending the class diagram
08:39 Create the Token entity
13:25 Create the Token repository
17:23 Save the generated token
21:42 Testing the changes
24:53 Revoke all user tokens
27:49 Test the changes
29:03 Update the JwtAuthentication Filter
35:24 Test the changes
37:17 Let's implement the logout
38:07 Add the security configuration
41:57 Create the Logout handler service
43:44 implement the logout handler service
47:04 Test the logout mechanism
49:28 Outro

Пікірлер: 289

  • @BoualiAli
    @BoualiAli11 ай бұрын

    COUPON Code: *EARLYBIRD20* => Spring Data J PA course: aliboucoding.com/p/the-full-guide-to-master-spring-boot-data-jpa

  • @MarkSatin1
    @MarkSatin1 Жыл бұрын

    Thank you so much for this and your other Spring Security videos! You break down overwhelming concepts into clear smaller pieces so easily. I learned so much from just going through a couple of your videos. Looking forward to watching more!

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Glad you like them! This motivates me

  • @aziztolearn
    @aziztolearn11 ай бұрын

    Excellent videos and playlist! 👍👍👍👍

  • @serdarsen6099
    @serdarsen609911 ай бұрын

    Thank you so much for this and your other Spring Security videos! Your work is truly wonderful; please keep it up!

  • @BoualiAli

    @BoualiAli

    11 ай бұрын

    Thank you! Will do!

  • @devynlab
    @devynlab Жыл бұрын

    Just finished watching the previous video implementing JWT, and adding this on top of that I've learnt a lot. Super thanks man 🔥.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy to know that. Happy you liked it

  • @Michaeljamieson10
    @Michaeljamieson10 Жыл бұрын

    These are amazing! Refresh tokens would be very useful !

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thanks. I will create a video about that fir sure

  • @mfarooqwasi
    @mfarooqwasi5 ай бұрын

    Great teacher with clear voice and content, making the life easier for the spring boot developers, great contribution and highly appreciated. Thank you so much 🥰

  • @sharathkumar2018
    @sharathkumar2018 Жыл бұрын

    A lot of videos out there which rarely explain about logout! thanks a lot for this amazing video 🙏

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you loved it 😊

  • @FilipPolitowski
    @FilipPolitowski4 ай бұрын

    This is the best video about Spring Security and JWT. Thanks a lot for this lesson, it helped me incredibly :)

  • @anirudh514
    @anirudh5145 ай бұрын

    Thank you so much, very well explained!

  • @HYR0720
    @HYR072011 ай бұрын

    You are so wonderful! you teaching is very easy to understand. I watched your video 4 about spring security and i added more to watch them later!

  • @BoualiAli

    @BoualiAli

    11 ай бұрын

    Really happy you liked it

  • @saqib-fi4bd
    @saqib-fi4bd2 ай бұрын

    Thank you so much for these updated videos. You have no idea how frustrated I got due to the out dated ones when working with Spring boot 3. you're helping me in both my full time job and in my freelance projects especially when it came to security of APIs. Bundle of Thanks

  • @BoualiAli

    @BoualiAli

    2 ай бұрын

    Glad I could help!

  • @flyh21c
    @flyh21c8 ай бұрын

    You are the best! Thanks a lot! Mentions every side of the JWT. Basics, logout, refresh token, password change etc...

  • @BoualiAli

    @BoualiAli

    7 ай бұрын

    Happy you liked it!

  • @mouradeljayi584
    @mouradeljayi584 Жыл бұрын

    Amazing, Thank you and please keep uploading videos like this cause we really learn a lot from you .

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thanks 🙏 I will absolutely continue uploading. You’re my source of motivation

  • @choooaround
    @choooaround3 ай бұрын

    Thanks, your video goes at a very good pace with clear explanations. Apart from a couple of deprecated codes, it was great. Thanks for your help!

  • @BoualiAli

    @BoualiAli

    3 ай бұрын

    deprecations are inevitable. I always release new videos for the updates. check the playlists or search in the channel

  • @boubacarbarry222
    @boubacarbarry222 Жыл бұрын

    Thank you again, it's very clear !

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    My pleasure ❤️

  • @aminechatate8487
    @aminechatate848711 ай бұрын

    Hi from Morocco, ur spring security tutorial are perfect, thank you for ur help.

  • @BoualiAli

    @BoualiAli

    11 ай бұрын

    Glad you like them!

  • @kdvamja
    @kdvamja Жыл бұрын

    Such amazing and useful content and a better way to explain it in an easy way. We love your work and efforts. Thank you for such amazing content like this ❤.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you liked it

  • @osekagorska2330
    @osekagorska2330 Жыл бұрын

    Well done, keep up the amazing work!

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thanks, will do!

  • @mustafasametsonmez7034
    @mustafasametsonmez7034 Жыл бұрын

    These videos are amazing thanks a lot. I searched many videos about jwt security in java spring boot but they wasn't easy to understand but I can learn easily and clearly with your videos thanks. access and refresh token would be great

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you for the good feedback. I will make a video about refresh token

  • @yosr.
    @yosr. Жыл бұрын

    tbarkallah 3la weld bledi thank you so much Ali you're a lifesaver

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank youu 🙏 My pleasure

  • @coralie5093
    @coralie50935 ай бұрын

    Thank you. Good job!

  • @BoualiAli

    @BoualiAli

    3 ай бұрын

    Glad it was helpful!

  • @iivailo94
    @iivailo94 Жыл бұрын

    really useful videos, keep going! I appreciate your course videos

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    I’m really happy that you like my content. This motivates me to create more

  • @arohawrami8132
    @arohawrami8132 Жыл бұрын

    Thanks a lot Bouali. It was very beneficial as always. I really admire your high quality work and please continue creating more content.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you liked it

  • @arohawrami8132

    @arohawrami8132

    Жыл бұрын

    @@BoualiAli Thank you Ali

  • @michealjohn5288
    @michealjohn5288 Жыл бұрын

    He is the best Teacher on spring security. i will recommend you anytime and anywhere

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you 🙏. Really appreciate that

  • @shraddhaparajuli7531
    @shraddhaparajuli7531 Жыл бұрын

    Applause to your effort. Need to say this video is really informative and helped me extremely for implementing logout with jwt tokens. It would be really helpful if you could start a youtube series on implementing the microservices with all the features provided by spring cloud.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    I already started preparing for such course. Preparation take really long time

  • @sandeepkumar-yz6lw
    @sandeepkumar-yz6lw Жыл бұрын

    Thankyou Sir for sharing your knowledge.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    My pleasure

  • @nuhali4729
    @nuhali47297 ай бұрын

    Thanks for this video Ali !

  • @BoualiAli

    @BoualiAli

    7 ай бұрын

    My pleasure

  • @KhalilChargui-jm1fj
    @KhalilChargui-jm1fj4 ай бұрын

    Amazing, Thank you

  • @mohammadobeidat5685
    @mohammadobeidat568510 ай бұрын

    Mashallah, thank you my brother for the clear step-by-step tutorial. Keep it up!

  • @BoualiAli

    @BoualiAli

    10 ай бұрын

    My pleasure!

  • @meetsamseen
    @meetsamseen10 ай бұрын

    Awesome tutorial man! 48:20 was spot on, you got me right there.

  • @BoualiAli

    @BoualiAli

    9 ай бұрын

    Awesome, thank you!

  • @behzadfazelasl9581
    @behzadfazelasl9581 Жыл бұрын

    Very useful and clear explanation. Thanks Ali

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Glad it was helpful!

  • @mrpipiskin4
    @mrpipiskin4 Жыл бұрын

    Thaks for your job. These guides save a lot of time for beginners

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you liked it

  • @mohamednaitmoussa2600
    @mohamednaitmoussa260010 ай бұрын

    the best one teaching spring 🔥🔥

  • @BoualiAli

    @BoualiAli

    10 ай бұрын

    Wow, thanks!

  • @etodemerzel1011
    @etodemerzel10113 ай бұрын

    Perfection !!!! 👍

  • @BoualiAli

    @BoualiAli

    3 ай бұрын

    Glad you like it!

  • @erichhc9698
    @erichhc9698 Жыл бұрын

    excellent Ali, I don't miss any video I learn a lot from you 😎

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy to know that. This motivates me

  • @pateldharmesh1361
    @pateldharmesh1361 Жыл бұрын

    It really cool and superb content.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you liked it!

  • @user-dg2gr3vi4d
    @user-dg2gr3vi4d6 ай бұрын

    wonderfull !!! what a enrgy sir. Appriciated....

  • @BoualiAli

    @BoualiAli

    6 ай бұрын

    So nice of you

  • @arsalansarwer
    @arsalansarwer Жыл бұрын

    great tutorial, and code works fine

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy to know that

  • @user-tr7vn8qm8k
    @user-tr7vn8qm8k Жыл бұрын

    Thank you so much loved the video. I was stuck and looking for a resource. It helped me alot. Love from India

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy to have you here. Happy you like my content

  • @autogenes
    @autogenes4 ай бұрын

    Nice one :)

  • @ismailforeveryone6889
    @ismailforeveryone6889 Жыл бұрын

    Amazing please keep going this topic of security is very rare, specially with this updates of spring security 6

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you, I will

  • @ismailforeveryone6889

    @ismailforeveryone6889

    Жыл бұрын

    @@BoualiAli Thank you so much Mr

  • @user-ld1bh1jt9j
    @user-ld1bh1jt9j Жыл бұрын

    Thax Sir Realy Helped

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you liked it

  • @Ideastoriesenglish
    @Ideastoriesenglish Жыл бұрын

    Thank you so much!

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    You’re welcome

  • @vadimsirenko6966
    @vadimsirenko6966 Жыл бұрын

    Hello from Russia, man. Thanks for your very helpful videos.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Greetings from Tunisia 🇹🇳 Happy you like my content

  • @jeanmorisanato6458
    @jeanmorisanato6458 Жыл бұрын

    Thanks for his sweet video

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you liked it

  • @errahulrajocjp
    @errahulrajocjp6 ай бұрын

    you have saved the IT Industry, Sir!!

  • @BoualiAli

    @BoualiAli

    2 ай бұрын

    Glad you liked it!

  • @tugbakoc379
    @tugbakoc3795 ай бұрын

    thank you!!

  • @BoualiAli

    @BoualiAli

    2 ай бұрын

    welcome

  • @amradel6030
    @amradel6030 Жыл бұрын

    thank you so much

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    You're welcome!

  • @amirkenesbay1615
    @amirkenesbay1615 Жыл бұрын

    Wow broooo u r amazing. This tutorial helped me to solve my problem in my project))

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy to know that bro.

  • @qossayjawadzeinelddin6727
    @qossayjawadzeinelddin67274 ай бұрын

    great

  • @khalilbouali3480
    @khalilbouali34809 ай бұрын

    thanks a lot !

  • @BoualiAli

    @BoualiAli

    9 ай бұрын

    You're welcome!

  • @saifchtourou5083
    @saifchtourou5083 Жыл бұрын

    Good Job !

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank bro

  • @ianfernandez3258
    @ianfernandez325810 ай бұрын

    Thank you

  • @BoualiAli

    @BoualiAli

    10 ай бұрын

    You're welcome

  • @user-lv5zh2ob4m
    @user-lv5zh2ob4m Жыл бұрын

    Топ контент. Дякую

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    my pleasure!

  • @hafedhbaazouzi3617
    @hafedhbaazouzi3617 Жыл бұрын

    Good Job Thanks

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    My pleasure

  • @inderpreetsingh5126
    @inderpreetsingh5126 Жыл бұрын

    Thanks and Keep It Up Bro

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you. I will

  • @infinity2471
    @infinity24713 ай бұрын

    ¡Gracias!

  • @BoualiAli

    @BoualiAli

    3 ай бұрын

    Thank you so much for the support. This is so generous from you.

  • @khalilmarzouki636
    @khalilmarzouki636 Жыл бұрын

    good job, keep going ali 🤩🤩

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you 🙏

  • @damian98772
    @damian9877211 ай бұрын

    If someone has a problem with LazyInitializationException occuring, in my case it was because i had @Data annotation in both User and Token Entities, that annotation contains @ToString which caused an infinite loop, I just replaced it with @Getter and @Setter.

  • @BoualiAli

    @BoualiAli

    11 ай бұрын

    Really thank you for sharing. Keep this great mindset

  • @anirudh514

    @anirudh514

    5 ай бұрын

    You saved several hours of time for me. Thank you very much for this suggestion which is hard to notice!!!

  • @ramakrishnamogilipuri1647
    @ramakrishnamogilipuri1647 Жыл бұрын

    nice content.thank you

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Glad you liked it!

  • @ramakrishnamogilipuri1647

    @ramakrishnamogilipuri1647

    Жыл бұрын

    Can you also make videos for Authorization using database roles instead of enum

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    @@ramakrishnamogilipuri1647 will do that

  • @sekharjamallamudi9531
    @sekharjamallamudi95318 ай бұрын

    so much thankful to you sir giving this videos very useful for me. Sir pls do video on forgot reset password

  • @BoualiAli

    @BoualiAli

    7 ай бұрын

    Already done. Subscribe and enable the notifications and you won't miss any of my new videos

  • @kevinameda2711
    @kevinameda2711 Жыл бұрын

    Thank you brother, this is inspiring and realy helped me a lot thank you. Can you pls help create a lecture for messaging queues like kafka or rabbitMQ? Stay blessed

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you for the feedback. MQ is coming soon

  • @aamirkuchay4832
    @aamirkuchay4832 Жыл бұрын

    thanks👋

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    My pleasure

  • @sheldonlawrence8798
    @sheldonlawrence8798 Жыл бұрын

    Excellent, a video on refresh token would help

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thanks, I will create one

  • @lowCostCoder
    @lowCostCoder3 ай бұрын

    it was really a great tutorial. Thanks for sharing your knowledge with us

  • @hamdimekni7220
    @hamdimekni7220 Жыл бұрын

    ❤❤❤

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thanks

  • @bartdiako1
    @bartdiako1 Жыл бұрын

    Thanks Bouali ! this Security vids have been a great help ❤. You are awsome!!! Just wondering if you're going to do a change password vid to? forgotten password and e mail verification ?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Yes I will

  • @user-te9up9ql4n
    @user-te9up9ql4n Жыл бұрын

    Helpful and useful video , but it could be in easier way, you could create a new method called logout , then decode jwt , then change it date and it is all . Your explaining is very clear . Thank you

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thanks for the comment. Just one question regarding you way of doing it. How would you for the user to update the token? (Token is stored on client side)

  • @mbilal6462
    @mbilal64627 ай бұрын

    i think that in the LogoutService before clearing the SecurityContextHolder, in addition to checking for the existence of the token in our database, we should also check that it's not been revoked or expired.

  • @poojabondkar4983
    @poojabondkar49834 ай бұрын

    Thank you so much AliBou !! This is very helpful. can you make video of logout from Keycloak auth server generating jwt token ?

  • @BoualiAli

    @BoualiAli

    3 ай бұрын

    Yes, soon

  • @bartosztoropolski8191
    @bartosztoropolski8191 Жыл бұрын

    Great tutorial! Will you create video about refresh token?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Happy you like it. I will soon

  • @balazsvarga4216
    @balazsvarga4216 Жыл бұрын

    I watched also the prev video about JWT. This videos are very straightforward and to the point. Just a little bit remark: why do you use var declaration instead of naming the proper type?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    It is just shorter to write 😅

  • @lucasmagalhaes1573
    @lucasmagalhaes15737 ай бұрын

    Hello Bouali! Great tutorial as always, thank you! Quick question: doesn't revoking the user's token on every authentication forbids them to log in to the application in different devices at the same time?

  • @BoualiAli

    @BoualiAli

    7 ай бұрын

    Yes, true

  • @sovannborithyun5205
    @sovannborithyun520510 ай бұрын

    Excellence video. Would you create a video using Angular to logout?

  • @BoualiAli

    @BoualiAli

    10 ай бұрын

    Great suggestion!

  • @augustineluvrenebaebae
    @augustineluvrenebaebae5 күн бұрын

    Thank you for your tutorial, I have a question that if every time the user authenticates and logs out, the token will be set revoked to false and expired is also false, but if that is the case, the database will contain corrupted fields. It's redundant and makes the database bigger when having to store the data. Can I clean up that extra data?

  • @hasanfarooqui2250
    @hasanfarooqui225015 күн бұрын

    1) What is the use of revoking and expiring all tokens during JWT creation ( you have already done it in LogoutService class) Why doing twice ? 2)In this application user can not simultaneously login from two device ? (As soon as he login on another device, you are simply revoking all its previous tokens). Is above questions are valid or am I missing anything ? btw lecture is very helpful. Thank you.

  • @user-bx9tf4cj3t
    @user-bx9tf4cj3t5 ай бұрын

    Very cool explained everything thank you!!! One question. Isn't it more logical to just delete tokens so as not to store a bunch of invalid tokens in the database?

  • @medAmineRg

    @medAmineRg

    5 ай бұрын

    i guess you can do it. it just a matter of history

  • @werghiaziz2943
    @werghiaziz29433 ай бұрын

    before watch this video ,after doing the login process and using in my react project and user registre and login ,i save the token in storage to use for others api ,this is good or no ? and when he choose to logout i distroy the token storage.clean()

  • @cristiangomez7227
    @cristiangomez7227 Жыл бұрын

    Hello Bouali, Incredible explanation and as always very useful content. So, I have a question: this implementation is solving the Multiple concurrent sessions problem, isn´t it? because I had an ethical hack just a couple of weeks ago and that was one of the problems to solve.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Really happy you liked it Yes it should

  • @mmge3967
    @mmge3967 Жыл бұрын

    Thanks for your effort in jwt. But i want to ask you a simple question Should we delete the previous tokens for specific user so we don't have alot of rows that we don't need in the database or not? Thanks in advance.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    This also can be an option if you don’t need the already revoked token

  • @mmge3967

    @mmge3967

    Жыл бұрын

    @@BoualiAli ok thanks

  • @alifka6815
    @alifka68155 ай бұрын

    Thanks for the informative video, the only thing I did not understand is why we do the same checks in LogoutHandler as in the filter, because if there is no Authorization header or it does not start with Bearer, then the filter will not skip this request, and if the filter missed us in EndPoint /logout, it means that the Authorization header is there and the token starts with Bearer and it is also not zero, am I right?

  • @ElFozter
    @ElFozter Жыл бұрын

    Please create a video of how to implement the refresh token, thanks for your work.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Working on it

  • @antoniodevic8704
    @antoniodevic8704 Жыл бұрын

    Thanks for the video, a lot of useful info in it! One question though, is with JWT tokens since they are stateless logout part on backend really necessary? Can it present any security issue or is it just enough to stop user from accessing secured resources if it is handled on frontend, invalidated there or deleted from local storage?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    It is also enough to delete it from local storage in the frontend. But this a double check and total logout and revoking the token. Many have request such functionality, so I answered the call 😁

  • @antoniodevic8704

    @antoniodevic8704

    Жыл бұрын

    @@BoualiAli Thanks for the quick answer! Sure, double check can't definitely hurt :D

  • @michaelroyf4766
    @michaelroyf4766 Жыл бұрын

    Thanks for the video! I just didn't understand why we need to create a token during registration. and what about the refresh token?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    The token in the registration is just to avoid re logging Refresh token will come soon

  • @khalilelemam1349
    @khalilelemam13495 ай бұрын

    why did you use inner join when trying to fetch the tokens in 16.56 ? you can do it without making use of joins !! also you said you want the expired and revoked should equal to false and then using or how that?

  • @FantaCocaine
    @FantaCocaine Жыл бұрын

    Great videos! Can you do some on Spring Authorization Server by any chance?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    I will make one

  • @gabrielmoreno7482
    @gabrielmoreno748211 ай бұрын

    Great video! could i replace token with jti?

  • @BoualiAli

    @BoualiAli

    11 ай бұрын

    Yes you can!

  • @theold_new1631
    @theold_new16313 ай бұрын

    Thank you for the video! I have a question. When I log out, Are the tokens just left permanently in the database? I am curious about what processing is common in a practical environment.

  • @BoualiAli

    @BoualiAli

    2 ай бұрын

    you can create a script to clean the DB. By the way, Better use keycloak I will release a new video next week

  • @usounitedsouls2174
    @usounitedsouls2174 Жыл бұрын

    Amazing video ,it was so helpful, My question is why we don't delete the old token instead of setting it expired

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Thank you. You can set a bash script to cleanup the database every period of time.

  • @bogdanjovanovic2737
    @bogdanjovanovic27373 ай бұрын

    16:50 The query will return the tokens for the user that are either not expired or not revoked which means that will include tokens that are expired but not revoked, as well as tokens that are revoked but not expired. Was that the intention or we should return tokens that are neither expired nor revoked? For example instead of: ``` where u.id =:userId and (t.expired = false or t.revoked = false) ``` We can return non-expired and non-revoked tokens: ``` where u.id = :userId and t.expired = false and t.revoked = false ```

  • @ahmetyasinozturk-uu4wd

    @ahmetyasinozturk-uu4wd

    2 ай бұрын

    I think that was a mistake. It should be non expired and non revoked token at the same time as you say.

  • @manospat1066
    @manospat10669 ай бұрын

    Amazing work! Just a question, why we generate jwt token both in register and the authenticate methods? In my understanding, in register the token is generated and then in each coming request user must pass the jwt token. Also, I thought that one user has only one token associated with him, so the relationship between user-token should be one-to-one and not one-to-many? Thanks

  • @BoualiAli

    @BoualiAli

    9 ай бұрын

    Totally true what you said. Just as I mentioned in the video, it is just for the tutorial to have a token after registration. But in real life no need for it. Feel free to adjust ass you need

  • @hamidoubalde2517
    @hamidoubalde2517 Жыл бұрын

    great video @Ali Bouali. I have a question. For you what is best for implementing jwt between custom jwt like you did or use Oath2 resource server which hold jwt implementation. In the last case how to implement logout? Thanks

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Both of them are jwt based. You can use both of them I’m working on a new course that covers both, jwt and oauth 2 with a frontend built with angular. I will publish the a waiting list soon enough so you can register and get discount

  • @hamidoubalde2517

    @hamidoubalde2517

    Жыл бұрын

    @@BoualiAli thanks

  • @migolovach1371
    @migolovach1371 Жыл бұрын

    Very helpful video. I have only one question, maybe I don’t understand something, but why store expired tokens.

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    You remove expired token. In a different context. You might use them for auditing and tracing (maybe)

  • @GoncaloSilva137
    @GoncaloSilva137 Жыл бұрын

    Hi, are we updating the database accessToken expired parameter when the accessTokenExpiration time finish ?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    I didn’t get your question can you please elaborate more?

  • @monkeybrotha855
    @monkeybrotha8558 ай бұрын

    Thank you very much Bouali for your tutorials, I'm a big fan and they helped me a lot in the past. I have a question though, i've implemented the logout like you did in the tutorial and I'm using an angular client but I'm having trouble to make the post request to /api/auth/logout. Do I need to add thist in my controller? I can not invalidate the usertoken calling from the frontend. Does anyone else had this Problem and can help me out here?

  • @BoualiAli

    @BoualiAli

    7 ай бұрын

    Can you give more dettails. How you call the endpoint?

  • @JasmineTrader-mw8ru
    @JasmineTrader-mw8ru Жыл бұрын

    Awesome tutorial. I have one big doubt, I've seen lots of peopke saying its not secure to store tokens in the database. I am wondering the reason why you are doing this?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    As I mentioned, there is no implicit implementation for logout (jwt) and that is one simple solution. You can performa a daily cleanup for revoked / invalid tokens from the database

  • @JasmineTrader-mw8ru

    @JasmineTrader-mw8ru

    Жыл бұрын

    @@BoualiAli yes i understand that. But i do not want to store the valid tokens in database either for security reasons. Can you suggest me a way to revoke a token in the backend? Id there anyway we can remove the jwt from security context holder?

  • @maisen7168
    @maisen71687 ай бұрын

    Thanks for the video! Why do you have expired and revoked flags? From my point of view expired flag is useless and confuses, cause token might not be expired but only revoked. It's two independent states.

  • @BoualiAli

    @BoualiAli

    7 ай бұрын

    I mentioned in the video that these flags might be helpful in the future. Maybe not in this tutorial

  • @MuhammadAli-rp9cz
    @MuhammadAli-rp9cz Жыл бұрын

    Hi Bouali - Thanks for the amazing videos on Jwt. My question - in the first video, why are you only checking that token belongs to the user and does not expire during validation? Where does the secret key and the signature plays a role during validation? Can't i just create my own token and map to a valid and it will still be marked as valid?

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    Decoding the token uses the secret that you generated. And it should be a secret of course 😁 To have your answer, I would ask you to generate a random token with a different secret ( you can use jwt.io website for that) and pass it to the backend and then you will see if it passes or not. Reply to this comment with your feedback. Looking forward for the result 🤛

  • @MuhammadAli-rp9cz

    @MuhammadAli-rp9cz

    Жыл бұрын

    @@BoualiAli Tested an i got the error io.jsonwebtoken.security.SignatureException: JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted. It seems like during decoding - it is not base64 decoding the payload - rather decrypting the signature and extracting the subject from their - Thanks, exactly as you said

  • @mdbits
    @mdbits Жыл бұрын

    This was a great add on to the Spring Security with JWT video! Thanks so much! This works great when testing in Postman, but when I test it from a browser (using axios in React) I cannot seem to avoid CORS violations. I can use @CrossOrigin on my REST controllers. Is there a proper way to configure the logout CORS policy?

  • @EagleT869

    @EagleT869

    Жыл бұрын

    maybe can add this in your security config http.csrf().disable().cors().configurationSource(new CorsConfigurationSource() { @Override public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.setAllowedOrigins(Collections.singletonList("*")); config.setAllowedMethods(Collections.singletonList("*")); config.setAllowedHeaders(Collections.singletonList("*")); config.setExposedHeaders(Arrays.asList( "Authorization", "X-Total-Count", "Link", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials" )); return config; }}).and()

  • @BoualiAli

    @BoualiAli

    Жыл бұрын

    You just need to add a cors bean