You can implement the OAuth provider service in Java on your server side, and then use it on the client side as the authentication. This authentication system (OAuth) is what you are looking for (or are thinking about).
The service is very much simple and straight-forward, and the standard explains how to do that. If you are willing to read about that standard, please go here, http://tools.ietf.org/html/rfc5849
]. I don't do that much Java programming, but here you go:
Is there an OAuth 2.0 Provider implementation in Java? (not oauth client) - Stack Overflow
Code — OAuth
] (Have a look at the Java section)
GitHub - OAuth-Apis/apis: OAuth Authorization as a Service
The GitHub samples provide the samples for both, server as well as client. You can download the source code and run them as you want.