https://github.com/nuraj250/custom-auth-interceptor
Spring Boot project with a custom annotation for user authentication using JWT and Spring Security.
https://github.com/nuraj250/custom-auth-interceptor
backend custom-annotation java logging spring-boot
Last synced: 12 months ago
JSON representation
Spring Boot project with a custom annotation for user authentication using JWT and Spring Security.
- Host: GitHub
- URL: https://github.com/nuraj250/custom-auth-interceptor
- Owner: Nuraj250
- Created: 2025-01-14T15:02:03.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-29T17:15:44.000Z (over 1 year ago)
- Last Synced: 2025-03-14T04:23:36.085Z (over 1 year ago)
- Topics: backend, custom-annotation, java, logging, spring-boot
- Language: Java
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Custom Annotation in Spring Boot with JWT Authentication
## π What is this project?
This project demonstrates how to create a **custom annotation** in a Spring Boot application while implementing **JWT-based authentication**. The custom annotation `@InjectUserContext` allows automatic injection of user details (like username and roles) into API methods, simplifying authentication and authorization workflows. The project also includes:
- **Spring Security** for authentication and authorization.
- **JWT (JSON Web Token)** for secure API access.
- **H2 in-memory database** for easy testing.
- **Aspect-Oriented Programming (AOP)** to handle the custom annotation.
- **Global Exception Handling** for better error management.
It is a complete authentication system that demonstrates best practices for user authentication and role-based access control in Spring Boot.
## π How to Run Your Spring Boot Project (With Custom Annotation & JWT Authentication)
Now that your project is set up correctly, follow these **step-by-step instructions** to run and test your application.
---
## πΉ 1οΈβ£ Ensure Your Environment is Ready
Before running the project, make sure you have:
- β
**Java 17** (or the version specified in `pom.xml`)
- β
**Maven Installed** (`mvn -v` to check)
- β
**IntelliJ IDEA / VS Code / Eclipse** (Any Java IDE)
- β
**Postman / cURL** (for API testing)
---
## πΉ 2οΈβ£ Build & Run the Application
### **Option 1: Using Your IDE (IntelliJ / Eclipse / VS Code)**
1. Open the project in **IntelliJ IDEA** (or your preferred IDE).
2. Navigate to the `CustomAnnotationApplication.java` class.
3. Click Run βΆοΈ OR use the shortcut:
Mac: Cmd + Shift + F10
Windows/Linux: Ctrl + Shift + F10
### **Option 2: Using Maven (Terminal)**
Run the following commands in the project root directory:
```sh
# 1οΈβ£ Clean previous builds (optional)
mvn clean
# 2οΈβ£ Build the project
mvn install
# 3οΈβ£ Run the application
mvn spring-boot:run
```
Your **Spring Boot application** should now start on **`http://localhost:8080`**.
---
## πΉ 3οΈβ£ Verify the H2 Database (Optional)
Since we are using **H2 in-memory database**, you can check the database via **H2 Console**:
- **URL**: `http://localhost:8080/h2-console`
- **JDBC URL**: `jdbc:h2:mem:testdb`
- **Username**: `sa`
- **Password**: `password`
Click **Connect** to view the `users` table.
---
## πΉ 4οΈβ£ Test API Endpoints (Authentication & Custom Annotation)
Now let's test your APIs using **Postman** or **cURL**.
### **π Login API (Get JWT Token)**
#### **POST `http://localhost:8080/api/auth/login`**
##### **Request Body (JSON)**
```json
{
"username": "admin",
"password": "admin123"
}
```
##### **Response (JSON)**
```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5..."
}
```
β
**Copy this token** for the next requests.
---
### **π Access Secured API (`@InjectUserContext` Custom Annotation)**
#### **GET `http://localhost:8080/api/auth/me`**
##### **Headers**
```text
Authorization: Bearer
```
##### **Response (JSON)**
```json
{
"username": "admin",
"roles": "ROLE_ADMIN"
}
```
If you donβt send a token, you should get:
```json
{
"error": "Unauthorized"
}
```
β
**This confirms that the JWT authentication and custom annotation are working.**