mysql数据库后端代码实现字段脱敏

后端代码实现

修改后端xml文件

姓名脱敏

脱敏规则:长度为2,脱敏第二个字;长度为3,脱敏中间的字;长度为4,脱敏第三个字;长度为其他,不脱敏。

(SELECT CONCAT(LEFT(name,1),'*') AS name FROM TABLE WHERE CHAR_LENGTH(name)=2)
UNION
(SELECT CONCAT(LEFT(name,1),'*',RIGHT(name,1)) AS name FROM TABLE WHERE CHAR_LENGTH(name)=3)
UNION
(SELECT CONCAT(LEFT(name,2),'*',RIGHT(name,1)) AS name FROM TABLE WHERE CHAR_LENGTH(name)=4)
UNION
(SELECT name FROM TABLE WHERE CHAR_LENGTH(name)!=2 AND CHAR_LENGTH(name)!=3 AND CHAR_LENGTH(name)!=4)

手机号脱敏

脱敏规则:对于11位的手机号码,保留前三位和后四位,脱敏中间四位

SELECT CONCAT(RPAD(SUBSTRING(mobile,1,3),CHAR_LENGTH(mobile)-4,'*'),RIGHT(mobile,4)) AS mobile FROM TABLE

邮箱脱敏

脱敏规则:对@字符的前两位字母脱敏

SELECT CONCAT(SUBSTRING(email,1,LOCATE('@',email)-3,'**@',SUBSTRING(email,LOCATE('@',email)+1)) AS email FROM TABLE

你可能感兴趣的:(数据库,mysql)