OAuth Client for Java: How to Choose and Use
OAuth (Open Authorization) is an open protocol that allows a third party (e.g., your Java application) to be granted limited access to a user's resources without sharing their login and password. In modern web development, OAuth 2.0 is the de facto standard for authorization via social networks (Google, Facebook, VK) and corporate systems (Microsoft, GitHub).
For Java, there are several mature libraries that implement both the OAuth client and server roles. In this article, we will look at three of the most popular solutions: ScribeJava, Spring Security OAuth, and Google OAuth Client Library for Java. You will learn how to add them to your project, write a basic integration, and choose the right tool for your tasks.
1. ScribeJava — Lightweight OAuth Client
ScribeJava is a simple and lightweight library that supports OAuth 1.0a and OAuth 2.0. It is ideal for standalone applications or microservices where the full Spring stack is not required.
Adding Dependency (Maven)
<dependency> <groupId>com.github.scribejava</groupId> <artifactId>scribejava-apis</artifactId> <version>8.3.3</version></dependency>Example: Login via Google
import com.github.scribejava.apis.GoogleApi20;import com.github.scribejava.core.builder.ServiceBuilder;import com.github.scribejava.core.model.*;import com.github.scribejava.core.oauth.OAuth20Service;
public class GoogleOAuthExample { public static void main(String[] args) throws Exception { String clientId = "YOUR_CLIENT_ID"; String clientSecret = "YOUR_CLIENT_SECRET"; String callbackUrl = "http://localhost:8080/callback";
OAuth20Service service = new ServiceBuilder(clientId) .apiSecret(clientSecret) .callback(callbackUrl) .scope("email profile") .build(GoogleApi20.instance());
// Get the URL for user redirect String authorizationUrl = service.getAuthorizationUrl(); System.out.println("Open in browser: " + authorizationUrl);
// After redirect, get the authorization code (in a real app from QueryString) String code = "CODE_FROM_REDIRECT";
// Exchange the code for a token OAuth2AccessToken accessToken = service.getAccessToken(code); System.out.println("Access Token: " + accessToken.getAccessToken());
// Use the token to request user data OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v2/userinfo"); service.signRequest(accessToken, request); Response response = service.execute(request); System.out.println("API Response: " + response.getBody()); }}Advantages of ScribeJava:
- Minimal dependencies (only OkHttp or URLConnection).
- Support for dozens of providers out of the box (Google, Facebook, GitHub, VK, etc.).
- Simple API without complex abstractions.
2. Spring Security OAuth — for Spring Boot Projects
If you are using Spring Boot, Spring Security OAuth (the spring-security-oauth2-client module) is the most natural choice. It fully integrates with the Security chain and allows configuring providers via application.yml.
Adding Dependency (Gradle)
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'Example Configuration (application.yml)
spring: security: oauth2: client: registration: Also in library
AddressSanitizer for C++: Complete Guide with Examples
Kotlin Security Crypto: Complete Guide with Code Examples
Working with YAML in C# (.NET): Libraries and Usage Examples
OAuth client for Rust: oauth2-rs library