Adding Google Sign-In to Spring Social

I wanted to add an OAuth Provider Sign-in to NixMash Spring that wasn’t baked in to Spring Social and see what was involved. As you’ll see in the case of adding Google, it is very little. We’ll be using Gabriel Axel’s Spring Social Google project (github) with documentation found here.

We’ll start in Gradle where we add the spring-social-google library dependency.

In our class we add the Google Connection Factory. You may recall from an earlier post, we’re retrieving our Social Provider Application IDs and Secret Keys from an external properties file.

And that’s pretty much it for our source code updates. With Spring Social already in place and our UserConnection object containing ProviderId and other properties, we can leverage existing authentication logic and much of our HTML for profile pics and various social tags like the Google indicators below.

Speaking of HTML, here is our Google Button HTML. Notice the scope values requiring the urls.

The Google Application Setup is straightforward. Authorized redirect URIs will include your server urls with /signin/google, an example being http://localhost:9000/signin/google. There’s one other point to remember when setting up the app in Google Developer. Enable the Google+ API. If not enabled you’ll return to your site login page with a generic error and little clue of what’s wrong.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.