Spring boot Oauth2 – Client

Connect an OAuth Client to a server using Spring OAuth2 library is a lightweight way to get information from third part where this protocol is available.
In this post I’ll show you how to make it using few configurations and code files.

As you’ll see, few lines of Java code and configuration are enough to make it work.
Having said that, the question could be: Why did you think that’s worth a dedicated post? Well, I think it’s worth because I’ve spent more time that I did figure to complete the solution and, above all, for the lack of good documentation of the client side.

Spring OAuth2 library is an extension of Spring Security Layer and there’s not official documentation excluding the Api docs. I think that missing discourages the user to adopt this library for their solution, thus, I think of having enough reasons for writing this article so, here we’re!

A point must be made for the userInfoUri. I wrongly thought of this Url as the protected resource where you can put whatever you want.
This was a mistake: this url return the userProfile; you can put other user information (as you can see below in this article I put the user’s birthday) in the Principal details object.

Let me show you what I’m speaking of by using this code of the protected remote resource.

And now some screenshot of the process flow.
1. Start of the request for the protected resource.

2. Check the user credential.

3. Authorize the user to access the protected resource.

4. Get the protected resource.

Concluding, I think of adopting Spring OAuth2 library only when I’m already using Spring Security library. I can’t figure this solution easily adapting at external authentication service despite there are ways which let this process available.

I’m looking forward to seeing the next versions of this library, I think there’s a lot to do again despite of the good level already achieved.