Watch Me Code an OAuth2 Google Login Client with Java Spring

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 มิ.ย. 2022
  • In this video you will get to see me set up a new Java Spring project using Spring Initialzr, Google Credentials Console, and walking through the Spring Docs.
    Spring Initialzr:
    start.spring.io/
    Google Credentials Console:
    console.cloud.google.com/apis...
    Spring Docs:
    docs.spring.io/spring-securit...
    #java #spring #oauth2
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 55

  • @morngrim2
    @morngrim2 11 หลายเดือนก่อน +3

    hi! this type of tutorials are amazing :) definitely better than skipping and showing copy-paste code of any app :) helps people to get to know how to learn instead of just how to solve particular problem.
    subbed for future :)

  • @88FEATEATEEFFEEE
    @88FEATEATEEFFEEE ปีที่แล้ว +4

    Super helpful how you explain how to find stuff. I wish every tutorial was like this

  • @geosystems2106
    @geosystems2106 11 หลายเดือนก่อน +3

    Immediately subscribed ❤❤ I hate these "typer" videos without explaining where and why and how, this is very much like a proper engineer style ! Well done 👍👍👍👍✔✔

  • @xeanluxcrille6847
    @xeanluxcrille6847 ปีที่แล้ว +3

    Hello, thank you for this! And yes, this is better, at least for me since it can help me learn extensively when I explore the materials by myself afterwards. Even just pointing out where to reference which stuff is helpful enough. Once again, thanks a lot!

  • @gangsterism
    @gangsterism ปีที่แล้ว +3

    great tutorial, best one on this topic. took too long to find

  • @bhdr111
    @bhdr111 ปีที่แล้ว +1

    Thank you very much. This was very helpful.

  • @MorbidMicke
    @MorbidMicke 3 หลายเดือนก่อน

    Thank you very much!

  • @user-eb5tv1vq2d
    @user-eb5tv1vq2d ปีที่แล้ว +1

    Great video, Thank you so much

  • @matadorxp1
    @matadorxp1 5 หลายเดือนก่อน

    You are the best

  • @conganhphung1488
    @conganhphung1488 3 หลายเดือนก่อน +1

    It's helpful. Thank you!
    But how we can get access token and use it in future ?
    example: use for google API driver

  • @artur4653
    @artur4653 6 หลายเดือนก่อน

    Hi, thanks a lot for your tutorial, you helped me a lot. Greetings from Ukarine :)

  • @MrSdsc81
    @MrSdsc81 ปีที่แล้ว +3

    Thanks. It is a good tutorial. I would recommend to increase the font size for recording the video, is very difficult to see the code, even on a monitor (dont even try to do it on my phone). Also I've got a question, now that you got the info, what should be the next steps?

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว +2

      There are multiple things you can do with the attributes, but one example is that you can use the "sub" property as a unique identifier for the user if you want to save user preferences or other actions to the database with that ID.

  • @stonedcodingtom9097
    @stonedcodingtom9097 ปีที่แล้ว

    Nice tutorial, thx!

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว +1

      You're welcome!

    • @stonedcodingtom9097
      @stonedcodingtom9097 ปีที่แล้ว

      @@TechTutorRyan Are u planning to create tutorial for oauth2 without spring? I mean oauth2 just for desktop java app.

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว

      @@stonedcodingtom9097 I haven't really thought about it. Spring is a fairly well maintained open source community so I typically don't bother to recreate features that it already provides.
      Similarly, in JavaScript I would have probably used Passport.js for Google Auth:
      www.passportjs.org/concepts/authentication/google/

    • @stonedcodingtom9097
      @stonedcodingtom9097 ปีที่แล้ว

      @@TechTutorRyan Okay, thank you for reply. Have a nice day!

  • @andy_yang924
    @andy_yang924 ปีที่แล้ว +1

    Hi, thank you for the tutorial. Just a question, how to get the token value in Principal? I couldn't find the method in OAuth2AuthenticationToken

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว +1

      I would need to read some of the open source code to see where they are stashing it. However, Java Spring should be managing the token for you and determining when the session has expired. Do you need it for something else?

  • @celtarebaixado3301
    @celtarebaixado3301 ปีที่แล้ว

    Hi Ryan, thanks for making this tutorial, it was very helpful. One thing I would like to do is create a working logout button, do you know how to do this correctly?

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว

      Hello! I just created a new video showing how to logout. I didn't create a button for it, but it would be very simple. All you need to do is create a button that leads the user to "{{siteURL}}/logout".
      Here is the new video for reference:
      th-cam.com/video/OtFLR-MReI0/w-d-xo.html

  • @bennettyibawenifiri6671
    @bennettyibawenifiri6671 ปีที่แล้ว

    Hello @Tech Tutor , From your implementation how can I further call the Endpoint in Postman or Browser to get the User detail from google and save it automatically into my database for future login?

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว +1

      In the controller you can take that Map object that is coming from getAttributes and save it using a repository layer. For best practice, I would suggest autowiring in a Service to your controller and then autowiring a Repository class into your Service to perform the save. There are some other videos on my channel that show how to easily add a database to a Spring Boot project.

  • @jayarredondo8387
    @jayarredondo8387 ปีที่แล้ว +1

    Thank you for the tutorial! I'm building an application that has Users stored in my database with specific UserRoles, and I want to be able to authenticate with google to require the access token for each request. How would I go about assigning these authorities to specific users in my database based on the UserRole?

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว +1

      You could store the user information that you get back from Google and then use that to fetch a role that you assign the user that you would store in your database.

  • @geosystems2106
    @geosystems2106 11 หลายเดือนก่อน

    One question though, once the authentication is done, how do you manage authorizations of users ? google and facebook (let's say) probably do not have the same GrantedAuthorities in their models. *a)* you need a database of your own to CRUD authorizations _or_ *b)* you write data to google or facebook's own user repositories so that at login time they are given back to you. Any comments ?

    • @TechTutorRyan
      @TechTutorRyan  11 หลายเดือนก่อน

      I haven't dug into this recently, but the last time that I looked into it, you still had to manage your own roles. For my implementation this involved using my own database as you already assumed and then checking to see which role the user had after Google confirmed the user identity.

  • @nezyha
    @nezyha 9 หลายเดือนก่อน +1

    Hey, I've setup OAuth 2 with google on the backend, However i'm so confused on how can we access restricted endpoints on the front end. I can authenticate in spring but i believe there's a gap in knowledge of how can our frontend app redirect to this backend login and once it's authorized get the protected endpoint data?

    • @shreyasmedade7154
      @shreyasmedade7154 3 หลายเดือนก่อน

      Got any solution?

    • @nezyha
      @nezyha 3 หลายเดือนก่อน

      Yeah I started using .net 😅

  • @amarildo-xyz
    @amarildo-xyz 2 ปีที่แล้ว +1

    Do you know how can I manually redirect the request? I have only a specific case to redirect to an AuthProvider (many if elses), so I don't want Spring/SpringSecurity to handle the auto-redirect.

    • @TechTutorRyan
      @TechTutorRyan  2 ปีที่แล้ว

      Are you using the same Maven dependency that I used in the video? If you are, then the code from that dependency uses Spring Security, so I would highly suggest using Spring Security to configure any security chains.

    • @amarildo-xyz
      @amarildo-xyz 2 ปีที่แล้ว +1

      ​@@TechTutorRyan yes I'm using Spring Security, also not limited on using any external library.
      I have one @PostMapping("/login"), which does multiple (different) authentications (2 databases, 1 SOAP request), and the last case (depending on user's configuration on previous database checks) might be an OpenID redirect to ADFS. Currently I handle the flow by calling different service classes, but I'm stuck at the ADFS redirection/authentication step. I don't know if this is achievable using Spring Security filters :/

    • @TechTutorRyan
      @TechTutorRyan  2 ปีที่แล้ว

      If I am understanding your question, it sounds like you want to redirect based on the authentication that was used? I think you may be able to do something like this:
      @RequestMapping("/")
      public String page() {
      Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      if (auth instanceof SomeAuthenticationToken)
      return "someAuthPage";
      if (auth instanceof SomeOtherAuthenticationToken)
      return "someOtherAuthPage";
      }

    • @TechTutorRyan
      @TechTutorRyan  2 ปีที่แล้ว +1

      You may also be able to set a specific redirect for each auth method using defaultSuccessUrl:
      docs.spring.io/spring-security/site/docs/4.0.x/apidocs/org/springframework/security/config/annotation/web/configurers/AbstractAuthenticationFilterConfigurer.html#defaultSuccessUrl-java.lang.String-

    • @amarildo-xyz
      @amarildo-xyz 2 ปีที่แล้ว +1

      @@TechTutorRyan actually it is much simpler/primitive, the following 'pseudocode' describes the workflow:
      @PostMapping("/")
      public Boolean login(@RequestBody Login login) {
      if(login.getProperty1() == xx) //internal auth
      return isFirstAuthOk(login);
      var sourceDb2 = db2GetInfo(login.getUsername);
      if(sourceDb2.getProperty2() == xx) //external service auth
      return isAuthUsingSoap(login);
      //if you reached this step, do an ADFS/OpenID redirect
      //todo: open ADFS login page and handle auth/token response
      }
      My initial approach was to use LDAP, which has an "authenticate(username, password)" approach, but it didnt support SSO (Single Sign On) which is the next step :(

  • @HuyNobles
    @HuyNobles ปีที่แล้ว

    Can I get the Git Link?

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว

      I didn't push this one to my git. It actually isn't that much code though. If you use follow along in the video and use Spring Initializr, it will basically generate all of the code you need except for the changes to the application.yml file, which you will still need to change to match your Google account. Let me know if you have any trouble after trying the steps in the video.

  • @tkrdata9621
    @tkrdata9621 ปีที่แล้ว

    Helllo, Why ? -> Invalid Redirect: must contain a domain

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว

      It sounds like you may have missed a step configuring your Google Settings.

  • @phatle-248
    @phatle-248 ปีที่แล้ว

    Hello, thank you for your video. I have this problem: antMatchers in my security config block redirecting to google login, how can I solve this?
    code:
    .antMatchers("/api/login","/api/register","/").permitAll()
    .antMatchers(HttpMethod.GET, "/api/v1/teachers").hasAuthority("admin") //block google login

    • @sultanyasin8952
      @sultanyasin8952 ปีที่แล้ว

      make a new class, say AuthHandlar:
      import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
      import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
      public class AuthHandlar extends SimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
      public AuthHandlar(){
      super();
      setUseReferer(false); // do not redirect user to same endpoint when request occurs
      }
      }
      Then in in securityConfigin class do the following ->
      httpSecurity.oauth2Login()
      .successHandler(new AuthHandlar()).and()....etc

    • @aryayellapu9952
      @aryayellapu9952 ปีที่แล้ว

      Try using this
      @Configuration
      @EnableWebSecurity
      public class AppConfig extends WebSecurityConfigurerAdapter{
      @Override
      protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable().authorizeRequests().antMatchers("/endpoint name").permitAll() //endpoints where you want to disable security
      .anyRequest().authenticated()
      .and()
      .oauth2Login().permitAll();
      }
      }

  • @FellTheSky
    @FellTheSky ปีที่แล้ว

    I have a similar method to yours:
    @GetMapping("/auth")
    public Map loginTest(OAuth2AuthenticationToken authentication) {
    return authentication.getPrincipal().getAttributes();
    }
    Setting spring.security.oauth2.client.registration.google.redirect-uri=localhost:8080/oauth2/callback/google property
    and localhost:8080/oauth2/callback/google in the API console uri redirect.
    And was not able to pass the login. I see the google form, but when I log in, I get the next message:
    "Error 400: redirect_uri_mismatch"

    • @karthiklv29
      @karthiklv29 ปีที่แล้ว +1

      /auth is the issue

    • @TechTutorRyan
      @TechTutorRyan  ปีที่แล้ว

      Yes, I agree with Karthik, /auth is likely the issue without being able to see the rest of the code.

    • @FellTheSky
      @FellTheSky ปีที่แล้ว

      @@TechTutorRyan The issue was: The redirect URI was blocked by spring boot as an URI that required authentication. So that would redirect into the login, and there was the endless loop.
      I had to create a configuration class with the following:
      http.authorizeRequests()
      .anyRequest().authenticated()
      .and()
      .oauth2Login();
      And now its working. Thanks anyways.

    • @EM1LJAN_
      @EM1LJAN_ ปีที่แล้ว

      @@FellTheSky that updating the google api console and that change being present can take some time. Generally only a few minutes but sometimes it seems longer.