在网上下载了开源CRM系统来管理销售相关的工作成果,但是对于联系人电话,都是公司的机密资产,不能让人可以直接导出数据表,将所有客户联系人方式都拿走。因此决定对手机字段进行加密,看了下CRM源码,很多地方都是使用select *来选择的表数据,如果直接修改代码,修改的地方会很多。就打算使用mysql触发器在数据插入和更新的时候自动加密数据,我们选择的时候使用视图来将数据进行解密。下面介绍一下如何完成这个操作。
1. 在cst_customer表中添加触发器,在插入之前更新手机字段。
BEGIN
SET new.mobile = to_base64(encode(new.mobile,'salt'));
END
2. 在更新之前更新手机字段。
BEGIN
if old.mobile!=new.mobile then
SET new.mobile = to_base64(encode(new.mobile,'salt'));
end if;
END
3. 创建视图,选择表数据的时候自动解密字段。
create view v_customer
(
customer_id
,name
,customer_no
,create_user_id
,owner_user_id
,create_time
,next_time
,conn_time
,conn_body
,linkman
,source
,grade
,industry
,mobile
,tel
,address
,intro
) as
select
customer_id
,name
,customer_no
,create_user_id
,owner_user_id
,create_time
,next_time
,conn_time
,conn_body
,linkman
,source
,grade
,industry
,decode(from_base64(mobile),'salt') as mobile
,tel
,address
,intro
from cst_customer