Realm Configuration¶
This guide walks you through creating and configuring the secure-test realm in Keycloak for the Authentication Test API.
What is a Realm?¶
A realm in Keycloak is an isolated space that manages:
- Users and groups
- Roles and permissions
- Clients (applications)
- Authentication settings
Prerequisites¶
- Keycloak installed and running
- Access to Keycloak Admin Console
- Admin credentials
Step 1: Access Admin Console¶
- Open browser:
http://localhost:8080 - Click "Administration Console"
- Login with admin credentials
Step 2: Create Realm¶
- Hover over "Master" in the top-left corner
- Click "Create Realm"
- Enter Realm Details:
- Realm name:
secure-test - Enabled: ✓ (checked)
- Realm name:
- Click "Create"
You should now see "secure-test" in the realm selector.
Create the realm via API:
# 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 realm
curl -X POST http://localhost:8080/admin/realms \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"realm": "secure-test",
"enabled": true,
"displayName": "Secure Test Realm",
"displayNameHtml": "<b>Secure Test</b>"
}'
Step 3: Configure Realm Settings¶
General Settings¶
- Select "secure-test" realm from dropdown
- Go to "Realm settings"
- Configure General tab:
- Display name:
Secure Test Realm - HTML Display name:
<b>Secure Test</b> - Frontend URL: Leave empty for development
-
Require SSL:
none(development) orexternal requests(production) -
Click "Save"
Login Settings¶
- Go to "Realm settings" → "Login" tab
- Configure:
- User registration: ✗ (unchecked for security)
- Forgot password: ✓ (optional)
- Remember me: ✓ (optional)
- Email as username: ✗ (use separate username)
- Login with email: ✓ (allow email login)
- Duplicate emails: ✗ (prevent duplicate emails)
-
Verify email: ✗ (disable for testing)
-
Click "Save"
Token Settings¶
- Go to "Realm settings" → "Tokens" tab
- Configure Token Lifespans:
- Access Token Lifespan:
5 Minutes(default) - Access Token Lifespan For Implicit Flow:
15 Minutes - Client login timeout:
1 Minutes - Login timeout:
30 Minutes -
Login action timeout:
5 Minutes -
Click "Save"
Token Lifespan
For development, you can increase token lifespan to avoid frequent re-authentication. For production, keep tokens short-lived for security.
Email Settings (Optional)¶
For password reset and email verification:
- Go to "Realm settings" → "Email" tab
- Configure SMTP:
- Host:
smtp.gmail.com(or your SMTP server) - Port:
587 - From:
noreply@example.com - Enable SSL: ✗
- Enable StartTLS: ✓
- Enable Authentication: ✓
- Username: Your email
-
Password: Your email password
-
Click "Save"
- Click "Test connection" to verify
Step 4: Create Realm Role¶
Create the schedule-user role required by the API.
- Go to "Realm roles" in left menu
- Click "Create role"
- Enter Role Details:
- Role name:
schedule-user - Description:
Role for accessing schedule API
- Role name:
- Click "Save"
# Get admin token (if not already set)
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 role
curl -X POST http://localhost:8080/admin/realms/secure-test/roles \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "schedule-user",
"description": "Role for accessing schedule API"
}'
Step 5: Configure Security Settings¶
Brute Force Detection¶
Protect against brute force attacks:
- Go to "Realm settings" → "Security defenses" tab
- Configure Brute Force Detection:
- Enabled: ✓
- Permanent Lockout: ✗
- Max Login Failures:
5 - Wait Increment:
60 Seconds - Quick Login Check Milli Seconds:
1000 - Minimum Quick Login Wait:
60 Seconds - Max Wait:
900 Seconds -
Failure Reset Time:
43200 Seconds(12 hours) -
Click "Save"
Password Policy¶
Set password requirements:
- Go to "Realm settings" → "Security defenses" → "Password policy" tab
- Add Policies:
- Minimum Length:
8 - Not Username: ✓
- Uppercase Characters:
1 - Lowercase Characters:
1 - Digits:
1 -
Special Characters:
1 -
Click "Save"
Step 6: Verify Realm Configuration¶
Check Realm Endpoints¶
Get the OpenID Connect configuration:
You should see:
{
"issuer": "http://localhost:8080/realms/secure-test",
"authorization_endpoint": "http://localhost:8080/realms/secure-test/protocol/openid-connect/auth",
"token_endpoint": "http://localhost:8080/realms/secure-test/protocol/openid-connect/token",
"jwks_uri": "http://localhost:8080/realms/secure-test/protocol/openid-connect/certs",
...
}
Verify JWKS Endpoint¶
Check the JSON Web Key Set:
You should see public keys used for JWT signature verification.
Testing with Users
To test token generation, you'll need to create users first. See the User Management guide for creating test users.
Export Realm Configuration¶
- Go to "Realm settings"
- Click "Action" → "Partial export"
- Select what to export:
- ✓ Export groups and roles
- ✓ Export clients
- ✗ Export users (for security)
- Click "Export"
Download: secure-test-realm.json
```bash