An open API service indexing awesome lists of open source software.

https://github.com/mrrajputh/springboot-jwt-auth

Spring Boot 3 JWT Authentication System
https://github.com/mrrajputh/springboot-jwt-auth

jwt-authentication jwt-authorization jwt-token postgresql-database spring-boot spring-security

Last synced: about 1 month ago
JSON representation

Spring Boot 3 JWT Authentication System

Awesome Lists containing this project

README

          

# ๐Ÿ” Spring Boot JWT Authentication System

This project is a **secure, production-ready JWT Authentication and Authorization system** built using **Spring Boot 3.0**, **Spring Security 6.0**, and **Java 17**. It demonstrates how to implement user registration, login, and role-based access to protected endpoints using JWT tokens.

---

## ๐Ÿš€ Features

- โœ… User Registration & Authentication
- ๐Ÿ” Stateless Security with JWT Tokens
- ๐Ÿ” Secure Token Generation (256-bit key)
- ๐Ÿง‘โ€๐Ÿ’ป Role-Based Access Control (RBAC)
- ๐Ÿ” Password Encryption using BCrypt
- ๐Ÿงฑ Modular Architecture: Controller, Service, Repository
- โš™๏ธ Configurable `application.yml`
- ๐Ÿงช API Testing via Postman or Swagger UI

---

## ๐Ÿงฐ Tech Stack

| Layer | Technology |
|--------------|-------------------------------------|
| Language | Java 17 |
| Framework | Spring Boot 3, Spring Security 6 |
| Build Tool | Maven |
| ORM | JPA, Hibernate |
| DB | PostgreSQL / H2 (for testing) |
| Auth Method | JWT (JJWT Library) |

---

## ๐Ÿ“ Project Structure

src/

โ””โ”€โ”€ main/

โ””โ”€โ”€ java/

โ””โ”€โ”€ com/arjun/sec/

โ”œโ”€โ”€ auth/ # DTOs & AuthService

โ”œโ”€โ”€ config/ # JWT & Security Config

โ”œโ”€โ”€ controller/ # REST endpoints

โ”œโ”€โ”€ entity/ # User entity & roles

โ”œโ”€โ”€ repository/ # Data access layer

โ””โ”€โ”€ SecApplication.java

---

## ๐Ÿ“ฆ Getting Started

### 1๏ธโƒฃ Clone the Repo

bash:
git clone https://github.com/mrRajputh/springboot-jwt-auth.git

2๏ธโƒฃ Configure Your DB (PostgreSQL / H2)

Open src/main/resources/application.yml and set your DB config:

spring:

datasource:

url: jdbc:postgresql://localhost:5432/jwt_security

username: write here your username

password: write here your password

driver-class-name: org.postgresql.Driver

jpa:

hibernate:

ddl-auto: create-drop

show-sql: true

properties:

hibernate:

format_sql: true

database: postgresql

database-platform: org.hibernate.dialect.PostgreSQLDialect

๐Ÿ” JWT 256-bit Secret Key Setup

To securely generate and sign JWT tokens, this project uses a 256-bit secret key.

โœ… Generate a 256-bit Key
You can generate a 256-bit key using any of the following methods:

You can use any online 256-bit key generator web site from google

Example:

Visit: https://www.allkeysgenerator.com

Select:

Key size: 256-bit

Output format: Base64

๐Ÿ”Add the Key to JwtService.java

private static final String SECRET_KEY = "your-256-bit-base64-key-goes-here";

3๏ธโƒฃ Run the Application
bash:

./mvnw spring-boot:run

Or run directly from your IDE (IntelliJ, Eclipse, etc.)

๐Ÿ” API Endpoints (you can use postman to test api Endpoints )

| Endpoint | Method | Description |
| --------------------------- | ------ | --------------------------- |
| `/api/v1/auth/register` | POST | Register a new user |
| `/api/v1/auth/authenticate` | POST | Login and receive JWT token |
| `/api/v1/demo-controller` | GET | Access protected endpoint |

๐Ÿ“Š Future Improvements:

๐Ÿ” Add Refresh Token functionality

๐Ÿงช Unit & Integration Tests

๐Ÿงฐ Dockerfile & docker-compose setup

๐Ÿ“œ Swagger API Docs

๐Ÿ“ก Monitoring (Prometheus/Grafana)

๐Ÿ” MFA or OAuth2 integration