DEV Community

alawode Samuel Tolulope
alawode Samuel Tolulope

Posted on

Working with Spring Security Oauth

The last backend task I really struggled with was integrating Oauth in a java springboot application.
You know how you want to sign up on an application and you see the option where you can login with google, or facebook, or github yeah? Yes, that was what I was trying to achieve. Funny enough, Spring boot has quite the robust library for that.
However, my issue what that I was trying to customize it to my need as much as possible. What was my need?
A little background about the application.
Security on this app was boostrapped with Spring-Security, with JWT security for the endpoints. Specifically, the Oauth application I was trying to use in Google’s, and as with most, I needed to configure a redirect link for when authentication was successful on google’e end. Naturally, I thought to route it to the app’s homepage. had it routed to the app’s homepage.
However, from the homepage, I needed to make a call to backend, to an endpoint that required token access. But, since I have outsourced the authentication process to Google, I could not possibly give an access to the user intending to login, since they are not doing so with my backend’s /login endpoint.
How did I resolve this? Well, I had to do most of the heavy lifting on the frontend. Soon as the user successfully verifies with google, it routes to a redirect page that makes a backend call, checking if the user’s mail (which I retrieved from Google’s Oauth BioData), has already been registered in my DB. If yes, I assigned a token, and then automatically redirect to frontend, where they can now continue business as normal. Otherwise, they are forwarded to a sign up page - where they also have the option of speeding things up with Google oauth.

I kid you not, I spent days on this. I was amateur, and most of the spring material I was trying to use didnt account for situations where the dev might want to customize the oauth flow to suit specific use cases.

Well, I have recently just started the HNG internship, and I hope I come out of it a much better problem solver. Looking forward to doing dope things, with the other brilliant people. If a career in tech interests you in any way, you can look out for them here.

Top comments (0)