Client Configuration¶
This guide explains how to configure the authentication-test-api client in Keycloak for OAuth 2.0 authentication.
What is a Client?¶
In Keycloak, a client represents an application that wants to use Keycloak for authentication. For our API, the client configuration defines:
- How the API validates JWT tokens
- Which authentication flows are allowed
- Token settings and lifespans
- Valid redirect URIs
Prerequisites¶
- Keycloak installed and running
secure-testrealm created- Access to Keycloak Admin Console
Step 1: Create Client¶
- Select "secure-test" realm from dropdown
- Go to "Clients" in left menu
- Click "Create client"
- Enter Client Details:
- Client type:
OpenID Connect - Client ID:
authentication-test-api
- Client type:
-
Click "Next"
-
Configure Capability:
- Client authentication: ✓ ON (confidential client)
- Authorization: ✗ OFF
- Authentication flow:
- ✓ Standard flow
- ✓ Direct access grants
- ✗ Implicit flow
- ✗ Service accounts roles
-
Click "Next"
-
Configure Login Settings:
- Root URL:
http://localhost:9080 - Home URL:
http://localhost:9080 - Valid redirect URIs:
http://localhost:9080/*https://localhost:9443/*- Valid post logout redirect URIs:
+(use root URL) - Web origins:
http://localhost:9080https://localhost:9443
- Root URL:
- Click "Save"
# Get admin token
TOKEN=$(curl -X POST http://localhost:8080/realms/master/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d "password=admin" \
-d "grant_type=password" \
-d "client_id=admin-cli" \
| jq -r '.access_token')
# Create client
curl -X POST http://localhost:8080/admin/realms/secure-test/clients \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"clientId": "authentication-test-api",
"name": "Authentication Test API",
"description": "RESTful API with OAuth 2.0 authentication",
"enabled": true,
"clientAuthenticatorType": "client-secret",
"redirectUris": [
"http://localhost:9080/*",
"https://localhost:9443/*"
],
"webOrigins": [
"http://localhost:9080",
"https://localhost:9443"
],
"publicClient": false,
"protocol": "openid-connect",
"standardFlowEnabled": true,
"directAccessGrantsEnabled": true,
"serviceAccountsEnabled": false
}'
Step 2: Get Client Secret¶
The client secret is required for the API to validate tokens.
- Go to "Clients"
- Click "authentication-test-api"
- Go to "Credentials" tab
- Copy the "Client secret"
Save this secret - you'll need it for the API configuration.
# Get client secret
curl -X GET "http://localhost:8080/admin/realms/secure-test/clients/$(curl -X GET http://localhost:8080/admin/realms/secure-test/clients \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.[] | select(.clientId=="authentication-test-api") | .id')/client-secret" \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.value'
Step 3: Configure Client Settings¶
Settings Tab¶
- Go to "Clients" → "authentication-test-api" → "Settings"
- Configure:
- Client ID:
authentication-test-api(read-only) - Name:
Authentication Test API - Description:
RESTful API with OAuth 2.0 authentication - Always display in console: ✗
- Consent required: ✗
- Login theme: (leave empty)
- Client authentication: ✓ ON
-
Authorization: ✗ OFF
-
Authentication flow:
- ✓ Standard flow (Authorization Code)
- ✓ Direct access grants (Resource Owner Password)
- ✗ Implicit flow
- ✗ Service accounts roles
-
✗ OAuth 2.0 Device Authorization Grant
-
Click "Save"
Access Settings¶
- Still in "Settings" tab, scroll to "Access settings"
- Configure:
- Root URL:
http://localhost:9080 - Home URL:
http://localhost:9080 - Valid redirect URIs:
- Valid post logout redirect URIs:
+ - Web origins:
-
Admin URL: (leave empty)
-
Click "Save"
Advanced Settings¶
- Scroll to "Advanced" section
- Configure Token Settings:
- Access Token Lifespan: (leave empty to use realm default)
- Client Session Idle: (leave empty)
- Client Session Max: (leave empty)
- Client Offline Session Idle: (leave empty)
-
Client Offline Session Max: (leave empty)
-
OAuth 2.0 Mutual TLS Certificate Bound Access Tokens: ✗
- OIDC CIBA Grant: ✗
- Click "Save"
Step 4: Configure Roles¶
Map the realm role to the client.
Client Roles Mapping¶
- Go to "Clients" → "authentication-test-api" → "Client scopes"
- Click "authentication-test-api-dedicated"
- Go to "Mappers" tab
- Click "Add mapper" → "By configuration"
- Select "User Realm Role"
- Configure:
- Name:
realm-roles - Mapper Type:
User Realm Role - Multivalued: ✓ ON
- Token Claim Name:
realm_access.roles - Claim JSON Type:
String - Add to ID token: ✓ ON
- Add to access token: ✓ ON
-
Add to userinfo: ✓ ON
-
Click "Save"
This ensures the schedule-user role appears in the JWT token.
Testing Client Configuration
To test the client configuration with actual tokens, you'll need to create users first. See the User Management guide, then return to test token generation.
Step 5: Configure API Environment¶
Update your API configuration with the client details:
Environment Variables¶
export JWT_JWKS_URI=http://localhost:8080/realms/secure-test/protocol/openid-connect/certs
export JWT_ISSUER=http://localhost:8080/realms/secure-test
export KEYCLOAK_CLIENT_ID=authentication-test-api
export KEYCLOAK_CLIENT_SECRET=YOUR_CLIENT_SECRET
MicroProfile Config¶
Update src/main/resources/META-INF/microprofile-config.properties:
mp.jwt.verify.publickey.location=http://localhost:8080/realms/secure-test/protocol/openid-connect/certs
mp.jwt.verify.issuer=http://localhost:8080/realms/secure-test
keycloak.client.id=authentication-test-api
Advanced Configuration¶
CORS Configuration¶
If your API needs to handle CORS:
- Go to "Clients" → "authentication-test-api" → "Settings"
- Scroll to "Web origins"
- Add allowed origins:
- Click "Save"
Token Lifespan¶
Customize token lifespan for this client:
- Go to "Clients" → "authentication-test-api" → "Advanced" tab
- Set "Access Token Lifespan":
10 Minutes - Click "Save"
Audience¶
Add audience claim to tokens:
- Go to "Clients" → "authentication-test-api" → "Client scopes"
- Click "authentication-test-api-dedicated"
- Go to "Mappers" tab
- Click "Add mapper" → "By configuration"
- Select "Audience"
- Configure:
- Name:
audience - Included Client Audience:
authentication-test-api - Add to ID token: ✗ OFF
- Add to access token: ✓ ON
- Click "Save"
Token Exchange
If you're using token exchange between clients, you'll also need to add an audience mapper to the source client (e.g., frontend) to include the target client (API) in its tokens. See Token Exchange Setup for details.
Troubleshooting¶
Invalid Client Credentials¶
If you get "invalid_client" error:
- Verify client ID is correct
- Check client secret matches
- Ensure client authentication is enabled
Token Missing Roles¶
If JWT token doesn't contain roles:
- Verify realm role mapper is configured
- Check user has the
schedule-userrole - Ensure mapper is added to access token
CORS Errors¶
If you get CORS errors:
- Add frontend origin to "Web origins"
- Ensure origin matches exactly (including protocol and port)
- Check browser console for specific CORS error
Next Steps¶
- User Management - Create test users
- API Authentication - Use tokens with the API
- Troubleshooting - Common issues