refactor: 默认 skills 装到 .agents,并清空 example 旧演示实现

对齐安装目录与 opencode 扫描路径;按 confirmed plan 清掉与 iteration1 不对齐的示例业务代码与 as-built 文档,只留空壳与需求输入。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
ning-inspire
2026-11-17 11:46:41 +08:00
parent e5abcd101d
commit 670e0c1fa1
155 changed files with 659 additions and 5620 deletions

View File

@@ -1,90 +1,30 @@
# 安全设计
> 本文档描述咨询预约平台的安全架构事实(as-built)。
> 平台骨架 as-built。登录/注册等业务 API 待 ACC 实现。
## JWT Token
**算法**:HMAC-SHA256(`jjwt` 库 MAC 算法)
**算法**:HMAC-SHA256(`jjwt`)
**密钥**:通过 `application.yml` 的 `jwt.secret` 配置,至少 256 位
**配置**:`jwt.secret` / `jwt.expiration`(毫秒)
**有效期**:24 小时(`jwt.expiration` 配置,单位毫秒)
**Claims(约定)**:`sub`=userId,`email`,`role`(如 `ROLE_USER` / `ROLE_ADMIN`),`iat`,`exp`
**Payload Claims**:
| Claim | 类型 | 值 | 说明 |
|-------|------|-----|------|
| sub | String | userId | 用户 UUID |
| email | String | email | 用户邮箱 |
| role | String | ROLE_USER / ROLE_ADMIN | 角色(与 Spring Security `hasRole()` 兼容) |
| iat | Date | 签发时间戳 | -
| exp | Date | 过期时间戳 | -
## JWT 处理流程
## 处理流程
```
请求 → JwtAuthenticationFilter (OncePerRequestFilter)
1. 从 Authorization: Bearer <token> 提取 Token
2. 解析 JWT → userId + email + role
3. 设置 SecurityContextHolder (Spring Security)
4. 设置 UserContextHolder (自定义 ThreadLocal)
5. 放行请求
6. 请求结束后清除 ThreadLocal
请求 → JwtAuthenticationFilter
提取 Bearer token → 校验 → UserContextHolder + SecurityContextHolder
finally 清除 ThreadLocal / SecurityContext
```
## Filter Chain 顺序
## 当前放行
```
Spring Security Filter Chain (SecurityConfig):
1. JwtAuthenticationFilter (自定义, 提取 JWT 并设置上下文)
2. UsernamePasswordAuthenticationFilter (内置, 不启用)
3. ExceptionTranslationFilter (内置, 处理 AccessDenied)
4. ...
```
- `GET /api/v1/health` — permitAll
- 其它 `/api/**` — authenticated(无签发入口时仅能带有效 token 访问)
## SecurityConfig 规则矩阵
## 相关类
| 路径模式 | 访问要求 |
|----------|----------|
| POST /api/v1/auth/register | 完全公开 |
| POST /api/v1/auth/login | 完全公开 |
| GET /api/v1/home/info | 完全公开 |
| GET /api/v1/data-dictionaries** | 完全公开 |
| GET /api/v1/business-rules** | 完全公开 |
| GET /api/v1/consultants** | 完全公开 |
| /admin/** | 仅 ROLE_ADMIN |
| /api/v1/** | 需要认证(任何角色) |
## BCrypt 密码策略
- 编码器:`BCryptPasswordEncoder`(Spring Security 默认实现)
- 强度:10(默认 rounds)
- 密码要求:至少 8 位,由前端校验
## UserContext ThreadLocal
**类**:`UserContextHolder` — `ThreadLocal<UserContext>`
**UserContext 字段**:
| 字段 | 类型 | 来源 |
|------|------|------|
| userId | String | JWT sub |
| email | String | JWT email |
| role | String | JWT role |
**获取**:`SecurityUtils.currentUserId()` — 从 UserContextHolder 获取当前用户 ID
## CORS 配置
- 当前:`WebConfig.java` 允许所有 origin(开发阶段)
- 生产:需限制为具体域名
## 密码修改流程
```
1. 用户输入旧密码 + 新密码
2. 从 DB 获取当前 BCrypt hash
3. passwordService.matches(旧密码, 当前 hash) → 校验
4. passwordService.encode(新密码) → 更新 DB
```
- `JwtTokenProvider` / `JwtAuthenticationFilter`
- `UserContext` / `UserContextHolder` / `SecurityUtils`
- `PasswordService` / `PasswordServiceImpl`(BCrypt)