Rest API Authentication | Spring OAuth 2.0 Resource Server, JWT, MongoDB, Spring Boot

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 111

  • @uhexos
    @uhexos 2 роки тому +8

    Hi please consider increasing your font size when recording these so that we can still read the text comfortably when not in fullscreen

    • @codingwmax
      @codingwmax  2 роки тому

      Thank you for the feedback

  • @rajivkumar-ub6uj
    @rajivkumar-ub6uj Рік тому +4

    Thanks for making. Can you make videos on spring Authorization server and fit this in microservices architecture behind the gateway. Explain oauth 2 + openid flow (from web app to auth server through gateway, can be standard or federated authorisation) and communication among resource services (client credentials grant) etc.

  • @9766475312
    @9766475312 2 роки тому +3

    Excellent and great explanation .. Very useful Please create more contain like this .

  • @bykalim
    @bykalim Рік тому +3

    this is super helpful, concise and just what i needed. Thank you so much for making this video

  • @paulcoelho9529
    @paulcoelho9529 Рік тому

    Thank you
    Finally latest Spring Sec OAuth
    No WebSecurityCongifAdapter

  • @pawsdev
    @pawsdev Рік тому +1

    Спасибо Максим, мало примеров с рефреш токеном, обычно везде только access token

  • @viralyogi
    @viralyogi Рік тому

    Excellent explanation ..Keep uploading new ones.👍

  • @SuperUnknownNetwork
    @SuperUnknownNetwork 11 місяців тому

    Being steady when there is happiness or failure seek peace and patience

  • @Solmaz_Valiyeva
    @Solmaz_Valiyeva 6 місяців тому

    Thanks for detailed explanation👍

  • @YasithTharindu
    @YasithTharindu 7 місяців тому

    thank u sir , its a great video, please continue your chanel

  • @FadiQua
    @FadiQua 2 роки тому

    Nice video!Looking forward to your next video.
    Thank you

  • @nfp613
    @nfp613 Рік тому +2

    wow this is super helpful, concise and just what i needed. Can you make tutorials on everything ???

  • @SpongeBOBat4
    @SpongeBOBat4 10 місяців тому

    Hey Max! Great video. I did have issues with the login end point where an 401 error is thrown. Using the debugger it said it has something to do with InvocationTargetException.

  • @devozs
    @devozs 2 роки тому

    Amazing detailed tutorial

  • @mr_speed_twin
    @mr_speed_twin Рік тому +1

    Hi Max, in my implementation even after doing all the things you mentioned /token api throws 401. In the stacktrace I see the error "Failed to authenticate since the JWT was invalid" your help will be appreciated here

    • @codingwmax
      @codingwmax  Рік тому

      Hey, are you adding the "Authorization" header to the request you are making with a value of "Bearer "?

    • @mr_speed_twin
      @mr_speed_twin Рік тому

      @@codingwmax Bearer and access token

  • @denisentanas.brahmana6411
    @denisentanas.brahmana6411 4 місяці тому

    can you implement using user role authorization?

  • @maneshipocrates2264
    @maneshipocrates2264 2 роки тому +2

    Thanks for the great video. Is it possible to do this with the new spring authorization server? And a client (API gateway)-spring authorization server- with backend resources (services)?

    • @codingwmax
      @codingwmax  2 роки тому

      Great suggestion

    • @lpandp90
      @lpandp90 Рік тому

      the same thing I would like to have, thanks!

  • @armendochandra9169
    @armendochandra9169 Місяць тому

    can u make tutorial to use ur mongodb and docker please?

  • @irshsheikh
    @irshsheikh 2 роки тому

    Great Content!. Thank you.
    Probably keep bigger fonts next time :)

  • @lanzaguisanpvz
    @lanzaguisanpvz 2 роки тому

    Hello, great curse!. when i uses postman to get the refreshToken at /token endpoint its Ok!, but when I get a 401 error in the Angular Interceptor HttpErrorResponse and i call the service that handle the post of the refreshToken, it always crash the spring server "java.lang.IllegalArgumentException: token cannot be empty", I don´t know what to do

    • @codingwmax
      @codingwmax  2 роки тому

      Hello, if you follow the stacktrace, you will see what exactly it complains about. Angular is probably sending an empty refresh token.

  • @lavasopranos5725
    @lavasopranos5725 Рік тому

    hello, .antMatchers is no longer supported by spring security , what is the alternative , although i tried using .requestMatchers and mvcMatchers and got 401 in postman

    • @codingwmax
      @codingwmax  Рік тому +1

      Hello, based on the docs docs.spring.io/spring-security/reference/5.8/servlet/authorization/authorize-http-requests.html you can simply replace the use of ".antMatchers" with ".requestMatchers"

  • @p.p526
    @p.p526 2 роки тому

    Sir, mongodb uri you mention to connect the db is not working. Could you help me with this please?

    • @codingwmax
      @codingwmax  2 роки тому

      Hello, please check the source code linked in the description for the url to use

  • @NguyenHung-wr3yz
    @NguyenHung-wr3yz Рік тому

    Thank you so much, can you do with Spring Authorization Server in microservices ?

  • @ФедорИванов-ъ2и
    @ФедорИванов-ъ2и 2 роки тому +1

    Can you please explain. It turns out that the resource server here is also an authorization server, because it generates tokens? Will you make a video in which you will show an example of a separate own authorization server. Or you can suggest materials where the implementation of your own authorization server is shown, please

    • @codingwmax
      @codingwmax  Рік тому +1

      Yes in this example the resource server acts as an authorization server, it allows exchanging a user password for an access & refresh token.
      An own spring authorization server can also be used for login into the application but it does not support exchanging a user password for an access & refresh token see github.com/spring-projects/spring-authorization-server/issues/126.
      When using the spring authorization server, a browser is needed for a user to perform a login (think OAuth login flow). The best materials to learn about it would be the spring security and spring authorization server docs.

  • @pulato000
    @pulato000 2 роки тому

    Greate explanation!

  • @MatheusMattos-m7i
    @MatheusMattos-m7i 17 днів тому

    Great !!

  • @giovanniamorim6469
    @giovanniamorim6469 2 роки тому

    Fantastic. I did some changes like use mysql without docker. thank you.

  • @PriteshRanjan30
    @PriteshRanjan30 2 роки тому

    nicely explained , Thanks👍

  • @franconekillerz139
    @franconekillerz139 9 місяців тому

    yo, is it possible implement logout? How can I do it?

  • @Fernando-mg7rx
    @Fernando-mg7rx Рік тому

    hello, is there any tutorial or resource you recommend to add all of this into an external authentication service? instead of keeping it on same application

    • @codingwmax
      @codingwmax  Рік тому

      Hey, it sounds like you are looking for an authorization server, the one responsible for all the auth stuff, your simple backend then integrates with this auth server. Docs for the auth server are here: docs.spring.io/spring-authorization-server/docs/current/reference/html/getting-started.html

  • @shuanshuanzai
    @shuanshuanzai Рік тому

    Is that possible to separate resource server and Authentication server? How does the newly added Resource Server use the existing authentication? spring.security.oauth2.resourceserver.jwt.issuer-uri=?

  • @grishapipoyan7501
    @grishapipoyan7501 Рік тому

    Excellent, great explanation. In some videos I've seen the programmers add jwt filter implementing once per request filter.. why you did not add it and whats difference ?

    • @codingwmax
      @codingwmax  Рік тому

      Thank you, the resource server already has inbuilt filters, all we have to do is configure certain beans to leverage existing filters. Other videos like you mentioned that add new filters may not be using a resource server or want to implement custom logic for token validation.

  • @p-lelouch
    @p-lelouch 2 роки тому

    Thank you. your video a help me to much.

    • @codingwmax
      @codingwmax  2 роки тому

      You’re very welcome 🙏

  • @ddifromo
    @ddifromo Рік тому

    Hi! Can you please explain one moment. I get different authentication objects: in register it is user entity and password and in login it is email and password, so token can not be created correctly, what can I do?

    • @codingwmax
      @codingwmax  Рік тому

      Hello, when registering, the user does not exist in the database yet so we create an Authentication object whose auth flag is set to true and then we create the jwt tokens.
      When logging in, we ask the daoAuthenticationProvider to authenticate the user (username and password) against the record stored in the database, if authentication passes we proceed to generate the jwt tokens.

  • @kim_convenience_store
    @kim_convenience_store Рік тому +1

    "EnableGlobalMethodSecurity" is deprecated....

    • @ram0973
      @ram0973 Рік тому

      yep, now we must use EnableMethodSecurity

  • @glaze4629
    @glaze4629 2 роки тому

    The video was great, but you should try the code more often as it would be easier for people to get the idea of each particular concept, one at the time

  • @testingdave7629
    @testingdave7629 2 роки тому

    How to unit test this implementation?

  • @testingdave7629
    @testingdave7629 2 роки тому

    Hi @Max, I just want to ask how you can limit the number of active session of a user on this project?

    • @codingwmax
      @codingwmax  2 роки тому +1

      Hi, in this project I used stateless sessions. If you want to limit sessions you will have to keep track of them somehow. Have a look at this page: docs.spring.io/spring-session/docs/2.2.x/reference/html/spring-security.html

  • @romeobernabe6871
    @romeobernabe6871 Рік тому

    Hi Thanks for the session. Do you have postman collection for this?

    • @codingwmax
      @codingwmax  Рік тому

      Hello, sorry I don't, I will try to provide one next time.

  • @schrodinger_s_cat_theory
    @schrodinger_s_cat_theory Рік тому

    Hi, great video.
    I have one question.
    Try to run your project and get one issue during user login.
    Firstly, I see that user in DB save as 'username' even if I'm using 'user'.
    Second one during the login I retrieve 401 and log message o.s.s.a.dao.DaoAuthenticationProvider : Failed to authenticate since password does not match stored value.

    • @hamzaechchabli8132
      @hamzaechchabli8132 Рік тому

      if you used table that already exist in the database try to remove "username" directly from the database and rerun the app , if not check you model it may have both "user" and "username"

    • @codingwmax
      @codingwmax  Рік тому

      Thank you.
      Not sure about the issues without seeing the code, do you have the code available on GitHub?

    • @schrodinger_s_cat_theory
      @schrodinger_s_cat_theory Рік тому

      I didn't make any changes to the logic other than DB. Used PostgreSQL instead of MongoDb, the code is completely your implementation.
      I'll try to debug the app and leave comment as soon as I find the reason.
      Anyway, thanks for the video and the answer. Good job!)

  • @Sandeep-Gurjar-0
    @Sandeep-Gurjar-0 Рік тому

    @Max I think you don't like my question as you have removed from here. is any specific reason you removed?

    • @codingwmax
      @codingwmax  Рік тому

      Hello, I haven't removed any comments so not sure what happened to your comment. Anyway, according to the docs I believe there is an example config of an authorization server + resource server in one project: docs.spring.io/spring-authorization-server/docs/current/reference/html/getting-started.html, hope that helps

  • @tuantrinh9979
    @tuantrinh9979 Рік тому

    so OAuth2 with monitoring using Prometheus & Grafana, please!

  • @ram0973
    @ram0973 Рік тому

    Great video, thanks. But why we just can't use Spring sessions in database or Redis, and http-only same-site secure session cookie ? If JWT is stolen, nothing will stop hacker in the access token lifetime period. And with sessions we can delete all user sessions. I know REST is known as "stateless". But it's not, if we want to authorize a user.

    • @codingwmax
      @codingwmax  Рік тому

      Thank you!
      So if you want stateless authentication you'd go for using a JWT. But if you want stateful authentication then you'd need to store session information somewhere. Each option has its own advantages and disadvantages, and the choice really depends on your use case. For example if you want to limit the number of sessions a user can have and your web app is in the browser only, you can use cookies and store session information somewhere in order to detect multiple sessions (stateful). But if you don't care about the number of sessions and you web app is a mobile app, and want to have long lived sessions, then you'd go for a stateless authentication using JWTs.

  • @FadiQua
    @FadiQua 2 роки тому

    Any idea how can we integrate this with spring oauth2 authorization server?

    • @codingwmax
      @codingwmax  2 роки тому

      The authorization server is used for third party authorization, think "login with Facebook, login with google, etc." where other applications want to login with "your app". You still want to have a way for users to login to your app directly. If you want to add an authorisation server you'd want to configure it to use the same user's database table for authentication source. For issuing and verifying JWT's the authorisation sever does that by itself when calling the right endpoints.

  • @ederillo97
    @ederillo97 Рік тому

    Many thanks for this helpful tutorial!! Is ok to use a resource server to authenticate users? what is the correct way to check if the user exists in the db when spring is authenticating it? How can I disable authorization for certain endpoints?? with endpoints with .permitAll() if I set the header authentication for that endopoints, spring rejects me saying "Bad authentication" but that endpoints should be with the authorization check disabled. Hope you can help me! Thanks

    • @codingwmax
      @codingwmax  Рік тому

      You're welcome.
      Yes it's ok to use the resource server to authenticate users if you have the necessary endpoints for authentication.
      As long as you provide spring with a DaoAuthenticationProvider linking to your user service, spring will use the user service to look up the user in the db when authenticating.
      For the header issue that is due to the BearerTokenAuthenticationFilter, which looks for the authorization header in the request, if present it will try and authenticate even if the endpoint is not secure, to resolve the issue you will need to ensure the client doesn't set the authorization header for endpoints that are not secure.

  • @Fernando-mg7rx
    @Fernando-mg7rx Рік тому

    or any path to add the /logout endpoint?

    • @codingwmax
      @codingwmax  Рік тому

      When using stateless authentication like JWT, there is no session information stored in the database, therefore there is no logout operation to perform, the JWT (access token) will simply expire after a short time for example 5 minutes, afterwards that JWT will fail validation and REST API calls will fail with authentication error. Now you could store some information related to the session in the db but that kind of defeats the purpose of using JWTs. From the client side perspective, when a user clicks log out, you can clear the tokens from the local storage.

  • @youssouphafaye1710
    @youssouphafaye1710 2 роки тому

    Hello, thank you very much for the video you're doing a great work. im trying to implement and oauth server where a randomly generate string is used by user and saved in the database when user login. i wanna use a manytoone to get the correct user and check if the token is expired , if the user is enable and if the have the correct roles (authority ) . how can i do that thank you in advance for the answer?

    • @codingwmax
      @codingwmax  2 роки тому

      where is the randomly generated string used by the user when logging in coming from? Are you trying to implement something like a OTP (one time passcode) or one time login code for multi factor authentication?

  • @АлександрБугримов-о1е

    Super !))

  • @zhenobiikuzo4957
    @zhenobiikuzo4957 2 роки тому

    Do you have a discord server? Also I need help running the repository.
    I tried to run docker and it runs the mongodb server. Then I build and start the project but got the following error.
    Error creating bean with name 'tokenGenerator'. I'm still new to spring boot and would appreciate some assistance to this error.

    • @codingwmax
      @codingwmax  2 роки тому +1

      Hello, in the repo, the default active profile is "prod" and the application expects to be given the keys to use for token generation. Change the active profile to "dev" so that the application generates the keys it needs. I also updated the code in GitHub to have "dev" as the default active profile.

    • @zhenobiikuzo4957
      @zhenobiikuzo4957 2 роки тому

      @@codingwmax Sorry for very late reply I haven't noticed the respond. I did that and managed to run mongodb perfectly at docker. But got the following problem on post request.
      No server chosen by com.mongodb.client.internal.MongoClientDelegate$1@74c1d86f from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[]}. Waiting for 30000 ms before timing out
      also do you have plan to try it on postgresql?

    • @codingwmax
      @codingwmax  2 роки тому

      The error means the database can’t be reached. Check the docket container logs to ensure the database started correctly. I don’t have plans currently to do this with postgres, maybe in the future.

  • @ramin2881
    @ramin2881 Рік тому

    Hi Max, thanks for your videos.
    Question is if an IOS/android app want to use this server, does this security implementation works fine or we need another implementation?

    • @codingwmax
      @codingwmax  Рік тому

      Yes this would absolutely work as a backend for a mobile application.

    • @ramin2881
      @ramin2881 Рік тому

      @@codingwmax in Android apps once user logs in he is logged in for ever!! Whats happening behind the scene?

    • @maxim-antonov
      @maxim-antonov Рік тому +1

      @@ramin2881 it would be the same for iOS. What happens behind the scenes is that your mobile api client automatically gets a new access token when it expires. But for the refresh token you’d have to refresh it for example 1 week or 1 month before expiration, because once it expires you can’t refresh it anymore. This gives the impression that you are logged in forever. But if the user is inactive and the refresh token expired, the user would have to login again.

  • @KenAgudelo
    @KenAgudelo 2 роки тому

    Excellent, great explanation... please can you do it with JWT?

    • @codingwmax
      @codingwmax  2 роки тому

      Hello, thank you. Can you please explain what you mean? We are using jwt tokens in this video for both access tokens and refresh tokens.

    • @KenAgudelo
      @KenAgudelo 2 роки тому

      @@codingwmax sorry wrong video, my bad

    • @codingwmax
      @codingwmax  2 роки тому

      No problem 👍

  • @tami-he4mm
    @tami-he4mm Рік тому

    sound is quiet, nothing changes when i turn the sound up, make it louder next time please

  • @luv-nnd
    @luv-nnd Рік тому

    Why can we enable CSRF?

    • @codingwmax
      @codingwmax  Рік тому +1

      I believe you are asking why is CSRF disabled?
      CSRF attacks depend on browser auto authentication, e.g. when using cookies, the browser automatically authenticates requests, this is susceptible to CSRF attacks. In this video we are using JWT authentication via an "Authorization" header, which must be explicitly set for requests, the browser doesn't do this automatically and therefore we don't need CSRF protection.

  • @ah_ay_
    @ah_ay_ Рік тому

    Thanks a lot for this very good video. One question please: is it possible to use this authentication mechanism and combine it with Google, facebook etc ? thanks in advance

    • @codingwmax
      @codingwmax  Рік тому

      I believe it's possible yes but you may need to do a lot of wiring things together. To leverage third party logins I believe it's best to use an authorization server.

  • @kpranay9710
    @kpranay9710 Рік тому

    Can u do one video email implementation jwt using spring boot

    • @codingwmax
      @codingwmax  Рік тому

      Hello, could you please explain a bit more the email part? Thanks

  • @berkaysalih188
    @berkaysalih188 2 роки тому

    hello, I watched all your videos, thank you very much for the information. I'm trying to do user update but I can't. Can you help me write updateuser?

    • @codingwmax
      @codingwmax  2 роки тому +1

      Hello, here is some code I threw together, it's untested but I hope you get the idea (note: you need to add a new dto: UserUpdateDTO):
      @PostMapping("/{id}")
      @PreAuthorize("#user.id == #id")
      public ResponseEntity user(@AuthenticationPrincipal User user, @PathVariable String id, @RequestBody UserUpdateDTO dto) {
      User retrievedUser = userRepository.findById(id).orElseThrow();
      retrievedUser.setUsername(dto.getUsername());
      userRepository.save(retrievedUser);
      return ResponseEntity.ok(UserDTO.from(retrievedUser));
      }

    • @berkaysalih188
      @berkaysalih188 2 роки тому

      @@codingwmax Thank you sir, I solved it with a similar code.

  • @locthiennguyen
    @locthiennguyen 11 місяців тому

    sir, i got 2 errors, both of them are java.io.FileNotFoundException:
    "access-refresh-token-keys\access-token-private.key" (The filename, directory name, or volume label syntax is incorrect)
    "access-refresh-token-keys\access-token-public.key" (The filename, directory name, or volume label syntax is incorrect)
    also i got question regarding those 4 file path you create in properties file, what is its uses? to store the key like a DB? it's still fuzzy at that point sir