feat: overnight-runner 入 SDD、dev-build 全栈编排,并落地 ACC-account

将 overnight-runner skill 无前缀并入套件;dev-build 编排自测/UI/代码评审;指南左侧导航;example 实现账号能力与 progress。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
ning-inspire
2026-11-17 16:56:57 +08:00
parent 972a031654
commit fc7521d6f9
154 changed files with 6688 additions and 498 deletions

View File

@@ -1,10 +1,11 @@
openapi: 3.0.0
info:
title: Consultation Platform API (skeleton)
version: 0.0.0
title: Consultation Platform API
version: 0.1.0
description: |
业务 API 尚未实现。目标需求见 docs/features/iteration1/。
当前仅保留健康探针。
咨询预约平台 API。当前已实现 ACC(账号与个人中心)。
需求来源:docs/features/iteration1/ACC-account/spec.md
技术方案:docs/features/iteration1/ACC-account/plan.md
paths:
/api/v1/health:
get:
@@ -20,3 +21,280 @@ paths:
status:
type: string
example: UP
/api/v1/auth/sms/send:
post:
summary: 发送登录 / 注册验证码
security: []
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [phone]
properties:
phone: { type: string, example: '13800138000' }
responses:
'204': { description: 已接受(不区分新老号,防枚举) }
'400': { description: 手机号非法 / 参数校验失败 }
'409': { description: 发送过于频繁 }
/api/v1/auth/sms/login:
post:
summary: 验证码登录或注册(默认 USER 角色)
security: []
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [phone, code]
properties:
phone: { type: string }
code: { type: string }
nickname: { type: string, description: 新号可选,缺省生成 }
responses:
'200':
description: token + account + profile
content:
application/json:
schema: { $ref: '#/components/schemas/AuthLoginResponse' }
'400': { description: 验证码错误 / 过期 / 手机号非法 }
'409': { description: 账号不可用(已禁用) }
/api/v1/auth/admin/login:
post:
summary: 管理员密码登录
security: []
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [phone, password]
properties:
phone: { type: string }
password: { type: string }
responses:
'200':
description: token + account
content:
application/json:
schema: { $ref: '#/components/schemas/AuthLoginResponse' }
'400': { description: 账号或密码不正确 }
'409': { description: 账号不可用(已禁用) }
/api/v1/me:
get:
summary: 当前账号与资料
security: [{ bearerAuth: [] }]
responses:
'200':
description: MeResponse
content:
application/json:
schema: { $ref: '#/components/schemas/MeResponse' }
'401': { description: 未登录 / 登录失效 / 账号不可用 }
/api/v1/me/profile:
put:
summary: 更新本人资料
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
nickname: { type: string, minLength: 2, maxLength: 30 }
avatarUrl: { type: string, maxLength: 500 }
email: { type: string, maxLength: 254 }
cityCode: { type: string, maxLength: 20 }
locale: { type: string, maxLength: 16, example: zh-CN }
responses:
'200':
description: UserProfileResponse
content:
application/json:
schema: { $ref: '#/components/schemas/ProfileResponse' }
'400': { description: 参数校验失败 }
'401': { description: 未登录 / 登录失效 }
/api/v1/me/favorites:
get:
summary: 收藏列表
security: [{ bearerAuth: [] }]
responses:
'200':
description: 收藏列表
content:
application/json:
schema:
type: array
items: { $ref: '#/components/schemas/FavoriteResponse' }
'401': { description: 未登录 / 登录失效 }
post:
summary: 收藏咨询师(幂等)
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [consultantId]
properties:
consultantId: { type: string }
responses:
'200':
description: 收藏记录(重复收藏返回同一条)
content:
application/json:
schema: { $ref: '#/components/schemas/FavoriteResponse' }
'400': { description: 咨询师不存在或不可展示 }
'401': { description: 未登录 / 登录失效 }
/api/v1/me/favorites/{consultantId}:
delete:
summary: 取消收藏(幂等)
security: [{ bearerAuth: [] }]
parameters:
- name: consultantId
in: path
required: true
schema: { type: string }
responses:
'204': { description: OK(未收藏也返回成功) }
'401': { description: 未登录 / 登录失效 }
/api/v1/me/agreements:
get:
summary: 查询本人协议同意记录
security: [{ bearerAuth: [] }]
responses:
'200':
description: 同意记录
content:
application/json:
schema:
type: array
items: { $ref: '#/components/schemas/AgreementConsentResponse' }
'401': { description: 未登录 / 登录失效 }
post:
summary: 记录协议同意(按类型 + 版本幂等)
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [agreementType, agreementVersion]
properties:
agreementType: { type: string, enum: [service, privacy] }
agreementVersion: { type: string, maxLength: 20 }
responses:
'200':
description: 同意记录
content:
application/json:
schema: { $ref: '#/components/schemas/AgreementConsentResponse' }
'400': { description: 协议类型 / 版本不合法 }
'401': { description: 未登录 / 登录失效 }
/api/v1/admin/accounts/{id}/disable:
post:
summary: 禁用账号(使现有会话失效)
security: [{ bearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [reason]
properties:
reason: { type: string, maxLength: 200 }
responses:
'200':
description: 账号视图(手机号脱敏)
content:
application/json:
schema: { $ref: '#/components/schemas/AccountResponse' }
'400': { description: 账号不存在 / 原因缺失 }
'401': { description: 未登录 / 登录失效 }
'403': { description: 非管理员 }
/api/v1/admin/accounts/{id}/enable:
post:
summary: 启用账号
security: [{ bearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
responses:
'200':
description: 账号视图(手机号脱敏)
content:
application/json:
schema: { $ref: '#/components/schemas/AccountResponse' }
'400': { description: 账号不存在 }
'401': { description: 未登录 / 登录失效 }
'403': { description: 非管理员 }
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
schemas:
AuthLoginResponse:
type: object
properties:
token: { type: string }
account: { $ref: '#/components/schemas/AccountResponse' }
profile: { $ref: '#/components/schemas/ProfileResponse' }
MeResponse:
type: object
properties:
account: { $ref: '#/components/schemas/AccountResponse' }
profile: { $ref: '#/components/schemas/ProfileResponse' }
AccountResponse:
type: object
properties:
id: { type: string }
roles:
type: array
items: { type: string, enum: [USER, CONSULTANT, ADMIN] }
status: { type: string, enum: [ACTIVE, DISABLED] }
phone: { type: string, nullable: true, description: 本人明文,其他视角脱敏 }
phoneVerifiedAt: { type: string, format: date-time }
lastLoginAt: { type: string, format: date-time, nullable: true }
disabledReason: { type: string, nullable: true }
createdAt: { type: string, format: date-time }
updatedAt: { type: string, format: date-time }
ProfileResponse:
type: object
properties:
accountId: { type: string }
nickname: { type: string }
avatarUrl: { type: string, nullable: true }
email: { type: string, nullable: true }
cityCode: { type: string, nullable: true }
locale: { type: string, example: zh-CN }
updatedAt: { type: string, format: date-time }
FavoriteResponse:
type: object
properties:
id: { type: string }
consultantId: { type: string }
createdAt: { type: string, format: date-time }
AgreementConsentResponse:
type: object
properties:
id: { type: string }
agreementType: { type: string, enum: [service, privacy] }
agreementVersion: { type: string }
consentedAt: { type: string, format: date-time }
sourceIp: { type: string }

View File

@@ -1,9 +1,84 @@
# 数据库规格
> 当前为工程空壳:**无业务表**。
> 目标需求:`docs/features/iteration1/`。实现各 feature 时通过 Flyway 新增表,并同步本文档。
> 当前已实现 ACC(账号与个人中心)的业务表。
> 其余 feature 按 `docs/features/iteration1/` 继续通过 Flyway 新增表,并同步本文档。
## 迁移
- 目录:`containers/backend/src/main/resources/db/migration/`
- 现状:无 migration 脚本(仅占位)
- 回滚脚本目录:`containers/backend/src/main/resources/db/rollback/`(Flyway 不自动执行,人工按需回滚)
| 版本 | 脚本 | 说明 |
|---|---|---|
| 20261117 | `V20261117__acc_account.sql` | ACC 账号与个人中心(5 张表) |
## 表清单
### account(账号)
| 字段 | 类型 | 约束 | 说明 |
|---|---|---|---|
| id | VARCHAR(64) | PK | UUID 字符串 |
| phone_hash | VARCHAR(64) | NOT NULL,UNIQUE(`uk_account_phone_hash`) | 手机号摘要(HMAC-SHA256,带密钥),唯一登录标识 |
| phone_cipher | VARCHAR(512) | NOT NULL | 手机号密文(AES-256-GCM),仅本人可解密展示 |
| roles_json | VARCHAR(128) | NOT NULL | 角色 JSON 数组,如 `["USER"]` |
| status | VARCHAR(32) | NOT NULL | `ACTIVE` / `DISABLED` |
| phone_verified_at | DATETIME(3) | NOT NULL | 最近一次手机号验证时间 |
| last_login_at | DATETIME(3) | NULL | 最近登录时间 |
| disabled_reason | VARCHAR(200) | NULL | 禁用原因(禁用时必填) |
| token_version | INT | NOT NULL DEFAULT 0 | 会话版本,禁用/启用时 +1,使旧 JWT 失效 |
| password_hash | VARCHAR(255) | NULL | 仅 ADMIN 账号使用(BCrypt) |
| created_at / updated_at | DATETIME(3) | NOT NULL | 创建/更新时间 |
### user_profile(用户资料)
| 字段 | 类型 | 约束 | 说明 |
|---|---|---|---|
| account_id | VARCHAR(64) | PK,FK→account(id) | 账号 ID |
| nickname | VARCHAR(30) | NOT NULL | 昵称(2~30 字符) |
| avatar_url | VARCHAR(500) | NULL | 头像地址 |
| email | VARCHAR(254) | NULL | 邮箱 |
| city_code | VARCHAR(20) | NULL | 城市编码 |
| locale | VARCHAR(16) | NOT NULL DEFAULT 'zh-CN' | 语言 |
| updated_at | DATETIME(3) | NOT NULL | 资料更新时间 |
### favorite_consultant(收藏咨询师)
| 字段 | 类型 | 约束 | 说明 |
|---|---|---|---|
| id | VARCHAR(64) | PK | 收藏记录 ID |
| account_id | VARCHAR(64) | NOT NULL,FK→account(id),索引 `idx_fav_account` | 收藏人 |
| consultant_id | VARCHAR(64) | NOT NULL | 被收藏咨询师(与 account_id 组成唯一键 `uk_fav_account_consultant`) |
| created_at | DATETIME(3) | NOT NULL | 收藏时间 |
### agreement_consent(协议同意记录)
| 字段 | 类型 | 约束 | 说明 |
|---|---|---|---|
| id | VARCHAR(64) | PK | 记录 ID(不可变、不可物理删除) |
| account_id | VARCHAR(64) | NOT NULL,FK→account(id),索引 `idx_consent_account` | 同意人 |
| agreement_type | VARCHAR(32) | NOT NULL | `SERVICE` / `PRIVACY`(对外小写 `service` / `privacy`) |
| agreement_version | VARCHAR(20) | NOT NULL | 协议版本 |
| consented_at | DATETIME(3) | NOT NULL | 同意时间 |
| source_ip | VARCHAR(45) | NOT NULL | 来源 IP |
唯一键 `uk_consent_account_type_ver(account_id, agreement_type, agreement_version)` 保证同版本幂等。
### sms_verification_code(短信验证码)
| 字段 | 类型 | 约束 | 说明 |
|---|---|---|---|
| id | VARCHAR(64) | PK | 记录 ID |
| phone_hash | VARCHAR(64) | NOT NULL,索引 `idx_sms_phone_created(phone_hash, created_at)` | 手机号摘要 |
| code_hash | VARCHAR(64) | NOT NULL | 验证码摘要(不存明文) |
| expires_at | DATETIME(3) | NOT NULL | 过期时间(默认 5 分钟) |
| fail_count | INT | NOT NULL DEFAULT 0 | 连续失败次数(达 5 次作废) |
| used_at | DATETIME(3) | NULL | 使用 / 作废时间 |
| created_at | DATETIME(3) | NOT NULL | 创建时间(重发间隔按此判断) |
## 约定
- 字符集 `utf8mb4` / 排序 `utf8mb4_bin`,引擎 InnoDB,表名单数小写下划线。
- 时间统一按 `Asia/Shanghai` 存储(`DATETIME(3)`),领域层用 `Instant`,转换见 `TimeConverter`。
- 测试库不用 Flyway:改用 `src/test/resources/schema.sql`(结构)与 `data.sql`(种子)。
管理员种子账号由 `AccountSeedInitializer` 在运行时创建(密文与部署环境密钥相关)。

View File

@@ -1,13 +1,88 @@
# 领域模型
> 当前为工程空壳:**无业务聚合**。
> 目标需求:`docs/features/iteration1/`(ACC~PLT)。按 feature plan 落地后回写本文档。
> 当前已实现 ACC(账号与个人中心)。
> 其余 feature 按 `docs/features/iteration1/`(COB~PLT)继续落地后回写本文档。
## 聚合根
### Account(账号)
统一账号:终端用户 / 咨询师 / 管理员共用一套账号体系。
| 字段 | 类型 | 说明 |
|---|---|---|
| id | String | UUID 字符串主键 |
| phoneHash | String | 手机号摘要,唯一登录标识 |
| phoneCipher | String | 手机号密文,仅本人可解密展示 |
| roles | Set\<AccountRole\> | 角色集合:`USER` / `CONSULTANT` / `ADMIN` |
| status | AccountStatus | `ACTIVE` / `DISABLED` |
| phoneVerifiedAt / lastLoginAt | Instant | 验证时间 / 最近登录时间 |
| disabledReason | String | 禁用原因 |
| tokenVersion | int | 会话版本;禁用/启用时 +1,令旧 JWT 立即失效 |
| passwordHash | String | 仅 ADMIN 使用(BCrypt) |
行为:
| 方法 | 说明 |
|---|---|
| `registerByPhone(...)` | 手机号注册,默认授予 `USER` 角色 |
| `createAdmin(...)` | 平台分配管理员(R-COMMON-001,不开放自助注册) |
| `grantConsultantRole()` | 幂等授予 `CONSULTANT`(供 COB 审核通过后调用) |
| `disable(reason)` / `enable()` | 状态流转 + 抬升 `tokenVersion`;重复调用幂等 |
| `touchLogin(now)` | 记录最近登录时间 |
| `assertUsable()` | 账号禁用时抛 `IllegalStateException`(提示不可用原因) |
状态机:
```
ACTIVE --disable(reason)--> DISABLED (tokenVersion +1)
DISABLED --enable()------> ACTIVE (tokenVersion +1)
```
### UserProfile(用户资料)
`accountId` / `nickname` / `avatarUrl` / `email` / `cityCode` / `locale` / `updatedAt`。
昵称 2~30 字符、邮箱格式与长度、城市编码长度在领域层校验。
### FavoriteConsultant(收藏咨询师)
`id` / `accountId` / `consultantId` / `createdAt`;(accountId, consultantId) 唯一。
### AgreementConsent(协议同意记录)
`id` / `accountId` / `agreementType` / `agreementVersion` / `consentedAt` / `sourceIp`;
不可变、不可物理删除;(accountId, type, version) 唯一。
### SmsVerificationCode(短信验证码)
`id` / `phoneHash` / `codeHash` / `expiresAt` / `failCount` / `usedAt` / `createdAt`;
连续失败 5 次作废。
## 领域服务与端口
| 组件 | 层 | 说明 |
|---|---|---|
| `SmsCodeService` | domain/service | 验证码签发 / 校验 / TTL / 失败次数作废 / 重发限流(纯 Java,由 `DomainServiceConfig` 注册) |
| `PhoneCrypto` | domain/service | 手机号摘要与加解密端口(实现 `AesPhoneCrypto`:HMAC-SHA256 摘要 + AES-256-GCM 密文) |
| `SmsSender` | domain/service | 短信发送端口(实现 `MockSmsSender`,真通道归 PLT) |
| `DisplayableConsultantPort` | domain/service | 咨询师「可展示」判定端口(实现 `StubDisplayableConsultantPort`,CDC/COB 落地后替换) |
| `TokenIssuer` | domain/service | 令牌签发端口(实现 `JwtTokenProvider`) |
| `PasswordService` | domain/service | 密码编码端口(实现 `PasswordServiceImpl`,BCrypt) |
## 值对象与枚举(domain/vo)
| 类型 | 取值 | 说明 |
|---|---|---|
| `AccountRole` | USER / CONSULTANT / ADMIN | 角色,JWT authority = `ROLE_<name>` |
| `AccountStatus` | ACTIVE / DISABLED | 账号状态 |
| `AgreementType` | SERVICE(`service`) / PRIVACY(`privacy`) | 协议类型,对外用小写码 |
| `PhoneNumber` | — | 手机号规范化(去空格/横线)与格式校验 |
## 平台骨架(非业务聚合)
| 组件 | 说明 |
|------|------|
| JWT / UserContext | 无状态鉴权骨架,待 ACC 接入登录签发 |
| JWT / UserContext | 无状态鉴权:Claims `sub` / `roles` / `tokenVersion`;过滤器校验账号状态与版本 |
| IBusinessRule / BusinessRuleConfig | 业务规则框架空注册 |
| DataDictionary* | 数据字典框架类型,暂无注册枚举 |
| DataDictionary* | 数据字典框架类型,已注册 `AccDataDictionaryType`(账号状态 / 角色 / 协议类型) |
| PasswordService | 密码编码接口(BCrypt 实现) |

View File

@@ -1,7 +1,8 @@
# ADR-004: 采用 JWT HMAC-SHA256 无状态鉴权
- **状态**: 已接受
- **状态**: 已接受(2026-11-17 扩展 Claims,方案本身不变)
- **日期**: 2026-06-19
- **扩展记录**: 2026-11-17 · ACC(账号与个人中心)落地,Claims 扩展为多角色 + 会话版本
## 上下文
@@ -11,10 +12,24 @@
后端自行签发与校验 JWT(HMAC-SHA256,24h 过期),无状态鉴权。
JWT Payload:
- `sub`: userId
- `email`: 用户邮箱
- `role`: ROLE_USER / ROLE_ADMIN
JWT Payload(as-built):
- `sub`: 账号 ID(`account.id`)
- `roles`: 角色数组,如 `["USER","CONSULTANT","ADMIN"]`(authority = `ROLE_<name>`)
- `tokenVersion`: 会话版本,与 `account.token_version` 比对
- `iat` / `exp`: 签发 / 过期时间
### Claims 扩展(2026-11-17)
原约定的 `email` 与单值 `role` Claims 移除,改为:
| 变更 | 原因 |
|---|---|
| `role` → `roles` 数组 | 同一账号可同时持有 USER / CONSULTANT / ADMIN 三种角色(HLD §3.2) |
| 新增 `tokenVersion` | 管理员禁用账号时抬升库内版本,令既有 JWT 立即失效(R-UPDATE-003 / AC-005) |
`JwtAuthenticationFilter` 在验签后需查库比对账号状态与 `tokenVersion`:
账号不存在 / 已禁用 / 版本不一致 → 不放行,返回 401 并带明确原因。
## 备选方案
@@ -22,7 +37,8 @@ JWT Payload:
|------|------|----------|
| Session + Cookie | Spring Security 默认方案 | 需 Redis 存储 session、跨域配置复杂、不适合前后端分离 |
| OAuth2 / OIDC | 标准协议 | 增加 IDP 依赖,小型项目不需要 |
| JWT HMAC-SHA256 | 无状态、无额外存储 | 无法主动使 token 失效 |
| JWT HMAC-SHA256 | 无状态、无额外存储 | 无法主动使 token 失效(本次以 `tokenVersion` 补齐) |
| JWT + 撤销名单(黑名单) | 精确吊销 | 需持久化与额外查询,收益有限;`tokenVersion` 已满足账号级失效 |
## 后果
@@ -30,8 +46,10 @@ JWT Payload:
- 无状态、无需 Redis
- 前后端分离友好(`Authorization: Bearer <token>`)
- 后端自行签发,无外部依赖
- 账号级会话失效无需维护令牌黑名单
**负面**:
- Token 签发后无法主动吊销(24h 内有效)
- 每个已认证请求需查库一次(比对状态与版本),有额外开销
- Token 仍无法按请求粒度吊销(24h 内有效,账号级失效除外)
- 密钥保存在配置中,泄漏风险需注意
- 载荷明文字段不宜存放敏感信息
- 载荷明文字段不宜存放敏感信息(手机号不入载荷,只放账号 ID)

View File

@@ -1,6 +1,18 @@
# 数据字典
> 空壳:示例字典枚举已清除。框架类型保留(`Description` / `DataDictionaryType` / `DataDictionaryUtil`)。
> 实现 iteration1 时注册字典并回写本文档。
> 框架类型保留(`Description` / `DataDictionaryType` / `DataDictionaryUtil`)。
> ACC 已注册 `AccDataDictionaryType`(`domain/datadictionary/AccDataDictionaryType.java`)。
当前注册字典类型:**无**。
## 当前注册字典类型
| 类型 | 说明 | 枚举来源 |
|---|---|---|
| `AccDataDictionaryType.ACCOUNT_STATUS` | 账号状态 | `AccountStatus`(ACTIVE / DISABLED) |
| `AccDataDictionaryType.ACCOUNT_ROLE` | 账号角色 | `AccountRole`(USER / CONSULTANT / ADMIN) |
| `AccDataDictionaryType.AGREEMENT_TYPE` | 协议类型 | `AgreementType`(service / privacy) |
## 新增枚举的约定
1. 枚举放在 `domain/vo/`,实现 `Description`(`getCode()` / `getDescription()`)。
2. 在 `AccDataDictionaryType`(或后续 feature 的字典类型)中登记。
3. 回写本文档的「当前注册字典类型」表。

View File

@@ -1,7 +1,6 @@
# 错误码清单
> 框架约定(空壳 as-built)。业务专属错误码待 iteration1 实现后补充。
> 与 `GlobalExceptionHandler` 及 `docs/standards/error.md` 对齐。
> as-built(含 ACC)。与 `GlobalExceptionHandler` 及 `docs/standards/error.md` 对齐。
## 当前 Handler 映射
@@ -12,6 +11,29 @@
| `MethodArgumentNotValidException` | 400 | `VALIDATION_ERROR` | `@Valid` 失败 |
| 其它 `Exception` | 500 | `SYSTEM_ERROR` | 兜底(对外「系统繁忙」) |
鉴权失败由 Spring Security 直接返回 **401 / 403**,不走 `ErrorResponse`。
响应体字段:`errorCode` / `errorMessage` / `data`。
## 鉴权失败(Spring Security 直接返回,不经 GlobalExceptionHandler)
| 场景 | HTTP | errorCode | errorMessage 来源 |
|---|---|---|---|
| 未携带 / 无效 / 过期 token | 401 | `UNAUTHORIZED` | 「未登录或登录已过期」 |
| 账号被禁用 | 401 | `UNAUTHORIZED` | 「账号不可用:<禁用原因>」 |
| 版本不一致(账号被禁用/启用后) | 401 | `UNAUTHORIZED` | 「登录状态已失效,请重新登录」 |
| 已登录但无权限(如非 ADMIN 访问 `/admin/**`) | 403 | `FORBIDDEN` | 「无权访问该资源」 |
实现:`RestAuthenticationEntryPoint`(401)/ `RestAccessDeniedHandler`(403),
响应体沿用与 `GlobalExceptionHandler.ErrorResponse` 相同的字段结构,便于前端统一处理。
## ACC 典型错误文案
| 场景 | HTTP | errorCode | errorMessage |
|---|---|---|---|
| 验证码错误 | 400 | `PARAM_ERROR` | 验证码错误 |
| 验证码已使用 / 过期 / 不存在 | 400 | `PARAM_ERROR` | 验证码已使用,请重新获取 / 验证码已过期,请重新获取 / 验证码不存在或已过期 |
| 验证码连续错误 5 次 | 400 | `PARAM_ERROR` | 验证码连续错误次数过多,已作废 |
| 手机号格式非法 | 400 | `PARAM_ERROR` | 手机号格式不正确 |
| 发码过于频繁 | 409 | `BUSINESS_CONFLICT` | 验证码发送过于频繁,请稍后再试 |
| 账号被禁用时登录 | 409 | `BUSINESS_CONFLICT` | 账号不可用:<禁用原因> |
| 咨询师不存在或不可展示 | 400 | `PARAM_ERROR` | 咨询师不存在或不可展示 |
| 管理员账号或密码错误 | 400 | `PARAM_ERROR` | 账号或密码不正确 |

View File

@@ -1,30 +1,73 @@
# 安全设计
> 平台骨架 as-built。登录/注册等业务 API 待 ACC 实现。
> as-built(含 ACC 账号与个人中心)。
## JWT Token
**算法**:HMAC-SHA256(`jjwt`)
**配置**:`jwt.secret` / `jwt.expiration`(毫秒)
**配置**:`jwt.secret` / `jwt.expiration`(毫秒,默认 24h)
**Claims(约定)**:`sub`=userId,`email`,`role`(如 `ROLE_USER` / `ROLE_ADMIN`),`iat`,`exp`
**Claims(as-built)**:
| Claim | 说明 |
|---|---|
| `sub` | 账号 ID(`account.id`) |
| `roles` | 角色数组,如 `["USER","CONSULTANT"]` |
| `tokenVersion` | 会话版本,与 `account.token_version` 比对 |
| `iat` / `exp` | 签发 / 过期时间 |
> 旧约定中的 `email` / `role` 单值 Claims 已移除(见 ADR-004 的 Claims 扩展)。
## 处理流程
```
请求 → JwtAuthenticationFilter
提取 Bearer token → 校验 → UserContextHolder + SecurityContextHolder
提取 Bearer token → 验签 → 解析 claims
→ 查库取账号:不存在 / 已禁用 / tokenVersion 不一致 → 不放行(401,带原因)
→ 写 UserContextHolder(accountId + roles)+ SecurityContextHolder(authority = ROLE_*)
finally 清除 ThreadLocal / SecurityContext
```
## 当前放行
**会话失效**:管理员禁用 / 启用账号会抬升 `tokenVersion`,旧 JWT 与库内版本不一致 → 立即 401。
账号被禁用时过滤器返回提示「账号不可用:<原因>」。
- `GET /api/v1/health` — permitAll
- 其它 `/api/**` — authenticated(无签发入口时仅能带有效 token 访问)
## 角色与端点
| 端点 | 要求 |
|---|---|
| `GET /api/v1/health` | permitAll |
| `POST /api/v1/auth/**` | permitAll(发码 / 验证码登录 / 管理员登录) |
| `/api/v1/admin/**` | `ROLE_ADMIN`(否则 403) |
| 其它 `/api/**` | authenticated(401) |
角色授权:JWT authority 为 `ROLE_USER` / `ROLE_CONSULTANT` / `ROLE_ADMIN`,
与 Spring Security `hasRole` 对齐;`SecurityUtils.hasRole("CONSULTANT")` 同时接受带/不带 `ROLE_` 前缀。
## 手机号保护
| 项 | 做法 |
|---|---|
| 存储 | `phone_hash`(HMAC-SHA256,带 `phone.crypto.key`)+ `phone_cipher`(AES-256-GCM) |
| 查询 | 唯一索引建在 `phone_hash` 上,不存明文可比对字段 |
| 展示 | 仅本人(`/api/v1/me`)返回明文;管理端等其它视角脱敏为 `138****8000` |
| 密钥 | `phone.crypto.key` 由环境变量注入,生产禁止入仓 |
## 密码
- 仅 ADMIN 账号使用密码登录;BCrypt 存储(`PasswordService` / `PasswordServiceImpl`)。
- ADMIN 账号由平台种子创建(`AccountSeedInitializer`),不开放自助注册或角色提升。
## 短信验证码
- 库内只存 `code_hash`,不存明文。
- TTL 默认 5 分钟;连续错误 5 次即作废;同一手机号 60 秒内不可重发(`BUSINESS_CONFLICT`)。
- 发送通道本期为 `MockSmsSender`(固定码仅本地联调 / E2E 使用,生产留空)。
## 相关类
- `JwtTokenProvider` / `JwtAuthenticationFilter`
- `JwtTokenProvider`(实现 `TokenIssuer`)/ `JwtAuthenticationFilter`
- `UserContext` / `UserContextHolder` / `SecurityUtils`
- `RestAuthenticationEntryPoint`(401)/ `RestAccessDeniedHandler`(403)
- `PasswordService` / `PasswordServiceImpl`(BCrypt)
- `AesPhoneCrypto` / `MockSmsSender` / `StubDisplayableConsultantPort`

View File

@@ -0,0 +1,87 @@
# 实现进度:plan.md
> 关联 plan:./plan.md
> 状态:已完成
> 更新:2026-11-17 全部步骤完成(后端 49 自测 / 前端构建 / QA E2E 10 用例全绿)
## Checklist
### 1. 领域模型、枚举、仓库接口与端口(含 PhoneCrypto / SmsSender / DisplayableConsultantPort)
- [x] 枚举 `AccountRole` / `AccountStatus` / `AgreementType`(实现 `Description`,可注册数据字典)
- [x] 值对象 `PhoneNumber`(规范化 + 格式校验)
- [x] 聚合根 `Account`(角色集合、状态机、tokenVersion、登录校验)
- [x] 实体 `UserProfile` / `FavoriteConsultant` / `AgreementConsent` / `SmsVerificationCode`
- [x] 仓库接口 `AccountRepository` / `UserProfileRepository` / `FavoriteConsultantRepository` /
`AgreementConsentRepository` / `SmsVerificationCodeRepository`
- [x] 端口 `PhoneCrypto` / `SmsSender` / `DisplayableConsultantPort` / `TokenIssuer`
- [x] 领域服务 `SmsCodeService`(签发、校验、TTL、失败 5 次作废、重发限流)
### 2. Flyway + PO/Mapper/Converter/RepositoryImpl + 测试 schema/data 种子 ADMIN
- [x] Flyway `V20261117__acc_account.sql`(§3 DDL 全量新建)
- [x] 回滚脚本 `db/rollback/V20261117__acc_account_rollback.sql`
- [x] PO:`AccountPO` / `UserProfilePO` / `FavoriteConsultantPO` / `AgreementConsentPO` / `SmsVerificationCodePO`
- [x] Mapper ×5
- [x] Converter ×5(roles_json 序列化、时间转换)
- [x] RepositoryImpl ×5
- [x] 基础设施实现:`AesPhoneCrypto` / `MockSmsSender` / `StubDisplayableConsultantPort`
- [x] 测试 `schema.sql` / `data.sql`(ADMIN 由 `AccountSeedInitializer` 运行时创建,密文与密钥相关)
### 3. Auth / Profile / Favorite / Agreement / Admin 应用服务与 Assembler、事务与幂等
- [x] `AuthAppService`(发码 / 验证码登录注册合一 / 管理员密码登录)
- [x] `ProfileAppService`(me 查询 / 资料更新)
- [x] `FavoriteAppService`(收藏幂等 / 取消幂等)
- [x] `AgreementAppService`(协议同意幂等 + 查询)
- [x] `AdminAccountAppService`(禁用 / 启用,抬 tokenVersion)
- [x] Assembler ×4(Account / Profile / Favorite / Agreement)
### 4. REST API、Security/JWT/UserContext 改造、全局错误语义对齐
- [x] `JwtTokenProvider`:Claims 扩展 `roles`(数组)+ `tokenVersion`
- [x] `JwtAuthenticationFilter`:校验 status=ACTIVE 且 tokenVersion 一致,否则 401
- [x] `UserContext` / `SecurityUtils`:角色集合 + `hasRole`
- [x] `SecurityConfig`:放行 `POST /api/v1/auth/**`;`/api/v1/admin/**` 需 ROLE_ADMIN
- [x] `AuthController` / `MeController` / `AdminAccountController`
- [x] DTO:Command/Request + Response(`adapter/rest/dto/`)
### 5. 前端登录页与个人中心(对齐原型路由)、auth store 与拦截器
- [x] `views/auth/Login.vue`(路由 `/user/login`)
- [x] `views/user/Profile.vue`(路由 `/user/profile`)
- [x] `layouts/UserLayout.vue`
- [x] `services/authService.ts` / `profileService.ts` / `favoriteService.ts` / `agreementService.ts`
- [x] `stores/auth.ts`:token + user + roles
- [x] `router/index.ts`:路由 + 守卫
- [x] `utils/request.ts`:401 跳转、400/403/409 提示
### 6. 开发自测(单元 + API)与 E2E/QA 文本用例挂钩
- [x] 单元测试:`AccountTest`(状态机 / 角色幂等 / tokenVersion)
- [x] 单元测试:`SmsCodeServiceTest`(失败 5 次作废 / 过期)
- [x] 单元测试:`PhoneNumberTest`(规范化 / hash 稳定性)
- [x] API 集成测试:`AuthControllerTest`(AC-001、R-CREATE-002)
- [x] API 集成测试:`MeControllerTest`(AC-002、AC-003、AC-004、PR-001)
- [x] API 集成测试:`AdminAccountControllerTest`(AC-005)
- [x] QA 文本用例写入 `docs/test-cases/e2e-cases/test-cases.md`(TC-001~TC-009)
- [x] E2E 自动化 `e2e/tests/qa/acc-account.spec.ts`(10 passed)
### 7. 同步 `docs/facts/` 与 JWT ADR Claims 说明
- [x] `docs/facts/api.yaml`
- [x] `docs/facts/db-schema.md`
- [x] `docs/facts/domain-model.md`
- [x] `docs/facts/tech-topic/security.md` / `data-dict.md` / `004-jwt-stateless-auth.md` / `error-code.md`
- [x] `AGENTS.md` §8 聚合简述
### 8. 验证与上线准备
- [x] 执行 Flyway 迁移 `V20261117__acc_account.sql`(本地 `consultation` 库按 plan §11.3 重建)
- [x] 回滚脚本就位 `db/rollback/V20261117__acc_account_rollback.sql`
- [x] 冒烟用例(plan §13 六步)全部通过
- [x] `mvn test` 全绿(49 用例)
- [x] `npm run build` 通过
- [x] `npm run test:qa` 全绿(10 用例)
## 备注
- **测试库**:测试环境使用本机 MySQL `consultation_test`(`src/test/resources/application.yml`)。
AGENTS.md §10 写的是 MariaDB4j + `@ActiveProfiles("test")`,与工程现状不一致 —— **本次未改**(属既有工程问题,已记录)。
- **管理员种子**:ADMIN 账号由 `AccountSeedInitializer` 运行时创建(密文与 `phone.crypto.key` 相关,
无法固化为 data.sql 静态行),测试用例每个 `@BeforeEach` 清库后重建。
- **前端单元测试**:本次未引入 Vitest(工程尚未配置),前端行为由 E2E(`e2e/tests/qa/`)覆盖。
- **接口层 DTO 位置**:按 plan §11.1,`*Response` 放在 `adapter/rest/dto/`,由 `application/assembler/*`
组装;因此 application 层存在对 adapter DTO 包的引用(分层细则的一处已知例外)。

View File

@@ -0,0 +1,628 @@
# ACC · 账号与个人中心 — 技术方案
>日期: 2026-11-17
>
>关联需求:docs/features/iteration1/ACC-account/spec.md
>
>关联 HLD:docs/features/iteration1/high-level-design.md(**已确认** 2026-11-17)
>
>**plan一旦定稿就不修改。** 二次改造新建 `plan-{slug}.md`。
> **实施进度**:由 `dev-build` 维护同目录 `plan-progress.md`;细项只写 progress,不回写本 plan。
>
> 状态:**待确认** — 确认前不写业务实现代码。
## 1. 文档说明
### 1.1 背景
- 工程为空壳;ACC 是其余模块的鉴权与账号前置(HLD §4:Account / 角色由 ACC 提供)。
- 规格:手机号验证码注册登录、多角色 Account、资料、收藏、协议同意、管理员禁用与会话失效。
- 原型:`prototypes` `/user/login`、`/user/profile`(样式参考;规则以 spec 为准)。
### 1.2 目标
- **实现目标**:落地 AC-001~AC-005 与 R-CREATE/QUERY/UPDATE/DELETE/COMMON 相关规则;JWT 上下文可被后续 feature 复用。
- **技术边界**:
- 遵循 HLD / ADR-001~005;路径 `containers/backend`、`containers/frontend`。
- **不**实现 OAuth、完整 RBAC 配置台、咨询师档案(COB)、可展示检索(CDC)。
- 短信通道:本期 **SmsSender 端口 + 本地/测试 Mock**(真短信归后续 PLT,不阻塞 ACC)。
- 收藏的「可展示咨询师」校验:ACC 定义 `DisplayableConsultantPort`;本期提供 **可替换 Stub**(测试种子 ID 可展示),CDC/COB Plan 替换为真实现。
- 扩展 JWT Claims:`roles` 多值 + `tokenVersion`(禁用抬版本使旧会话失效);不改为 Session。
### 1.3 关键决策(本 Plan)
| ID | 决策 | 说明 |
|---|---|---|
| P-001 | USER/CONSULTANT:手机号 + 短信验证码登录/注册合一 | 已有手机号不重复建号(R-CREATE-001) |
| P-002 | ADMIN:平台种子/分配账号 + **密码**登录(独立鉴权) | 不开放自助注册 ADMIN(R-COMMON-001) |
| P-003 | 手机号:存 `phone_hash`(唯一查询)+ `phone_cipher`(可逆展示给本人) | 满足「库内加密」与唯一登录 |
| P-004 | 禁用抬升 `token_version`,JWT 携带同名字段,过滤器不一致则 401 | 满足「现有会话失效」 |
| P-005 | 验证码:DB 表存储(无 Redis 依赖),TTL + 失败次数 | 连续 5 次失败作废(R-CREATE-002) |
---
## 2. 领域模型(PlantUML)
```plantuml
@startuml
entity Account {
* id : String
--
* phoneHash : String
* phoneCipher : String
* roles : Set<AccountRole>
* status : AccountStatus
phoneVerifiedAt : Instant
lastLoginAt : Instant
disabledReason : String
* tokenVersion : int
passwordHash : String
* createdAt : Instant
* updatedAt : Instant
}
entity UserProfile {
* accountId : String
* nickname : String
avatarUrl : String
email : String
cityCode : String
* locale : String
* updatedAt : Instant
}
entity FavoriteConsultant {
* id : String
* accountId : String
* consultantId : String
* createdAt : Instant
}
entity AgreementConsent {
* id : String
* accountId : String
* agreementType : AgreementType
* agreementVersion : String
* consentedAt : Instant
* sourceIp : String
}
enum AccountRole {
USER
CONSULTANT
ADMIN
}
enum AccountStatus {
ACTIVE
DISABLED
}
enum AgreementType {
SERVICE
PRIVACY
}
Account ||--|| UserProfile
Account ||--o{ FavoriteConsultant
Account ||--o{ AgreementConsent
@enduml
```
聚合根:**Account**(角色集合、状态机、tokenVersion、登录校验)。
UserProfile / FavoriteConsultant / AgreementConsent:由 Account 用例编排持久化;AgreementConsent **不可变、不可物理删**。
领域能力(示意):
- `Account.registerOrLoginByPhone(...)` / `Account.loginAsAdmin(...)`
- `Account.disable(reason)` / `Account.enable()` → 抬升 `tokenVersion`
- `Account.grantConsultantRole()` — 幂等(供 COB 调用,ACC 暴露 Domain/App API)
- `Account.assertUsable()` — disabled 拒绝
---
## 3. 数据库设计(SQL DDL)
Flyway:`containers/backend/src/main/resources/db/migration/V20261117__acc_account.sql`
测试:`schema.sql` / `data.sql` 同步;种子 1 个 ADMIN(手机号约定见验证步骤)。
```sql
CREATE TABLE account (
id VARCHAR(64) NOT NULL PRIMARY KEY,
phone_hash VARCHAR(64) NOT NULL,
phone_cipher VARCHAR(512) NOT NULL,
roles_json VARCHAR(128) NOT NULL COMMENT 'JSON array e.g. ["USER"]',
status VARCHAR(32) NOT NULL,
phone_verified_at DATETIME(3) NOT NULL,
last_login_at DATETIME(3) NULL,
disabled_reason VARCHAR(200) NULL,
token_version INT NOT NULL DEFAULT 0,
password_hash VARCHAR(255) NULL COMMENT 'ADMIN only',
created_at DATETIME(3) NOT NULL,
updated_at DATETIME(3) NOT NULL,
UNIQUE KEY uk_account_phone_hash (phone_hash)
);
CREATE TABLE user_profile (
account_id VARCHAR(64) NOT NULL PRIMARY KEY,
nickname VARCHAR(30) NOT NULL,
avatar_url VARCHAR(500) NULL,
email VARCHAR(254) NULL,
city_code VARCHAR(20) NULL,
locale VARCHAR(16) NOT NULL DEFAULT 'zh-CN',
updated_at DATETIME(3) NOT NULL,
CONSTRAINT fk_profile_account FOREIGN KEY (account_id) REFERENCES account(id)
);
CREATE TABLE favorite_consultant (
id VARCHAR(64) NOT NULL PRIMARY KEY,
account_id VARCHAR(64) NOT NULL,
consultant_id VARCHAR(64) NOT NULL,
created_at DATETIME(3) NOT NULL,
UNIQUE KEY uk_fav_account_consultant (account_id, consultant_id),
KEY idx_fav_account (account_id),
CONSTRAINT fk_fav_account FOREIGN KEY (account_id) REFERENCES account(id)
);
CREATE TABLE agreement_consent (
id VARCHAR(64) NOT NULL PRIMARY KEY,
account_id VARCHAR(64) NOT NULL,
agreement_type VARCHAR(32) NOT NULL,
agreement_version VARCHAR(20) NOT NULL,
consented_at DATETIME(3) NOT NULL,
source_ip VARCHAR(45) NOT NULL,
KEY idx_consent_account (account_id),
UNIQUE KEY uk_consent_account_type_ver (account_id, agreement_type, agreement_version),
CONSTRAINT fk_consent_account FOREIGN KEY (account_id) REFERENCES account(id)
);
CREATE TABLE sms_verification_code (
id VARCHAR(64) NOT NULL PRIMARY KEY,
phone_hash VARCHAR(64) NOT NULL,
code_hash VARCHAR(64) NOT NULL,
expires_at DATETIME(3) NOT NULL,
fail_count INT NOT NULL DEFAULT 0,
used_at DATETIME(3) NULL,
created_at DATETIME(3) NOT NULL,
KEY idx_sms_phone_created (phone_hash, created_at)
);
```
---
## 4. API 设计(OpenAPI)
前缀 `/api/v1`。错误体沿用 `GlobalExceptionHandler`(`PARAM_ERROR` / `BUSINESS_CONFLICT` / `VALIDATION_ERROR`)。
```yaml
openapi: 3.0.0
info:
title: ACC Account API
version: 0.1.0
paths:
/api/v1/auth/sms/send:
post:
summary: 发送登录/注册验证码
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [phone]
properties:
phone: { type: string }
responses:
'204': { description: 已接受(防枚举不区分新老号) }
'400': { description: 手机号非法 / 发送过频 }
/api/v1/auth/sms/login:
post:
summary: 验证码登录或注册(默认 USER)
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [phone, code]
properties:
phone: { type: string }
code: { type: string }
nickname: { type: string, description: 新号可选,缺省生成 }
responses:
'200':
description: token + account + profile
/api/v1/auth/admin/login:
post:
summary: 管理员密码登录
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [phone, password]
properties:
phone: { type: string }
password: { type: string }
responses:
'200': { description: token + account }
/api/v1/me:
get:
summary: 当前账号与资料
security: [{ bearerAuth: [] }]
responses:
'200': { description: MeResponse }
/api/v1/me/profile:
put:
summary: 更新本人资料
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
nickname: { type: string }
avatarUrl: { type: string }
email: { type: string }
cityCode: { type: string }
locale: { type: string }
responses:
'200': { description: UserProfileResponse }
/api/v1/me/favorites:
get:
summary: 收藏列表
security: [{ bearerAuth: [] }]
responses:
'200': { description: FavoriteItem[] }
post:
summary: 收藏咨询师(幂等)
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [consultantId]
properties:
consultantId: { type: string }
responses:
'200': { description: FavoriteItem }
/api/v1/me/favorites/{consultantId}:
delete:
summary: 取消收藏(幂等成功)
security: [{ bearerAuth: [] }]
responses:
'204': { description: OK }
/api/v1/me/agreements:
post:
summary: 记录协议同意(按类型+版本幂等)
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [agreementType, agreementVersion]
properties:
agreementType: { type: string, enum: [service, privacy] }
agreementVersion: { type: string }
responses:
'200': { description: AgreementConsentResponse }
get:
summary: 查询本人同意记录
security: [{ bearerAuth: [] }]
responses:
'200': { description: AgreementConsentResponse[] }
/api/v1/admin/accounts/{id}/disable:
post:
summary: 禁用账号
security: [{ bearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [reason]
properties:
reason: { type: string, maxLength: 200 }
responses:
'200': { description: AccountResponse }
/api/v1/admin/accounts/{id}/enable:
post:
summary: 启用账号
security: [{ bearerAuth: [] }]
responses:
'200': { description: AccountResponse }
```
SecurityConfig:放行 `POST /api/v1/auth/**`;`/api/v1/admin/**` 需 `ROLE_ADMIN`(JWT authority);`/api/v1/me/**` 需登录。
咨询师端路由守卫(前端 + 可选后端 `/api/v1/consultant/**` 预留):无 `CONSULTANT` 角色 → 403(EX-003);本 Plan 至少提供 **鉴权工具方法** `SecurityUtils.hasRole("CONSULTANT")`,完整咨询师 API 属后续 feature。
---
## 5. 时序图(PlantUML)
### 5.1 验证码登录/注册
```plantuml
@startuml
actor User
participant AuthController
participant AuthAppService
participant SmsCodeService
participant Account
participant AccountRepository
participant JwtTokenProvider
User -> AuthController : POST /auth/sms/send
AuthController -> AuthAppService : sendCode(phone)
AuthAppService -> SmsCodeService : issue + SmsSender.send
AuthAppService --> User : 204
User -> AuthController : POST /auth/sms/login
AuthController -> AuthAppService : loginOrRegister(phone, code)
AuthAppService -> SmsCodeService : verify (failCount/expire)
AuthAppService -> AccountRepository : findByPhoneHash
alt 新用户
AuthAppService -> Account : create USER + profile
AuthAppService -> AccountRepository : save
else 已存在
AuthAppService -> Account : assertUsable + touchLogin
end
AuthAppService -> JwtTokenProvider : issue(sub, roles, tokenVersion)
AuthAppService --> User : 200 token + me
@enduml
```
### 5.2 管理员禁用 → 旧 Token 失效
```plantuml
@startuml
actor Admin
participant AdminAccountController
participant AccountAppService
participant Account
participant Filter as JwtAuthenticationFilter
Admin -> AdminAccountController : POST .../disable {reason}
AdminAccountController -> AccountAppService : disable(id, reason)
AccountAppService -> Account : disable → tokenVersion++
AccountAppService --> Admin : 200
User -> Filter : Bearer oldJwt (旧 version)
Filter -> Filter : version != account.tokenVersion
Filter --> User : 401
@enduml
```
---
## 6. 专题设计
### 6.1 权限设计
- JWT HMAC-SHA256;Claims:`sub`=accountId,`roles`(数组),`tokenVersion`,`iat`/`exp`。
- Filter:验签 → 查库或缓存校验 status=ACTIVE 且 version 一致 → `UserContext`(accountId + roles)。
- PR-001~004:未登录 401;admin 接口 `hasRole('ADMIN')`;无 CONSULTANT 访问咨询师端 403。
### 6.2 并发控制
- 收藏 / 协议同意:DB 唯一键 + 捕获冲突后转幂等成功(R-CREATE-004、同意幂等)。
- 发码:同手机号短时限流(如 60s 内不可重发)→ `BUSINESS_CONFLICT` 或 `PARAM_ERROR`(文案明确)。
### 6.3 事务设计
- `AuthAppService.loginOrRegister`、`updateProfile`、`disable` 等写用例 `@Transactional` 在 application 层。
### 6.4 异常处理
| 场景 | 异常 | errorCode |
|---|---|---|
| 验证码错误/过期/作废 | IllegalArgumentException | PARAM_ERROR |
| 账号禁用 | IllegalStateException 或统一 401 + 明确文案 | BUSINESS_CONFLICT / 401 |
| 非法状态流转 | IllegalStateException | BUSINESS_CONFLICT |
| 收藏不可展示咨询师 | IllegalArgumentException | PARAM_ERROR |
### 6.5 性能设计
- `phone_hash` / 收藏唯一索引;验证码按 phone_hash + 时间检索最近一条。
- 头像仅存 URL(上传服务本期可 Mock 前端直传占位,不建对象存储)。
### 6.6 扩展性设计
- `SmsSender`、`PhoneCrypto`、`DisplayableConsultantPort` 端口在 domain/application,infrastructure 可替换。
- `grantConsultantRole` 供 COB 应用服务调用(同进程 Bean),不开放用户自助提权。
---
## 7. 一致性约束
- 字段枚举与 spec §5 一致:`active/disabled` → DB/API 用 `ACTIVE`/`DISABLED`(对外 JSON 可用小写枚举,须在 Assembler 统一)。
- 协议类型 API:`service` / `privacy` ↔ 领域 `SERVICE` / `PRIVACY`。
- 角色 JWT authority:`ROLE_USER` / `ROLE_CONSULTANT` / `ROLE_ADMIN`(与 Spring `hasRole` 对齐)。
- 手机号仅本人 `me` 接口返回明文(解密);列表/管理视图按 R-QUERY-002 脱敏(管理员审计场景可后续 ADM 细化,本期 admin disable 按 id 操作即可)。
- 命名:API camelCase,DB snake_case。
---
## 8. 单元测试设计
### 8.1 范围
- `Account` 状态机、角色幂等授予、tokenVersion。
- `SmsCodeService` 校验/失败次数/过期。
- Phone 规范化与 hash 稳定性。
### 8.5 用例清单(摘要)
| 被测单元 | 场景 | 预期 |
|---|---|---|
| Account.disable | 填原因 | status=DISABLED,version+1 |
| Account.disable | 重复禁用 | 幂等 |
| Account.grantConsultantRole | 已有角色 | 不变 |
| SmsCodeService.verify | 错 5 次 | 作废 |
| SmsCodeService.verify | 过期 | 失败 |
---
## 9. API 集成测试设计
### 9.1 范围
RestAssured + 现有 TestBase/ResetDb;需可用 MySQL(或项目既定测试库)。覆盖 AC-001~005 主路径。
### 9.5 用例清单
| 接口 | 场景 | 预期 | 关联 |
|---|---|---|---|
| POST /auth/sms/login | 新号+正确码 | 200,角色含 USER | AC-001 |
| POST /auth/sms/login | 错误码 | 400 | R-CREATE-002 |
| PUT /me/profile | 本人修改 | 200 再 GET 一致 | AC-002 |
| PUT /me/profile | 无 token | 401 | PR-001 |
| POST/DELETE favorites | 幂等收藏取消 | 列表正确 | AC-003 |
| POST /me/agreements | 双协议版本 | 落库 IP/时间 | AC-004 |
| POST admin disable + 旧 token | 访问 /me | 401 | AC-005 |
---
## 10. E2E 测试设计
### 10.1 范围
对齐原型路由:`/user/login`、`/user/profile`。文本用例写入 `docs/test-cases/e2e-cases/`(QA),自动化 `e2e/tests/qa/`(可与 qa-write-cases 并行补)。
### 10.6 用例清单
| 链路 | 步骤 | 验证 |
|---|---|---|
| 注册登录 | 发码(测试后门或固定 Mock 码)→ 登录 | 进入个人中心 |
| 改资料 | 改昵称保存 | 刷新仍在 |
| 收藏 | 收藏种子咨询师 → 取消 | 列表变化 |
| 禁用 | Admin 禁用后用户旧会话 | 被踢/无法访问 |
测试环境验证码:`SmsSender` Mock + 测试配置固定码或读最新码表(仅 test profile)。
---
## 11. 修改清单
### 11.1 关键文件速查
| 操作 | 文件 | 备注 |
| :--: | :-- | :-- |
| 新增 | `.../domain/aggregate/Account.java` | 聚合根 |
| 新增 | `.../domain/aggregate/UserProfile.java` 等 | 实体/值对象 |
| 新增 | `.../domain/repository/AccountRepository.java` | |
| 新增 | `.../domain/service/PhoneCrypto.java`、`SmsSender.java`、`DisplayableConsultantPort.java` | 端口 |
| 新增 | `.../application/service/AuthAppService.java`、`ProfileAppService.java`、`FavoriteAppService.java`、`AgreementAppService.java`、`AdminAccountAppService.java` | |
| 新增 | `.../application/assembler/*Assembler.java` | |
| 新增 | `.../adapter/rest/AuthController.java`、`MeController.java`、`AdminAccountController.java` | |
| 新增 | `.../adapter/rest/dto/**` | Command/Response |
| 新增 | `.../infrastructure/persistence/po|mapper|converter/**` | |
| 新增 | `.../infrastructure/repositoryimpl/*RepositoryImpl.java` | |
| 新增 | `.../infrastructure/sms/MockSmsSender.java` | |
| 新增 | `.../infrastructure/crypto/AesPhoneCrypto.java` | |
| 新增 | `.../infrastructure/consultant/StubDisplayableConsultantPort.java` | |
| 修改 | `.../infrastructure/security/JwtTokenProvider.java` | roles + tokenVersion |
| 修改 | `.../infrastructure/security/JwtAuthenticationFilter.java` | version/status 校验 |
| 修改 | `.../infrastructure/security/UserContext.java` | roles 集合 |
| 修改 | `.../infrastructure/config/SecurityConfig.java` | 放行 auth |
| 新增 | `db/migration/V20261117__acc_account.sql` | |
| 修改 | `src/test/resources/schema.sql`、`data.sql` | |
| 新增 | `src/test/java/.../AuthControllerTest.java` 等 | |
| 新增 | `containers/frontend/src/views/auth/Login.vue`、`views/user/Profile.vue` | |
| 修改 | `containers/frontend/src/router/index.ts`、`stores/auth.ts`、`services/*` | |
| 修改 | `docs/facts/api.yaml`、`db-schema.md`、`domain-model.md`、`tech-topic/security.md`、`data-dict.md` | 实施后 |
| 修改 | `docs/facts/tech-topic/004-jwt-stateless-auth.md` | Claims 扩展说明(非推翻 HMAC) |
### 11.2 接口变更
| 路径 | 方法 | 类型 | 说明 |
|---|---|---|---|
| `/api/v1/auth/sms/send` | POST | 新增 | 发码 |
| `/api/v1/auth/sms/login` | POST | 新增 | 登录/注册 |
| `/api/v1/auth/admin/login` | POST | 新增 | 管理员 |
| `/api/v1/me` | GET | 新增 | 当前用户 |
| `/api/v1/me/profile` | PUT | 新增 | 改资料 |
| `/api/v1/me/favorites` | GET/POST | 新增 | 收藏 |
| `/api/v1/me/favorites/{consultantId}` | DELETE | 新增 | 取消 |
| `/api/v1/me/agreements` | GET/POST | 新增 | 协议 |
| `/api/v1/admin/accounts/{id}/disable\|enable` | POST | 新增 | 禁用启用 |
### 11.3 数据库变更
- 脚本:`V20261117__acc_account.sql`(§3 全量新建)。
- 无历史业务表迁移;本地旧库建议重建。
---
## 12. 实施步骤(粗)
1. 领域模型、枚举、仓库接口与端口(含 PhoneCrypto / SmsSender / DisplayableConsultantPort)
2. Flyway + PO/Mapper/Converter/RepositoryImpl + 测试 schema/data 种子 ADMIN
3. Auth / Profile / Favorite / Agreement / Admin 应用服务与 Assembler、事务与幂等
4. REST API、Security/JWT/UserContext 改造、全局错误语义对齐
5. 前端登录页与个人中心(对齐原型路由)、auth store 与拦截器
6. 开发自测(单元 + API)与 E2E/QA 文本用例挂钩
7. 同步 `docs/facts/` 与 JWT ADR Claims 说明
---
## 13. 验证步骤
1. `POST /api/v1/auth/sms/send` + Mock 码 → `POST .../login` → 200 含 token;`GET /api/v1/me` 成功。
2. 改昵称 `PUT /me/profile` → 再 GET 一致。
3. 对 Stub 可展示 `consultantId` 收藏/取消,重复操作不报错。
4. 提交 service+privacy 同意后 GET 可见版本与 IP。
5. Admin 登录 → disable 用户 → 用户旧 Bearer 访问 `/me` 为 401。
6. `mvn test`、前端 `npm run build`;可选 `e2e` 冒烟登录页。
---
## 14. 风险与回滚
| 风险 | 缓解 |
|---|---|
| JWT Claims 变更影响空壳旧 token | 空壳无业务 token;文档标明不兼容旧 demo |
| 收藏依赖咨询师主数据 | Stub 端口 + 种子 ID;CDC 替换前 E2E 用种子 |
| 手机加密密钥泄漏 | 密钥仅配置/环境变量;禁止入仓 |
| 无 Redis 验证码表膨胀 | TTL 任务或登录前清理过期行(可简单实现) |
回滚:Revert 合并提交 + 删除 Flyway 脚本并重建库(无生产数据)。
---
## 15. 实施后需更新的事实文档
- `docs/facts/api.yaml`
- `docs/facts/db-schema.md`
- `docs/facts/domain-model.md`
- `docs/facts/tech-topic/security.md`、`data-dict.md`、`004-jwt-stateless-auth.md`(Claims)
- `docs/facts/tech-topic/error-code.md`(若新增对外文案映射)
- `AGENTS.md` §8 聚合简述(Account 等)
- `docs/test-cases/e2e-cases/test-cases.md`(ACC 相关 TC)
**docs 同步检查**:涉及。
---
## 16. 变更记录
| 版本 | 日期 | 变更内容 |
| ---- | ---------- | -------- |
| v1.0 | 2026-11-17 | 初稿,待确认 |
---
## 确认清单(人工)
- [ ] 同意 P-001~P-005 与 Stub 收藏端口策略
- [ ] 同意 ADMIN 密码登录 + USER 短信登录
- [ ] 确认后进入 TDD / `dev-build`(先 progress,不改本 plan)

View File

@@ -3,7 +3,8 @@
> 状态:**v0.1**(字段、规则、验收与原型已完成)
> 日期:2026-11-16
> 原型:独立 Vite Demo(Element Plus),见 [`prototypes/`](./prototypes/)(端口 5174)
> **实现**:业务代码已清空为工程骨架;请按各 feature 的 plan 重新落地(见 `_chores/plan-20261117-purge-demo-impl-for-iteration1.md`)。
> **实现**:按各 feature 的 plan 逐个落地。**ACC 已实现**(见 [`ACC-account/plan.md`](./ACC-account/plan.md) 与 [`ACC-account/plan-progress.md`](./ACC-account/plan-progress.md));
> 其余 feature 仍为工程骨架(见 `_chores/plan-20261117-purge-demo-impl-for-iteration1.md`)。
> **高阶设计**:[high-level-design.md](./high-level-design.md) — **确认前不得写 feature Plan**。
## 1. 产品概述
@@ -17,7 +18,7 @@
| Code | Spec | 覆盖源 REQ / 章节 | 角色 | 状态 |
|---|---|---|---|---|
| ACC | [ACC-account/spec.md](./ACC-account/spec.md) | §6.1 + REQ-006 | 三端 | v0.1 |
| ACC | [ACC-account/spec.md](./ACC-account/spec.md) | §6.1 + REQ-006 | 三端 | v0.1 · **已实现** |
| CDC | [CDC-consultant-discovery/spec.md](./CDC-consultant-discovery/spec.md) | REQ-001、002 | 用户 | v0.1 |
| BKG | [BKG-booking/spec.md](./BKG-booking/spec.md) | REQ-003、004、005 | 用户 | v0.1 |
| COB | [COB-consultant-onboarding/spec.md](./COB-consultant-onboarding/spec.md) | REQ-007、008 | 咨询师 | v0.1 |

View File

@@ -5,7 +5,7 @@
> 版本:v0.1
> 作者:TL
> 日期:2026-11-17
> 状态:**待确认** — 确认前不得编写 feature `plan.md` 或开始实现
> 状态:**已确认**(人工确认 2026-11-17)— 可对各 feature 执行 `dev-plan`
---
@@ -91,19 +91,19 @@
| ID | 问题 | 倾向 | 状态 |
|---|---|---|---|
| OD-001 | 支付网关首期范围(微信 / 支付宝 / 模拟) | 待 TL 拍板 | 开放 |
| OD-002 | 档期时区与夏令时策略 | Asia/Shanghai,存 UTC | 提议 |
| OD-003 | 评价公开可见性与敏感词策略 | 过词后公开 | 提议 |
| OD-001 | 支付网关首期范围(微信 / 支付宝 / 模拟) | 首期用模拟网关;真网关另开 ADR | **延期**(不影响 ACC~CSC 开工;阻塞 BKG/SET 支付联调前须关闭) |
| OD-002 | 档期时区与夏令时策略 | Asia/Shanghai,存 UTC | **采纳**(CSC/BKG Plan 遵循) |
| OD-003 | 评价公开可见性与敏感词策略 | 过词后公开 | **延期**(BKG 评价 Plan 前关闭) |
---
## 6. 确认清单
- [ ] 范围与非目标与迭代 README / 九份 spec 一致
- [ ] 技术边界与既有 ADR 无冲突(或已更新 ADR)
- [ ] 跨模块契约权属清晰,无双头所有
- [ ] 开放决策已关闭或标明延期不影响开工顺序
- [ ] **人工确认**本文件后,方可对各 feature 执行 `dev-plan`
- [x] 范围与非目标与迭代 README / 九份 spec 一致
- [x] 技术边界与既有 ADR 无冲突(或已更新 ADR)
- [x] 跨模块契约权属清晰,无双头所有
- [x] 开放决策已关闭或标明延期不影响开工顺序(OD-001/003 延期;OD-002 采纳)
- [x] **人工确认**本文件(2026-11-17)后,方可对各 feature 执行 `dev-plan`
---

View File

@@ -6,8 +6,15 @@
> - **不写 `TC-` 编号**(`TC-` 是 QA 用例编号空间)
> - 自测全绿 ≠ 需求已验收
## 本迭索引(示例)
## 本迭索引
### ACC · 账号与个人中心
| 场景 | 位置 | 备注 |
|---|---|---|
| (按 Plan 补) | `containers/backend/src/test/...` | Happy Path + 失败分支 |
| 账号状态机、角色幂等授予、tokenVersion | `containers/backend/src/test/java/com/consultation/domain/aggregate/AccountTest.java` | 禁用/启用幂等、禁用拒绝登录 |
| 验证码签发 / 校验 / 失败 5 次作废 / 过期 / 重发限流 | `containers/backend/src/test/java/com/consultation/domain/service/SmsCodeServiceTest.java` | 领域服务(纯 Java) |
| 手机号规范化与格式校验 | `containers/backend/src/test/java/com/consultation/domain/vo/PhoneNumberTest.java` | 值对象 |
| 发码、验证码登录注册、管理员登录 | `containers/backend/src/test/java/com/consultation/adapter/rest/AuthControllerTest.java` | AC-001、R-CREATE-001/002 |
| 本人资料、收藏幂等、协议同意 | `containers/backend/src/test/java/com/consultation/adapter/rest/MeControllerTest.java` | AC-002/003/004、PR-001 |
| 管理员禁用 / 启用、会话失效、越权拦截 | `containers/backend/src/test/java/com/consultation/adapter/rest/AdminAccountControllerTest.java` | AC-005、PR-003 |

View File

@@ -1,6 +1,97 @@
# E2E 测试用例(QA)
> **归属:QA 测试。** 旧示例 TC 已清除。
> 请按 `docs/features/iteration1/` 各 spec 的验收标准重新编写 `TC-xxx`,再实现 `e2e/tests/qa/`。
> **归属:QA 测试。** 判定权与定义权归 QA。
> 自动化实现见 `e2e/tests/qa/`,编号一一对应。
> 需求来源:`docs/features/iteration1/ACC-account/spec.md`(AC-001~AC-005 / R-CREATE-* / R-QUERY-* / R-UPDATE-* / R-DELETE-*)。
当前基线:无业务验收用例(仅工程壳冒烟见自动化占位)。
## 说明
- 运行环境:`bash e2e-run.sh`(前端 :5173 + 后端 :8080)。
- 测试环境验证码:Mock 短信通道固定码 `123456`(后端 `SMS_MOCK_FIXED_CODE`)。
- 平台管理员种子账号:`13900000000` / `Admin@12345`(后端 `seed.admin.*`)。
- 每个用例使用独立手机号,避免同号登录态互相干扰。
---
## ACC · 账号与个人中心
### TC-001 手机号验证码注册登录
| 项 | 内容 |
|---|---|
| 关联 | AC-001、R-CREATE-001 |
| 前置 | 未登录;手机号 `1{随机 10 位}` 未注册 |
| 步骤 | 1. 打开 `/user/login`;2. 输入手机号,点击「获取验证码」;3. 输入验证码,勾选协议;4. 点击「登录 / 注册」 |
| 预期 | 1. 提示验证码已发送;2. 登录成功并跳转到 `/user/profile`;3. 个人中心展示昵称、手机号;4. 账号角色为 USER,状态 active |
### TC-002 验证码错误时拒绝登录
| 项 | 内容 |
|---|---|
| 关联 | R-CREATE-002、EX-001 |
| 前置 | 未登录;已获取验证码 |
| 步骤 | 1. 打开 `/user/login`;2. 输入手机号与错误验证码 `000000`;3. 点击「登录 / 注册」 |
| 预期 | 登录被拒绝,页面提示「验证码错误」,停留在登录页,未产生登录态 |
### TC-003 修改个人资料后仍然生效
| 项 | 内容 |
|---|---|
| 关联 | AC-002、R-UPDATE-001 |
| 前置 | 已登录(TC-001 的账号) |
| 步骤 | 1. 进入 `/user/profile`;2. 修改昵称、邮箱、城市并保存;3. 刷新页面 |
| 预期 | 1. 保存成功提示;2. 刷新后昵称 / 邮箱 / 城市为最新值 |
### TC-004 未登录访问个人中心被拦截
| 项 | 内容 |
|---|---|
| 关联 | PR-001 |
| 前置 | 未登录(清空本地登录态) |
| 步骤 | 1. 直接访问 `/user/profile` |
| 预期 | 被重定向到 `/user/login`,不展示个人中心内容 |
### TC-005 收藏咨询师
| 项 | 内容 |
|---|---|
| 关联 | AC-003、R-CREATE-004 |
| 前置 | 已登录 |
| 步骤 | 1. 进入 `/user/profile`;2. 在「我的咨询师」输入可展示咨询师 ID `stub-consultant-001`;3. 点击「收藏」 |
| 预期 | 提示收藏成功,收藏列表出现该咨询师;重复点击收藏不报错且列表不重复 |
### TC-006 取消收藏(幂等)
| 项 | 内容 |
|---|---|
| 关联 | AC-003、R-DELETE-001 |
| 前置 | 已收藏 `stub-consultant-001` |
| 步骤 | 1. 在收藏列表点击「取消收藏」并确认;2. 再次对同一咨询师执行取消 |
| 预期 | 1. 列表不再展示该咨询师;2. 重复取消不报错,仍为空列表 |
### TC-007 协议同意记录
| 项 | 内容 |
|---|---|
| 关联 | AC-004、R-CREATE-003 |
| 前置 | 已登录并完成协议勾选 |
| 步骤 | 1. 登录(勾选协议);2. 进入 `/user/profile` 查看「协议同意记录」 |
| 预期 | 分别记录服务协议与隐私政策,含协议版本、同意时间与来源 IP;重复登录不产生重复记录 |
### TC-008 管理员禁用账号后旧会话失效
| 项 | 内容 |
|---|---|
| 关联 | AC-005、R-UPDATE-003、EX-002 |
| 前置 | 用户已登录;管理员账号可用 |
| 步骤 | 1. 用户登录并停留在个人中心;2. 管理员以 `13900000000` 登录并禁用该用户账号(原因「QA 禁用」);3. 用户刷新个人中心 |
| 预期 | 1. 禁用成功且账号状态为 disabled;2. 用户旧会话被拒绝(401),前端清除登录态并跳回登录页;3. 该账号再次登录被拒绝并提示账号不可用 |
### TC-009 非管理员不得调用管理端接口
| 项 | 内容 |
|---|---|
| 关联 | PR-003、R-COMMON-001 |
| 前置 | 以普通用户登录 |
| 步骤 | 1. 使用普通用户令牌调用 `POST /api/v1/admin/accounts/{id}/disable` |
| 预期 | 返回 403,用户角色不能自提权或禁用他人账号 |