Authentication and authorization microservice for DeviceHub.
This service is responsible for:
- User authentication (login)
- User registration (signup)
- JWT token generation and validation
- Role-based access control (ADMIN / CLIENT)
- Synchronization of user data with other microservices via Kafka
It acts as the internal Identity Provider for the system.
The Auth Service exposes public REST endpoints for:
- Creating a new account
- Authenticating existing users
After successful authentication, it returns a signed JWT token. All protected endpoints in the system rely on this token.
The service is:
- Stateless (no server-side sessions)
- Backed by PostgreSQL
- Integrated with Kafka for user synchronization
- Built with Spring Boot 3, Spring Security, JPA and jjwt
- Java 17
- Spring Boot 3
- Spring Security (stateless JWT)
- Spring Data JPA
- PostgreSQL
- Apache Kafka
- jjwt (HS256 signing)
- Bean Validation (Jakarta Validation)
-
Client sends signup request.
-
Auth Service:
- Validates input
- Hashes password (BCrypt)
- Stores user in
auth_users - Publishes
USER_CREATEDevent to Kafka - Generates JWT
-
JWT is returned to client.
-
Client sends username + password.
-
Spring Security authenticates using:
- DAO Authentication Provider
- Database lookup
- BCrypt password verification
-
JWT is generated and returned.
Base path:
/api/auth
Registers a new user.
{
"username": "john.doe",
"email": "john@example.com",
"password": "StrongPass1",
"firstName": "John",
"lastName": "Doe"
}-
Username: 3–50 characters
-
Email: valid format
-
Password:
- Minimum 8 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one digit
-
First name: required
-
Last name: required
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}HTTP 200 OK
Authenticates existing user.
{
"username": "john.doe",
"password": "StrongPass1"
}{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}HTTP 200 OK
The service generates signed JWT tokens using:
- Algorithm: HS256
- Secret: Base64-encoded secret from configuration
- Expiration: configurable (default 1 hour)
Standard:
sub→ usernameiat→ issued atexp→ expiration
Custom:
role→ ADMIN or CLIENTuserId→ UUID
These claims allow downstream services to:
- Identify the user
- Apply role-based authorization
- Avoid additional DB calls if trusted
- No server-side sessions
- Each request must include:
Authorization: Bearer <token>
- Passwords are hashed using BCrypt
- Plain passwords are never stored
- Password strength is validated using a custom validator
Each user has exactly one role:
- ADMIN
- CLIENT
Authorities are mapped as:
ROLE_ADMIN
ROLE_CLIENT
Table: auth_users
| Column | Type | Notes |
|---|---|---|
| id | UUID | Primary key |
| username | TEXT | Unique, not null |
| password | TEXT | BCrypt hash |
| role | TEXT | Stored as string enum |
User IDs are generated at signup time.
Topic:
sync.users
Event: USER_CREATED
Payload:
{
"eventType": "USER_CREATED",
"id": "uuid",
"username": "john.doe",
"email": "john@example.com",
"firstName": "John",
"lastName": "Doe"
}Purpose:
- Synchronize user creation with other microservices (e.g. user-service)
Auth Service listens to:
USER_UPDATEDUSER_DELETED
On update:
- Username may be updated locally.
On delete:
- User is removed from auth database.
This ensures consistency between services.
All input DTOs use Bean Validation.
Custom exception handling ensures consistent error responses:
{
"status": 409,
"code": "USERNAME_ALREADY_TAKEN",
"message": "Username already taken: john.doe"
}Authentication failures return:
- HTTP 401
- Generic message (no sensitive details exposed)
The service includes:
-
Unit tests for:
AuthServiceJwtService
-
Controller tests using MockMvc
-
Context load test
Tests validate:
- Token generation and validation
- Signup flow
- Login flow
- Event publishing
- Authentication failure behavior
security:
jwt:
secret-key: ${SECURITY_JWT_SECRET_KEY}
expiration-time: 3600000The secret must be Base64-encoded.
localdocker
Each profile defines:
- Database connection
- Kafka bootstrap servers
- User service URL
Multi-stage Docker build:
- Stage 1: Maven build
- Stage 2: Runtime image (Temurin JDK 17)
Expose:
8081
Run example:
docker build -t auth-service .
docker run -p 8081:8081 auth-service
Swagger UI:
/swagger-ui.html
OpenAPI Docs:
/v3/api-docs
Actuator endpoints available under:
/actuator
- Clear separation of layers (Controller → Service → Repository)
- Stateless security
- Strong password policy
- Event-driven synchronization
- Explicit error model
- Unit-tested business logic
Current design:
- Single role per user
- No refresh token mechanism
- No token revocation mechanism
- Kafka publish not transactional with DB
- No distributed tracing yet
These are areas for future enhancement.
Auth Service is a stateless JWT-based authentication microservice built for a distributed system.
It provides:
- Secure credential handling
- Token-based authorization
- Role enforcement
- Event-driven synchronization with other services
- Clean layering and test coverage
It is designed to be simple, clear, and extensible for a microservices environment.

