Skip to content

Commit 047f213

Browse files
committed
jwt-rotation
1 parent cb2d525 commit 047f213

13 files changed

Lines changed: 2096 additions & 0 deletions

File tree

springboot-jwt-rotation/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# JWT 动态密钥轮换演示系统
2+
3+
这是一个Spring Boot JWT 动态密钥轮换演示系统,实现了 **RSA 2048 + KID + 定时轮换** 的安全生产实践。
4+
5+
## 🎯 功能特性
6+
7+
### 核心功能
8+
-**动态密钥轮换**: 定时自动生成新的 RSA 密钥对
9+
-**多版本密钥共存**: 新 Token 用新密钥,旧 Token 仍可验证
10+
-**用户无感知**: 密钥轮换不影响已登录用户
11+
-**KID 标识**: JWT Header 包含密钥 ID,支持多版本验证
12+
-**定时清理**: 自动清理过期的密钥
13+
14+
### 演示功能
15+
- 🔐 **用户认证**: 登录/退出、Token 生成和验证
16+
- 🛡️ **受保护资源**: 需要有效 Token 才能访问
17+
- 🔑 **密钥管理**: 查看密钥状态、手动轮换、清理过期密钥
18+
- 🔍 **Token 解析**: 解析 JWT Header、Payload 和验证状态
19+
- 📊 **系统监控**: 实时显示密钥存储和系统状态
20+
21+
## 🏗️ 技术架构
22+
23+
### 后端技术栈
24+
- **Spring Boot 3.2.0** - 应用框架
25+
- **JJWT 0.12.3** - JWT 处理库
26+
- **RSA 2048** - 非对称加密算法
27+
- **Spring Scheduling** - 定时任务
28+
29+
### 前端技术栈
30+
- **HTML5 + CSS3** - 页面结构
31+
- **JavaScript ES6** - 交互逻辑
32+
- **Tailwind CSS** - UI 样式框架
33+
- **Font Awesome** - 图标库
34+
35+
## 🚀 快速开始
36+
37+
### 环境要求
38+
- JDK 17+
39+
- Maven 3.6+
40+
41+
### 运行步骤
42+
43+
1. **克隆项目**
44+
```bash
45+
cd springboot-jwt-rotation
46+
```
47+
48+
2. **编译运行**
49+
```bash
50+
mvn clean spring-boot:run
51+
```
52+
53+
3. **访问应用**
54+
```
55+
浏览器打开: http://localhost:8080
56+
```
57+
58+
### 测试账户
59+
| 用户名 | 密码 | 角色 |
60+
|--------|------|------|
61+
| admin | password | 管理员 |
62+
| user | 123456 | 普通用户 |
63+
| test | test | 测试用户 |
64+
65+
## 🔧 核心组件
66+
67+
### 1. DynamicKeyStore - 动态密钥存储
68+
- 管理 RSA 密钥对的生成、存储和获取
69+
- 支持多版本密钥共存
70+
- 线程安全的 ConcurrentHashMap 存储
71+
- 自动密钥清理机制
72+
73+
### 2. JwtTokenService - JWT 服务
74+
- Token 生成、验证和解析
75+
- 支持多版本密钥验证
76+
- Token 刷新功能
77+
- KID (Key ID) 管理
78+
79+
### 3. KeyRotationScheduler - 密钥轮换调度
80+
- 定时检查和轮换密钥
81+
- 过期密钥清理
82+
- 监控和日志记录
83+
- 支持手动触发
84+
85+
## 📋 API 接口
86+
87+
### 认证相关
88+
- `POST /api/auth/login` - 用户登录
89+
- `POST /api/auth/validate` - Token 验证
90+
- `POST /api/auth/refresh` - Token 刷新
91+
- `GET /api/auth/me` - 获取当前用户信息
92+
93+
### 管理功能
94+
- `POST /api/auth/admin/rotate-keys` - 手动轮换密钥
95+
- `POST /api/auth/admin/cleanup-keys` - 清理过期密钥
96+
97+
### 演示功能
98+
- `GET /api/demo/key-stats` - 获取密钥统计
99+
- `POST /api/demo/parse-token` - 解析 Token
100+
- `POST /api/demo/generate-test-token` - 生成测试 Token
101+
- `GET /api/demo/protected` - 受保护资源
102+
- `GET /api/demo/system-info` - 系统信息
103+
104+
## ⚙️ 配置说明
105+
106+
### application.yml
107+
```yaml
108+
jwt:
109+
token-expiration: 24 # Token过期时间(小时)
110+
rotation-period-days: 7 # 密钥轮换周期(天)
111+
grace-period-days: 14 # 旧密钥保留时间(天)
112+
key-size: 2048 # RSA密钥长度
113+
```
114+
115+
### 定时任务
116+
- **每天凌晨2点**: 检查是否需要轮换密钥
117+
- **每天凌晨3点**: 清理过期密钥
118+
- **每小时**: 密钥状态监控
119+
120+
## 🔄 密钥轮换流程
121+
122+
```
123+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
124+
│ 第1天 │ │ 第7天 │ │ 第21天 │
125+
├─────────────────┤ ├──────────────────┤ ├─────────────────┤
126+
│ 生成 key-001 │ │ 生成 key-002 │ │ 清理 key-001 │
127+
│ 新Token使用 │→ │ 新Token使用 │→ │ 保留 key-002 │
128+
│ key-001签名 │ │ key-002签名 │ │ key-002继续使用 │
129+
│ key-001验证 │ │ key-001仍可验证 │ │ │
130+
└─────────────────┘ └──────────────────┘ └─────────────────┘
131+
```
132+
133+
## 🎨 前端界面
134+
135+
### 功能页面
136+
1. **用户登录** - 登录认证和状态显示
137+
2. **受保护资源** - 演示 Token 保护
138+
3. **密钥信息** - 实时密钥存储状态
139+
4. **Token 解析** - JWT 结构分析工具
140+
5. **管理功能** - 密钥轮换和清理
141+
142+
## 🧪 测试演示
143+
144+
### 基础流程
145+
1. 打开 http://localhost:8080
146+
2. 使用测试账户登录
147+
3. 查看生成的 JWT Token 和 KID
148+
4. 访问受保护资源验证 Token
149+
5. 查看密钥存储状态
150+
151+
### 密钥轮换演示
152+
1. 在管理页面手动轮换密钥
153+
2. 观察新 Token 使用新密钥
154+
3. 旧 Token 仍可正常验证
155+
4. 查看密钥统计信息变化

