Services Deep Dive
In-depth look at the business logic layer: KeyCloakService and KeyCloakManager.
KeyCloakServiceImpl
The KeyCloakServiceImpl is the primary orchestrator for identity-related operations in the Auth Service.
1. User Authentication (authenticateUser)
Handles the verification of user credentials via Keycloak's token endpoint.
Java code-highlight
// Build form data for password grant
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("grant_type", "password");
formData.add("client_id", keyCloakManager.getClientId());
formData.add("client_secret", keyCloakManager.getClientSecret());
// ...
Login Failure Strategy
If Keycloak returns a 401 Unauthorized, the Auth Service triggers a Feign call to the User Service to increment the failure counter, potentially leading to an account lock.
2. User Creation (createKeyCloakUser)
Translates KeyCloakUser DTOs into Keycloak UserRepresentation objects and persists them using the Admin SDK.
- Username: Derived from the application-level
userId. - Credential Storage: Creates a non-temporary password credential for the user record.
KeyCloakManager
The KeyCloakManager is responsible for initializing and managing the lifecycle of the Keycloak Admin Client.
- Initialization: Builds the
Keycloakinstance at startup usingclient_credentials. - Resource Access: Exposes the
UsersResourcebean for atomic user operations across the service layer.
Java code-highlight
@Bean
public UsersResource getUsersResource() {
return keyCloakManager.getKeyCloakInstanceWithRealm().users();
}
The
KeyCloakManager uses the arya-banking-auth-client credentials to perform administrative identity operations.