Skip to content

sharanggupta/oauth2product

Repository files navigation

OAuth2 Product

Prerequisites

  • Docker and Docker Compose
  • JDK 17
  • Maven
  • jq (for JSON processing)

Steps to run

  1. Build the application (including executing tests):
./mvnw clean package
  1. Start the services:
docker compose up --build -d
  1. Wait for all the services to be healthy (this may take a few minutes):
docker compose ps
  1. Try accessing the products endpoint without authentication (should return 401 Unauthorized):
curl -v -X GET http://localhost:8080/products
  1. Get an access token:
access_token=$(curl -X POST http://localhost:8081/realms/product-realm/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=product-client" \
-d "client_secret=product-secret" \
-d "username=test-user" \
-d "password=password" | jq -r .access_token)
  1. Access the products endpoint:
curl -X GET http://localhost:8080/products \
-H "Authorization: Bearer $access_token"
  1. Test the OAuth2 client flow using a spring OAuth2 client (which handles token management automatically):
# Get products through the OAuth2 client (with automatic token management)
curl -X GET http://localhost:8082/retail/products

This will:

  • Automatically obtain a client credentials token from Keycloak
  • Call the resource server with the token
  • Apply retail markup to prices
  • Return the marked-up product prices

Service URLs

Cleanup

docker-compose down -v

Releases

No releases published

Packages

No packages published