A modern, robust, and production-ready implementation of stateless user authentication and authorization using Spring Boot 3, Spring Security 6, and JSON Web Tokens (JWT).
This project demonstrates software engineering best practices by cleanly separating database layers, data containers, security middleware, and global error interceptors.
- Stateless Authentication: Completely sessionless architecture utilizing JSON Web Tokens (JWT).
- Cryptographic Security: Secure password storage using the BCrypt hashing algorithm.
- Mass Assignment Protection: Strict separation of data layers using specialized Data Transfer Objects (DTOs).
- Global Exception Handling: Clean, centralized error processing producing standardized RFC 7807 (Problem Details) JSON responses.
- Cross-Origin Resource Sharing (CORS): Pre-configured secure origin handling for standard frontend client ecosystems (e.g., React on
localhost:8005).
The project acts as an automated pipeline, processing network-level HTTP requests into securely verified backend transactions:
- Network Level (CORS & Filters): The request hits the security boundary. The
JwtAuthenticationFilterintercepts it, extracts the token from theAuthorizationheader, and verifies its authenticity. - Context Vault: If validated, the user profile is packed into an
Authenticationpassport and stored in Spring Security'sSecurityContextHolder. - Data Mapping (DTOs): Incoming requests map directly to lightweight DTOs (
RegisterUserDto,LoginUserDto) to protect raw JPA Entities from mass-assignment injection attacks. - Service / DB Integration: The
AuthenticationServiceorchestrates business logic (e.g., validating hashes, persisting entries viaUserRepositoryinto MySQL).
src/main/java/com/andrej/auth_api/
│
├── configs/ # Spring Security configuration and dependency beans
│ ├── ApplicationConfiguration.java
│ ├── JwtAuthenticationFilter.java # Core middleware interceptor
│ └── SecurityConfiguration.java # Main security filter chain layout
│
├── controllers/ # REST Controllers exposing protected/unprotected routes
│ └── UserController.java
| └──AuthenticationController.java
│
├── dtos/ # Data Transfer Objects protecting network payloads
│ ├── LoginUserDto.java
│ └── RegisterUserDto.java
│
├── entities/ # Database models mapped via Jakarta Persistence (JPA)
│ └── User.java
│
├── exceptions/ # Global application error safety nets
│ └── GlobalExceptionHandler.java # Centralized exception translator
│
├── repositories/ # Database access communication interfaces
│ └── UserRepository.java
│
└── services/ # Core business logic orchestrators
├── AuthenticationService.java
└── JwtService.java
- Java 17 or higher
- Maven 3.x
- MySQL Server (or any alternative relational database)
Create a database in your MySQL instance and update the database connectivity parameters inside src/main/resources/application.properties:
spring.datasource.url=jdbc:mysql://localhost:3606/your_database_name
spring.datasource.username=your_mysql_username
spring.datasource.password=your_mysql_password
spring.jpa.hibernate.ddl-auto=update
Navigate to the root directory of the project and execute the following commands in your terminal:
# Clean project and download dependencies
mvn clean install
# Launch the Spring Boot application server
mvn spring-boot:run
The server will boot up and begin listening for incoming requests on http://localhost:8080.
- HTTP Method:
POST - URL:
http://localhost:8080/auth/signup - Payload (JSON):
{
"email": "user@example.com",
"password": "securePassword123",
"fullName": "John Doe"
}
- HTTP Method:
POST - URL:
http://localhost:8080/auth/login - Payload (JSON):
{
"email": "user@example.com",
"password": "securePassword123"
}
Returns a JSON containing the JWT string under the token key.
To access these routes, you must append the JWT token obtained from logging in inside the HTTP Request Header as follows:
- Key:
Authorization - Value:
Bearer <your_copied_jwt_token>
- HTTP Method:
GET - URL:
http://localhost:8080/users/me
This project is open-source and available under the MIT License.