Overview
sgivu-user manages users, roles, and permissions in the SGIVU ecosystem. It provides CRUD operations for users and persons, role and permission management, multi-criteria search, and support endpoints for the Authorization Server (username lookup for JWT issuance).
Technologies
- Java: 21
- Spring Boot: 4.0.1
- Spring Security: Resource Server (JWT validation)
- Spring Data JPA: PostgreSQL persistence
- Flyway: Database migrations
- Spring Cloud Config Client: Centralized configuration
- Spring Cloud Eureka Client: Service registration
- SpringDoc OpenAPI: Swagger UI documentation
- MapStruct: DTO mapping
- Lombok: Code generation
Prerequisites
- JDK 21
- Maven 3.9+
- PostgreSQL database
sgivu-configandsgivu-discoveryservices running
Running the Service
Development with Docker Compose
Local Execution
Docker Build
Security
Authentication
JWT Token Validation:- Tokens issued by
sgivu-authAuthorization Server - Validation using
NimbusJwtDecoderpointing to issuer - Resource Server configuration validates signature via JWKS endpoint
Authorization
Claims-Based Authorization:rolesAndPermissionsclaim used for granular authorization- Required permissions per endpoint:
user:create- Create usersuser:read- View usersuser:update- Update usersuser:delete- Delete usersperson:create,person:read,person:update,person:delete- Person operations
Internal Service Authentication
X-Internal-Service-Key Header:- Allows
sgivu-authto query users by username during token issuance - Used for service-to-service authentication
- Critical: Do not expose this key externally
Custom Validators
PasswordStrength Validator:- Minimum length requirements
- Complexity rules (uppercase, lowercase, numbers, special characters)
- Applied on user creation and password updates
- Prevents special characters in specific fields
- Applied to usernames and other text fields
- Configurable character whitelist
Database Schema
Flyway Migrations
Location:src/main/resources/db/migration
V1__initial_schema.sql creates:
- Core Tables
- Junction Tables
permissions: System permissions catalogroles: User rolespersons: Personal informationusers: User accounts
users.username(unique)persons.document_number(unique)permissions.name(unique)roles.name(unique)
Seed Data
Location:src/main/resources/db/seed/R__seed_data.sql
Creates:
- Admin user:
stevenwithADMINrole - Complete permissions catalog
- Default roles (ADMIN, USER, etc.)
- Sample person and address data
Repeatable migration (
R__) runs on every startup if checksum changes. Use for reference data that may need updates.API Endpoints
User Management
List all users with paginationRequired Permission:
user:readQuery Parameters:page: Page number (default: 0)size: Page size (default: 20)sort: Sort field and direction
Get user by IDRequired Permission:
user:readCreate new userRequired Permission:
user:createValidations:- Unique username
- Password strength requirements
- Valid role assignments
Update existing userRequired Permission:
user:updateDelete user (soft delete)Required Permission:
user:deletePerson Management
List persons with advanced searchRequired Permission:
person:readSupports:- Multi-criteria filtering
- Sorting and pagination
- Search by document number, name, etc.
Get person by ID including addressesRequired Permission:
person:readCreate person with optional addressesRequired Permission:
person:createUpdate person informationRequired Permission:
person:updateDelete personRequired Permission:
person:deleteInternal Endpoints
Query user by username for token issuanceAuthentication: Requires
X-Internal-Service-Key headerUsed By: sgivu-auth during login flowReturns: User details with roles and permissionsRequest/Response Examples
Create User
Response
Observability
Actuator Endpoints
/actuator/health: Service health status/actuator/info: Application metadata- Additional endpoints configurable per environment
OpenAPI Documentation
Swagger UI:/swagger-ui/index.html
Configuration: OpenApiConfig defines server URLs for different environments
Distributed Tracing
Configured viasgivu-config:
- Zipkin endpoint for trace export
- Trace IDs propagated via headers
- Integration with Micrometer
Testing
- Unit tests for services and validators
- Integration tests for repositories
- Controller tests with MockMvc
- Security tests for permission checks
Troubleshooting
401/403 Authorization errors
401/403 Authorization errors
Internal endpoint fails
Internal endpoint fails
Symptoms: Auth service can’t query users during loginSolutions:
- Verify
X-Internal-Service-Keyheader is present - Check internal key matches between auth and user services
- Ensure internal endpoints aren’t blocked by gateway
- Review network connectivity between services
- Check Eureka service discovery is working
Database migration errors
Database migration errors
Symptoms: Service fails to start with Flyway errorsSolutions:
- Verify PostgreSQL connection parameters
- Check database exists and user has proper permissions
- Review Flyway migration history table
- Ensure migration scripts have correct syntax
- Check for conflicts in seed data (duplicate keys)
Password validation fails
Password validation fails
Symptoms: User creation rejected with password errorsSolutions:
- Review password strength requirements
- Ensure password meets minimum length
- Include required character types (uppercase, lowercase, numbers, special)
- Check custom validator configuration
Duplicate username/document
Duplicate username/document
Symptoms: Constraint violation errors on creationSolutions:
- Check uniqueness before creation
- Handle duplicate key exceptions gracefully
- Return clear error messages to clients
- Consider soft delete for deactivated users
Environment Variables
| Variable | Description | Example |
|---|---|---|
DB_HOST | PostgreSQL host | sgivu-postgres-user |
DB_PORT | PostgreSQL port | 5432 |
DB_NAME | Database name | sgivu_user |
DB_USERNAME | Database username | sgivu_user |
DB_PASSWORD | Database password | secure_password |
SERVICE_INTERNAL_SECRET_KEY | Internal service key | secret-key-value |
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI | Auth server issuer | http://sgivu-auth:9000 |
Best Practices
Permission Model
Use fine-grained permissions for flexible access control
Password Policy
Enforce strong password requirements with custom validators
Audit Trail
Track createdAt/updatedAt for all entities
Soft Delete
Consider soft deletes to maintain data integrity
Related Services
Auth Server
Queries user service for credential validation
Gateway
Routes user management API requests
Client Service
Similar entity management patterns