Overview
OrgStack will use JWT (JSON Web Tokens) for stateless authentication. When users log in, the server will issue a signed token containing their identity and organization membership. Users will include this token in subsequent requests to prove their identity.Authentication flow
Server validates credentials
Spring Security’s authentication manager verifies your credentials against the database. Passwords are hashed using BCrypt.
JWT token generation
If credentials are valid, the server generates a JWT containing your user ID, organization ID, and roles.
Client stores token
The Angular frontend stores the JWT in memory (or httpOnly cookie for enhanced security).
Token included in requests
All subsequent API requests include the JWT in the
Authorization: Bearer <token> header.Spring Security configuration
OrgStack integrates Spring Security 6.x (part of Spring Boot 4.0.3) with JWT-based authentication:pom.xml
Spring Security provides comprehensive security features including authentication, authorization, CSRF protection, and secure headers out of the box.
JWT token structure
A typical OrgStack JWT contains these claims:Token claims explained
- sub: Subject - your user UUID from the database
- organizationId: The tenant/organization you belong to (critical for multi-tenancy)
- email: Your email address for display purposes
- roles: Your roles within the organization for authorization
- iat: Issued at timestamp (Unix epoch)
- exp: Expiration timestamp (typically 24 hours)
Password security
Passwords in OrgStack are secured using industry-standard practices:BCrypt hashing
Passwords are hashed with BCrypt, which includes automatic salting and configurable cost factor.
Never logged
Password fields are excluded from all logging and serialization.
One-way only
Passwords cannot be decrypted - only validated against the stored hash.
Secure transport
Always transmitted over HTTPS in production environments.
Security filter chain
Spring Security’s filter chain processes every request:Token validation
The JWT filter performs these validations on every request:Signature verification
Signature verification
Validates that the token was signed by the server using the secret key. This prevents token forgery.
Expiration check
Expiration check
Ensures the token hasn’t expired. Expired tokens are rejected with 401 Unauthorized.
Claims validation
Claims validation
Verifies that required claims (sub, organizationId) are present and have valid formats (UUIDs).
User existence
User existence
Optionally queries the database to confirm the user still exists and is active (can be cached).
Session management
OrgStack uses stateless authentication:- No server-side sessions: The JWT contains all necessary information
- Horizontal scalability: Any server instance can validate any token
- Logout handling: Tokens cannot be invalidated server-side (use short expiration times)
- Refresh tokens: Longer-lived tokens can be used to obtain new access tokens
For enhanced security, consider implementing token revocation using a Redis blacklist for logged-out tokens.
Entity auditing
Spring Data JPA automatically tracks who created and modified entities:JpaConfig.java
@CreatedDate and @LastModifiedDate annotations in BaseEntity, this provides automatic audit trails:
BaseEntity.java
You can extend auditing to track
createdBy and lastModifiedBy by implementing Spring Security’s AuditorAware interface.Public endpoints
Certain endpoints don’t require authentication:/auth/login- Login endpoint/auth/register- User registration/health- Health check for monitoring/actuator/**- Spring Boot Actuator endpoints (should be secured in production)
CORS configuration
For the Angular frontend to communicate with the Spring Boot backend, CORS (Cross-Origin Resource Sharing) must be configured:Best practices
Use HTTPS in production
Always use TLS/SSL to encrypt tokens in transit. Never send JWTs over plain HTTP.
Short token lifetimes
Keep access token expiration short (15 minutes to 1 hour). Use refresh tokens for longer sessions.
Validate all claims
Don’t trust token claims blindly. Always validate UUIDs are properly formatted and reference existing entities.
Testing authentication
Spring Security provides test utilities:Next steps
Authorization
Learn about role-based access control
Multi-tenancy
Understand how tenant context flows through authentication