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

https://github.com/chamikathereal/j2ee-security-jwt-with-db

A practical Java EE project demonstrating secure JWT-based authentication and role-based authorization with a database-backed identity store, Hibernate, and Jakarta EE Security APIβ€”ideal for learning modern, stateless security in enterprise applications.
https://github.com/chamikathereal/j2ee-security-jwt-with-db

api-rest authentication jakartaee java java-automation javaee jwt jwt-authentication jwt-token

Last synced: 11 months ago
JSON representation

A practical Java EE project demonstrating secure JWT-based authentication and role-based authorization with a database-backed identity store, Hibernate, and Jakarta EE Security APIβ€”ideal for learning modern, stateless security in enterprise applications.

Awesome Lists containing this project

README

          

# πŸš€ J2EE-Security-JWT-With-db

## πŸ“ Project Overview

**J2EE-Security-JWT-With-db** is a practical Java EE project that demonstrates secure authentication and role-based authorization using JSON Web Tokens (JWT) and a database-backed identity store. The project leverages Jakarta EE Security API, JAX-RS for RESTful endpoints, Hibernate ORM for persistence, and MySQL for data storage. It is ideal for learning how to build modern, stateless, and scalable authentication systems in enterprise Java applications.

---

## πŸ“½οΈ Demo Video
[![J2EE-Security-JWT-With-db Project Demo](https://github.com/chamikathereal/J2EE-Security-JWT-With-db/blob/main/J2EE-Security-JWT-With-db.png)](https://youtu.be/sSrRBxIvBqo)

---

## πŸ—‚οΈ Project Structure

```
J2EE-Security-JWT-With-db/
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ main/
β”‚ β”‚ β”œβ”€β”€ java/
β”‚ β”‚ β”‚ └── lk.jlat.app.security/
β”‚ β”‚ β”‚ β”œβ”€β”€ config/
β”‚ β”‚ β”‚ β”‚ └── AppConfig
β”‚ β”‚ β”‚ β”œβ”€β”€ controller/
β”‚ β”‚ β”‚ β”‚ └── AuthController
β”‚ β”‚ β”‚ β”œβ”€β”€ model/
β”‚ β”‚ β”‚ β”‚ └── User
β”‚ β”‚ β”‚ β”œβ”€β”€ security/
β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ AppIdentityStore
β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ AuthMechanism
β”‚ β”‚ β”‚ β”‚ └── Credentials
β”‚ β”‚ β”‚ β”œβ”€β”€ service/
β”‚ β”‚ β”‚ β”‚ └── LoginService
β”‚ β”‚ β”‚ β”œβ”€β”€ servlet/
β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ Login
β”‚ β”‚ β”‚ β”‚ └── Profile
β”‚ β”‚ β”‚ └── util/
β”‚ β”‚ β”œβ”€β”€ resources/
β”‚ β”‚ β”‚ └── META-INF/
β”‚ β”‚ β”‚ └── persistence.xml
β”‚ β”‚ └── webapp/
β”‚ β”‚ β”œβ”€β”€ user/
β”‚ β”‚ β”‚ └── index.jsp
β”‚ β”‚ β”œβ”€β”€ WEB-INF/
β”‚ β”‚ β”‚ └── web.xml
β”‚ β”‚ β”œβ”€β”€ index.jsp
β”‚ β”‚ └── login.jsp
β”œβ”€β”€ pom.xml
```
[See full structure and code in the attached file][1]

## πŸ“¦ Module Details

#### 🟦 **config**
- **AppConfig**
Configures the JAX-RS application path (`/api`) for all REST endpoints.

##

#### 🟦 **controller**
- **AuthController**
REST controller for authentication endpoints (`/auth/login`, `/auth/register`). Handles login requests, validates credentials, and issues JWT tokens.

##

#### 🟦 **model**
- **User**
JPA entity representing a user with username, password, and a set of roles. Used for authentication and authorization.

##

#### 🟦 **security**
- **AppIdentityStore**
Custom identity store that validates user credentials and retrieves roles from the database using the `LoginService`.

- **AuthMechanism**
Custom HTTP authentication mechanism that supports both JWT-based and form-based authentication, using the Jakarta EE Security API.

- **Credentials**
POJO representing username and password for authentication requests.

##

#### 🟦 **service**
- **LoginService**
Handles user validation and role retrieval from the database using JPA and Hibernate.

##

#### 🟦 **servlet**
- **Login**
Servlet for processing login requests and authenticating users.

- **Profile**
Servlet for displaying the user profile page, secured with role-based access control annotations.

##

#### 🟦 **util**
- **JWTUtil**
Utility class for generating and validating JWT tokens, embedding username and roles as claims.

##

#### 🟦 **resources/META-INF**
- **persistence.xml**
JPA configuration file for Hibernate and MySQL integration.

##

#### 🟦 **webapp**
- **user/index.jsp**
User dashboard, accessible only to authenticated users with the appropriate role.

- **WEB-INF/web.xml**
Declarative security configuration for protected resources and role mappings.

- **login.jsp**
Custom login form for user authentication.

- **index.jsp**
Public landing page.

## βš™οΈ Key Features

- βœ… Secure JWT-based authentication for REST APIs
- βœ… Database-backed user and role management with Hibernate (JPA)
- βœ… Custom identity store and authentication mechanism (Jakarta EE Security API)
- βœ… Stateless session management using JWTs
- βœ… Role-based access control for servlets and endpoints
- βœ… Clean, modular Maven project structure

## πŸ’‘ How It Works

- **User Authentication:**
Users authenticate via REST (`/api/auth/login`) or form. Credentials are validated against the database. On success, a JWT is issued containing the username and roles.

- **JWT Validation:**
Protected endpoints require a valid JWT in the `Authorization` header (`Bearer `). The custom authentication mechanism parses and validates the token, extracting user identity and roles for access control.

- **Role-Based Authorization:**
Access to servlets and JSPs is protected using declarative (`web.xml`) and annotation-based security. Only users with appropriate roles can access secured resources.

- **Session Management:**
Stateless authentication is achieved using JWTs, eliminating the need for server-side session storage.

- **Database Integration:**
User credentials and roles are stored in MySQL and managed via Hibernate/JPA.

## πŸ› οΈ Technologies Used

- Java 11
- Jakarta EE 10 (JAX-RS, Security API, Servlet, JPA)
- Hibernate ORM
- MySQL
- JJWT (io.jsonwebtoken)
- Maven

## πŸ“š Learning Outcomes

- βœ… Implement JWT-based authentication and authorization in Java EE
- βœ… Integrate database-backed identity management with Hibernate
- βœ… Secure RESTful APIs and web resources with modern best practices
- βœ… Build scalable, stateless, and secure enterprise Java applications

## πŸ§‘β€πŸ’» Author

Chamika Gayashan
Undergraduate Software Engineer | Sri Lanka
Linkedin: @chamikathereal
Current date: Friday, June 27, 2025, 6:30 PM +0530