接上文 实现了数据加密储存 https://www.cnblogs.com/wf-zhang/p/14842403.html
新需求 java后台实现数据脱敏即敏感信息部分显示
最终效果如下
思路
1.自己定义脱敏规则 就是隐藏什么数据 自己构建个工具类
实现效果 张* 王*五 欧阳娜* 136******** 410*************** 北京市*************
两个字隐藏第二个字
单个字隐藏中间字
字符数字超过三个 后面的有隐藏
2.在上文中自定义类型处理器解密处理后再加上脱敏处理
开始操作
脱敏工具类
public class DesensitizationUtil { private static final int SIZE = 6; private static final String SYMBOL = "*"; /** * 通用脱敏方法 * @param value * @return */ public static String commonDisplay(String value) { if (null == value || "".equals(value)) { return value; } int len = value.length(); int pamaone = len / 2; int pamatwo = pamaone - 1; int pamathree = len % 3; StringBuilder stringBuilder = new StringBuilder(); if (len <= 2) { if (pamathree == 1) { return SYMBOL; } stringBuilder.append(value.charAt(0)); stringBuilder.append(SYMBOL); } else { if (pamatwo <= 0) { stringBuilder.append(value.substring(0, 1)); stringBuilder.append(SYMBOL); stringBuilder.append(value.substring(len - 1, len)); }else { stringBuilder.append(value.substring(0, 3)); for (int i = 0; i < len-3; i++) { stringBuilder.append(SYMBOL); } } } return stringBuilder.toString(); } }
2.类型处理器解密后再加上脱敏处理
public class AESHandler extends BaseTypeHandler { private final String password = "Vt*************="; @Override public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, AESUtil.encrypt((String)parameter, password)); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { String columnValue = rs.getString(columnName); return DesensitizationUtil.commonDisplay(AESUtil.decrypt(columnValue, password)); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String columnValue = rs.getString(columnIndex); return DesensitizationUtil.commonDisplay(AESUtil.decrypt(columnValue, password)); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String columnValue = cs.getString(columnIndex); return DesensitizationUtil.commonDisplay(AESUtil.decrypt(columnValue, password)); } }