接上文 實現了數據加密儲存 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)); } }
