SPRING SECURITY 6 with JWT Authentication: Secure Your App in MINUTES!

Поделиться
HTML-код
  • Опубликовано: 11 фев 2025

Комментарии • 51

  • @michaelalozano
    @michaelalozano Год назад +2

    Straight to the point and well explained. Great job!

  • @ІО-16АндрійЛисак
    @ІО-16АндрійЛисак Год назад

    Great explanation of the Spring Security authentication. Helped a lot, thank you

  • @joanjanku1909
    @joanjanku1909 Год назад

    Very useful video. Thank you. Short, concise, very well explained, simple and quite useful.

  • @经泰
    @经泰 2 года назад +2

    Thank you very much! very helpful.

  • @paolofrancescosciammarella1420

    Thanks Sergio!
    Great explanation and very simple code flow!
    Very useful!

  • @AlexanderKurguzkin
    @AlexanderKurguzkin 2 года назад +2

    Thank you, very helpful!

  • @szopsop
    @szopsop Год назад

    Awesome work! This code is so easy to read. Great stuff. Thank you!

  • @jacup001
    @jacup001 Год назад +8

    It was a nice tutorial, but I keep seeing the same mistake over and over from multiple people. Amigoscode did the same thing as you did. The whole point of using JWT token is that you do NOT check against your database. That is literally the most important advantage in comparison to cookie / sessionID. You should have all the relevant information within token itself.
    The only exception to this rule is if you actually want to create or modify resource in your application and you need to make sure that the user still exists in the DB. And even in that case, that logic should be part of the controller/service that handles creation/modification of the resource. But if you just want to make sure your user is valid, the token should be all you need. If it is not, then you should reconsider whether the JWT token authentication is the best approach for your use case.
    But otherwise great tutorial. Thanks for that :)

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  Год назад +4

      You're totally right Jakub.
      I should avoid validating the user against the database for all read actions, but make some checks for the write actions.
      Thanks for the advice. I will use this approach in my followings projects/videos.

    • @huynhanhkietk17hcm15
      @huynhanhkietk17hcm15 Год назад +1

      Can you explain further, I thought what Sergio did was right. First, you login and then receive a JWT token with the login as the payload. After that, every request has the JWT in the header and then the server will know who you are to query the database corresponding to who you are. In which point did he check against the database? My approach is that I have the userId inside the payload so I can query everything with this Id.

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  Год назад +1

      In this new video, ruclips.net/video/YUqi1IjLX8I/видео.html, i get the JWT after validating the credentials, then read the JWT from the headers, without requesting the database.
      Finally, if I perform some write operation, I validate the JWT with the user Id against the database

  • @CeratiGilmour
    @CeratiGilmour Год назад

    Tendrás un ejemplo con el algoritmo RS256 (llaves publicas y privadas)?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  Год назад +1

      La generación de un JWT con RS256? No, nunca he hecho eso. Pero igual esta página te puede ayudar, auth0.com/blog/rs256-vs-hs256-whats-the-difference/

  • @marouaniAymen
    @marouaniAymen Год назад

    Thanks for the video, I think there is a missing point which is that the REST API communication is in clear HTTP, using HTTPS will protect our login object sent in the request body.

  • @Fernando-mg7rx
    @Fernando-mg7rx 2 года назад

    So for credit cards or any sensitive data which would be the way to send it to the backend?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  2 года назад

      You must take into account that the JWT is an immutable token which will be stored in the frontend during all the user navigation. If you store critical information and this token is stolen, somebody may be able to decrypt the token.
      The critical informations must be sent to the backend through a POST request and stored in the backend (in a database or something else).

  • @ІванМостовий-у1в

    Very clear explanation!

  • @AhmedZahranDEV
    @AhmedZahranDEV Год назад

    does we need another endpoint for refresh token ?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  Год назад +1

      Yes Ahmed, the refresh token is another process.
      When refreshing a token, you only need to check if the current token is valid, you don't need to check the credentials again.
      Still, you can do it with a renew token to increase the security, sergiolema.dev/2023/03/13/how-to-renew-a-jwt-without-requesting-to-login/

    • @AhmedZahranDEV
      @AhmedZahranDEV Год назад

      OK thanks a lot
      But its still the same cycle when implementing the renew token
      I hade to validate token and credentials from db for every request or just I check the token validity instead of db call to check the user every request?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  Год назад

      I've implemented a two ways JWT validation in the following video.
      When it's a GET, I trust the JWT and just try to decode it.
      When it's another HTTP verb (POST, DELETE, PUT...) I also check the user in the database.
      bit.ly/43YU0wb

  • @Jux925
    @Jux925 Год назад

    Why write this by hand, doesn't Spring Security provide this out of the box?
    Creating custom filters and jwt tokens, etc

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  Год назад

      Spring Security provides some resources for the most basic authentication, like the Basic auth or the Session auth. But if you want to customize more, using a stateless app, handling the information in the JWT and more, you need to write this by hand.

  • @Lars9177
    @Lars9177 2 года назад

    Hi man..how do you invalidate the jwt token anyway?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  2 года назад +3

      The easiest way to invalidate the JWT is to set a validity date very short, and renew it every short time.
      Another way, is to store the JWT in an invalidation table. Every time you invalidate a JWT, add it to a table and check it when used.
      In a stateless application, there is no invalidateSession that can be used, you have to find some hacks.

  • @abndev.0195
    @abndev.0195 2 года назад +2

    Thanks sound good, but looks like "Hello world".
    What about improving it?
    How?
    1-hour expiration is too long, what if we want
    1> around 10 minutes expiration time (5, 7, 10, 15)
    2> each time authenticated user invokes any authenticated page - his expiration time should be renewed by this now() moment
    3> next request should use new (updated) JWT.

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  2 года назад +1

      It may seem an utopian, but I've use this configuration in several projects. Sometimes with 1 hour, sometimes with 1 week expiration time. It will depend on the usage.
      About the renew of the JWT, I've used another method:
      * When creating the JWT, I also create a renew-JWT (with an expiration higher)
      * Both are stored in the frontend
      * When the frontend receives a 403 HTTP code, it tries an /auth/renew endpoint with the renew-JWT
      * This will return a fresh JWT and a fresh renew-JWT
      In my method, I don't need to generate a new JWT every request. You may have an inactivity higher than 10 minutes, do you want the user to log in again? What about parallel requests, each will receive their own JWT, which one must be stored?
      What do you think?

  • @federico42o
    @federico42o 2 года назад

    Tenes otro canal en español o solo este?

  • @jose-k7y3w
    @jose-k7y3w 11 месяцев назад

    en español por favorrrrr

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  11 месяцев назад

      Todos mis videos tienen subtítulos en español traducidos por mi mismo.

  • @ProfessorAglayrtonJuliao
    @ProfessorAglayrtonJuliao 2 года назад

    VocÊ é brasileiro?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  2 года назад +1

      Quase, sou espanhol

    • @ProfessorAglayrtonJuliao
      @ProfessorAglayrtonJuliao 2 года назад

      @@TheDevWorldbySergioLema amigo, tens tempo para fazer essa aula particular comigo? Eu pago. posso enviar meu discord?

    • @TheDevWorldbySergioLema
      @TheDevWorldbySergioLema  2 года назад +1

      Sorry, but i don't speak Portuguese (i've used google translate to answer you). I speak english, spanish or french. If it's ok for you, send me an email at thedevworldsl@gmail.com

    • @ProfessorAglayrtonJuliao
      @ProfessorAglayrtonJuliao 2 года назад +1

      @@TheDevWorldbySergioLema te respondi via email, poemos fazer em spanish