ArtList 手机号加密改造:新增 V2 迁移与 PhoneCrypto,登录注册与查询统一走 phone_hash,对外输出脱敏
自动发布开发环境 Nacos 配置 / publish (push) Successful in 19s
自动发布开发环境 Nacos 配置 / publish (push) Successful in 19s
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
-- =============================================================================
|
||||
-- V2 手机号加密与手机号哈希(ArtList)
|
||||
-- 日期:2026-11-04
|
||||
-- 口径:不做数据迁移,空库初始化。本迁移只调整结构,不回填历史数据。
|
||||
-- 说明:
|
||||
-- - t_user.phone / t_user.contact_phone 改为密文列 phone_cipher / contact_phone_cipher;
|
||||
-- - 新增 phone_hash / contact_phone_hash,用于等值查询与去重;
|
||||
-- - 哈希算法与 Salon 一致:HMAC-SHA256(规范化手机号),规范化为 trim;
|
||||
-- - 加密算法与 Salon 一致:AES/GCM/NoPadding,12 字节随机 IV,128 位 tag。
|
||||
-- 可重复执行:所有 DDL 均先查 information_schema 再执行。
|
||||
-- =============================================================================
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_v2_add_column_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_drop_column_if_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_drop_index_if_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_add_index_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_add_unique_index_if_not_exists;
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE sp_v2_add_column_if_not_exists(
|
||||
IN p_table VARCHAR(64),
|
||||
IN p_column VARCHAR(64),
|
||||
IN p_definition VARCHAR(500)
|
||||
)
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE() AND table_name = p_table AND column_name = p_column
|
||||
) THEN
|
||||
SET @ddl = CONCAT('ALTER TABLE `', p_table, '` ADD COLUMN `', p_column, '` ', p_definition);
|
||||
PREPARE stmt FROM @ddl;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END $$
|
||||
|
||||
CREATE PROCEDURE sp_v2_drop_column_if_exists(
|
||||
IN p_table VARCHAR(64),
|
||||
IN p_column VARCHAR(64)
|
||||
)
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE() AND table_name = p_table AND column_name = p_column
|
||||
) THEN
|
||||
SET @ddl = CONCAT('ALTER TABLE `', p_table, '` DROP COLUMN `', p_column, '`');
|
||||
PREPARE stmt FROM @ddl;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END $$
|
||||
|
||||
CREATE PROCEDURE sp_v2_drop_index_if_exists(
|
||||
IN p_table VARCHAR(64),
|
||||
IN p_index VARCHAR(64)
|
||||
)
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE() AND table_name = p_table AND index_name = p_index
|
||||
) THEN
|
||||
SET @ddl = CONCAT('ALTER TABLE `', p_table, '` DROP INDEX `', p_index, '`');
|
||||
PREPARE stmt FROM @ddl;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END $$
|
||||
|
||||
CREATE PROCEDURE sp_v2_add_index_if_not_exists(
|
||||
IN p_table VARCHAR(64),
|
||||
IN p_index VARCHAR(64),
|
||||
IN p_columns VARCHAR(255)
|
||||
)
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE() AND table_name = p_table AND index_name = p_index
|
||||
) THEN
|
||||
SET @ddl = CONCAT('CREATE INDEX `', p_index, '` ON `', p_table, '` ', p_columns);
|
||||
PREPARE stmt FROM @ddl;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END $$
|
||||
|
||||
CREATE PROCEDURE sp_v2_add_unique_index_if_not_exists(
|
||||
IN p_table VARCHAR(64),
|
||||
IN p_index VARCHAR(64),
|
||||
IN p_columns VARCHAR(255)
|
||||
)
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE() AND table_name = p_table AND index_name = p_index
|
||||
) THEN
|
||||
SET @ddl = CONCAT('CREATE UNIQUE INDEX `', p_index, '` ON `', p_table, '` ', p_columns);
|
||||
PREPARE stmt FROM @ddl;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END $$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL sp_v2_add_column_if_not_exists('t_user', 'phone_cipher', 'varchar(255) NULL COMMENT ''手机号密文(AES-GCM)''');
|
||||
CALL sp_v2_add_column_if_not_exists('t_user', 'phone_hash', 'char(64) NULL COMMENT ''手机号哈希(HMAC-SHA256,十六进制小写)''');
|
||||
CALL sp_v2_add_column_if_not_exists('t_user', 'contact_phone_cipher', 'varchar(255) NULL COMMENT ''联系电话密文(AES-GCM)''');
|
||||
CALL sp_v2_add_column_if_not_exists('t_user', 'contact_phone_hash', 'char(64) NULL COMMENT ''联系电话哈希(HMAC-SHA256,十六进制小写)''');
|
||||
|
||||
CALL sp_v2_drop_index_if_exists('t_user', 'idx_phone');
|
||||
CALL sp_v2_drop_column_if_exists('t_user', 'phone');
|
||||
CALL sp_v2_drop_column_if_exists('t_user', 'contact_phone');
|
||||
|
||||
-- MySQL 唯一索引允许多行 NULL,因此未绑定手机号的用户不受影响。
|
||||
CALL sp_v2_add_unique_index_if_not_exists('t_user', 'uk_user_phone_hash', '(phone_hash)');
|
||||
CALL sp_v2_add_index_if_not_exists('t_user', 'idx_user_contact_phone_hash', '(contact_phone_hash)');
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_v2_add_column_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_drop_column_if_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_drop_index_if_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_add_index_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_v2_add_unique_index_if_not_exists;
|
||||
Reference in New Issue
Block a user