This project demonstrates a basic implementation of JSON Web Tokens (JWT) in Java without using any heavy frameworks. It provides core JWT functionality including token generation, validation, and claim extraction.
- Java 11 or higher
- Maven 3.6+
- Your favorite IDE (IntelliJ IDEA, Eclipse, or VS Code)
jwt-demo/
├── src/
│ └── main/
│ ├── java/
│ │ └── com/
│ │ └── example/
│ │ └── jwt/
│ │ ├── JwtUtil.java
│ │ └── Main.java
│ └── resources/
├── pom.xml
├── README.md
└── JWT-THEORY.md
- Clone the repository:
git clone https://github.com/yourusername/jwt-demo.git
cd jwt-demo- Build the project:
mvn clean installJwtUtil jwtUtil = new JwtUtil();
// Create claims
Map<String, Object> claims = new HashMap<>();
claims.put("userId", "123");
claims.put("role", "admin");
// Generate token
String token = jwtUtil.generateToken("[email protected]", claims);try {
boolean isValid = jwtUtil.validateToken(token);
if (isValid) {
System.out.println("Token is valid!");
}
} catch (JwtException e) {
System.out.println("Token validation failed: " + e.getMessage());
}Claims claims = jwtUtil.extractAllClaims(token);
String userId = claims.get("userId", String.class);
String role = claims.get("role", String.class);Generated Token: eyJhbGciOiJIUzI1NiJ9...
Token is valid: true
Claims:
- Subject: [email protected]
- UserId: 123
- Role: admin
- Issued At: 2023-07-10T10:30:00Z
Feel free to submit issues and enhancement requests!
For detailed information about JWT concepts, security considerations, and best practices, please refer to JWT-THEORY.md.
This project is licensed under the MIT License - see the LICENSE file for details.