springboot-jwt-rotation/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>jwt-rotation</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>jar</packaging>
12+
13+
<name>JWT Key Rotation Demo</name>
14+
<description>动态密钥轮换JWT认证系统演示</description>
15+
16+
<parent>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-parent</artifactId>
19+
<version>3.2.0</version>
20+
<relativePath/>
21+
</parent>
22+
23+
<properties>
24+
<java.version>17</java.version>
25+
<jjwt.version>0.12.3</jjwt.version>
26+
</properties>
27+
28+
<dependencies>
29+
<!-- Spring Boot Starter Web -->
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<!-- Spring Boot Starter Thymeleaf -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
39+
</dependency>
40+
41+
<!-- JWT Library -->
42+
<dependency>
43+
<groupId>io.jsonwebtoken</groupId>
44+
<artifactId>jjwt-api</artifactId>
45+
<version>${jjwt.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.jsonwebtoken</groupId>
49+
<artifactId>jjwt-impl</artifactId>
50+
<version>${jjwt.version}</version>
51+
<scope>runtime</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.jsonwebtoken</groupId>
55+
<artifactId>jjwt-jackson</artifactId>
56+
<version>${jjwt.version}</version>
57+
<scope>runtime</scope>
58+
</dependency>
59+
60+
<!-- Jackson for JSON processing -->
61+
<dependency>
62+
<groupId>com.fasterxml.jackson.core</groupId>
63+
<artifactId>jackson-databind</artifactId>
64+
</dependency>
65+
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.jwt;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@SpringBootApplication
8+
@EnableScheduling
9+
public class JwtRotationApplication {
10+
public static void main(String[] args) {
11+
SpringApplication.run(JwtRotationApplication.class, args);
12+
}
13+
}

0 commit comments

Comments
 (0)