auth-starter

Auth Starter: Spring Boot JWT Authentication Template

Description

Auth Starter is a ready-to-use Spring Boot template designed to bootstrap applications requiring secure user authentication using JSON Web Tokens (JWT). It provides a solid foundation for user registration and login, integrating essential security practices with Spring Security, Spring Data JPA, and JWT.

This project aims to save development time by offering pre-configured components for:

Features (Current Implementation)

Technologies Used

Prerequisites

Configuration

The application requires the following environment variables to be set:

These are configured in src/main/resources/application.properties to read from the environment.

Running the Application

  1. Clone the repository:
    git clone https://github.com/suryadeepkoduri/auth-starter.git
    cd auth-starter
  2. Set Environment Variables: Export the required variables listed in the Configuration section in your terminal or IDE run configuration.
  3. Database Setup: Ensure your database schema exists (e.g., auth_db). The application uses spring.jpa.hibernate.ddl-auto=update, which will attempt to update the schema based on entities, but it’s recommended to manage schema changes explicitly in production (e.g., using Flyway or Liquibase).
  4. Build the application:
    mvn clean install
  5. Run the application:
    mvn spring-boot:run
    Alternatively, you can run the packaged jar:
    java -jar target/auth-starter-0.0.1-SNAPSHOT.jar

The application will start on the default port (usually 8080).

API Endpoints

Future Improvements & Considerations

This starter provides a basic setup. Consider implementing the following enhancements for production readiness:

  1. Refresh Tokens: Implement a refresh token mechanism to allow users to obtain new access tokens without repeatedly logging in. This involves generating, storing securely, validating, and rotating refresh tokens.
  2. Enhanced Security:
    • Secret Management: Use Vault, AWS Secrets Manager, GCP Secret Manager, or Azure Key Vault instead of environment variables for JWT_SECRET_KEY and DATABASE_PASSWORD.
    • Password Policy: Enforce stronger password complexity rules.
    • Rate Limiting: Protect login/signup endpoints against brute-force attacks.
  3. Role Management:
    • Implement logic to assign default roles (e.g., ‘USER’) upon registration.
    • Create endpoints or mechanisms to manage user roles (e.g., assigning ‘ADMIN’ roles).
    • Implement role-based access control (RBAC) more explicitly on protected endpoints using @PreAuthorize or similar annotations.
  4. Robust Exception Handling: Implement a global exception handler (@ControllerAdvice) to provide consistent, standardized error responses across the API.
  5. Comprehensive Testing: Add extensive unit tests (Services, Utils), integration tests (Controllers, Security Flow), and potentially contract tests.
  6. Logging: Implement structured logging throughout the application for monitoring and debugging.
  7. CORS Configuration: Make allowed CORS origins configurable via application.properties for different environments.
  8. Database Migrations: Use Flyway or Liquibase for reliable database schema management instead of ddl-auto=update in production.
  9. User Management Features: Add endpoints for profile updates, password reset/change functionality, email verification, etc.
  10. Dependency Updates: Keep Spring Boot, security libraries (JJWT), and other dependencies up-to-date.

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues.