Securing Java Microservices with OAuth2.0: Integrating Keycloak with Windows AD
André Ramos
Senior Software Engineer | Java | Spring Boot | Micro Services | Fullstack Software Developer | Angular | AWS | TechLead
Introduction
Securing Java microservices is a critical aspect of modern software development. With OAuth2.0 as a widely accepted authentication standard, Keycloak simplifies identity and access management by providing a centralized authentication server. When integrated with Windows Active Directory (AD), organizations can leverage existing user credentials for seamless authentication and Single Sign-On (SSO).
This guide walks you through:
1. Deploying Keycloak
To integrate Keycloak with Windows AD, you first need a running Keycloak instance. The simplest way to do this is using Docker.
1.1 Running Keycloak in a Docker Container
docker run -d -p 8080:8080 --name keycloak \
-e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin \
quay.io/keycloak/keycloak start-dev
Once the container is running, access the Keycloak admin console at: https://localhost:8080/admin
Default credentials:
1.2 Creating a New Realm
A realm is an isolated space where users, roles, and clients are managed.
2. Connecting Keycloak to Windows AD
To allow authentication via Windows AD, configure Keycloak’s User Federation.
2.1 Adding an LDAP Provider
Once configured, users from AD can log in using their domain credentials.
3. Configuring OAuth2.0 in Java Microservices
Now, configure a Java microservice to validate JWT tokens issued by Keycloak.
3.1 Adding Dependencies
In pom.xml, add:
3.2 Configuring OAuth2.0 in Spring Boot
Modify application.yml to integrate with Keycloak:
3.3 Securing Endpoints with Roles
Use @PreAuthorize to control access:
4.2 Accessing a Secured Endpoint
curl -X POST https://localhost:8080/realms/company-realm/protocol/openid-connect/token \
-d "client_id=myclient" -d "username=myuser" -d "password=mypassword" \
-d "grant_type=password" -d "client_secret=mysecret"
Successful response:
4.2 Accessing a Secured Endpoint
Pass the token in an API request:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://localhost:8081/secure
Expected response:
5. Handling Token Expiration
Since access tokens expire, they must be refreshed periodically.
To refresh a token:
curl -X POST https://localhost:8080/realms/company-realm/protocol/openid-connect/token \
-d "grant_type=refresh_token" -d "client_id=myclient" \
-d "refresh_token=YOUR_REFRESH_TOKEN" -d "client_secret=mysecret"
To refresh a token:
curl -X POST https://localhost:8080/realms/company-realm/protocol/openid-connect/token \
-d "grant_type=refresh_token" -d "client_id=myclient" \
-d "refresh_token=YOUR_REFRESH_TOKEN" -d "client_secret=mysecret"
Conclusion
By integrating Keycloak with Windows AD, you create a centralized authentication system that enhances security and simplifies user management. Implementing OAuth2.0 ensures that Java microservices remain secure while providing seamless authentication for users.
Benefits of This Approach:
Software Engineer | Java | AWS Cloud | Spring Boot | Microservices | Kafka | REST APIs | CI/CD
1 周thats an amazing topic to talk about. I used keycloak in 2022 and it speeds up tons of engineering effort
Dynamic Technology Leader | Innovator in .NET Development and Cloud Solutions
1 周Useful tips
Great guide on securing microservices! Keycloak + OAuth2.0 + AD is a powerful combo for centralized authentication. Definitely a must read!
Senior React Developer | Full Stack Developer | JavaScript | TypeScript | Node.js
2 周Nice, thanks for sharing !
Desenvolvedor Full stack | HTML, CSS, JavaScript, React | Node.js | Git & Github | Python
2 周Great post ????