1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
@ControllerAdvice @AllArgsConstructor @Slf4j public class UnSensitiveAdvice implements ResponseBodyAdvice<R> {
@Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { Type type = returnType.getGenericParameterType(); String typeName = type.getTypeName(); return typeName.startsWith("com.qw.desensitize.common.R"); }
@Nullable @Override public R beforeBodyWrite(@Nullable R body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { if (body != null) { if (body.getData() != null) { if (body.getData() instanceof UnSensitiveDto) { UnSensitiveDto sensitive = (UnSensitiveDto) body.getData(); if (sensitive.isSensitiveFlag()) { Long start = System.currentTimeMillis(); body.setData(unSensitive(sensitive)); log.warn("脱敏耗时{}毫秒", System.currentTimeMillis() - start); return body; } } else if (body.getData() instanceof List) { List<Object> list = (List<Object>) body.getData(); if (list != null && list.size() > 0) { Object element = list.get(0); if (element instanceof UnSensitiveDto) { UnSensitiveDto sensitive = (UnSensitiveDto) element; if (sensitive.isSensitiveFlag()) { Long start = System.currentTimeMillis(); body.setData(unSensitive(list)); log.warn("脱敏耗时{}毫秒", System.currentTimeMillis() - start); return body; } } } } } } return body; }
private Object unSensitive(Object data) { try { if (data instanceof List) { List<Object> list = (List) data; for (Object o : list) { unSensitive(o); } } else { unSensitiveParam(data); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } return data; }
private void unSensitiveParam(Object data) throws IllegalAccessException { if (data == null) { return; } List<Field> fields = getFields(data.getClass()); for (Field field : fields) { field.setAccessible(true); Class<?> classType = field.getType(); if (classType.getName().startsWith("com.qw.desensitize.dto")) { unSensitiveParam(field.get(data)); } else if (List.class.isAssignableFrom(classType)) { Object objList = field.get(data); if (objList != null) { List<Object> dataList = (List<Object>) objList; for (Object dataParam : dataList) { unSensitiveParam(dataParam); } } } else { UnSensitive annotation = field.getAnnotation(UnSensitive.class); if (annotation != null) { String type = annotation.type(); if (UN_SENSITIVE_EMAIL.equals(type)) { if (field.get(data) != null) { field.set(data, email(String.valueOf(field.get(data)))); } } if (UN_SENSITIVE_PHONE.equals(type)) { if (field.get(data) != null) { field.set(data, phone(String.valueOf(field.get(data)))); } } if (UnSensitiveDto.UN_SENSITIVE_ID_NUM.equals(type)) { if (field.get(data) != null) { field.set(data, idNum(String.valueOf(field.get(data)))); } } } } } }
private List<Field> getFields(Class<?> clazz) { List<Field> list = new ArrayList<>(); Field[] declaredFields = clazz.getDeclaredFields(); list.addAll(Arrays.asList(declaredFields));
Class<?> superclass = clazz.getSuperclass(); if (superclass.getName().startsWith("com.qw.desensitize.dto")) { list.addAll(getFields(superclass)); } return list; }
private String email(String src) { if (src == null) { return null; } String email = src.toString(); int index = StringUtils.indexOf(email, "@"); if (index <= 1) { return email; } else { return StringUtils.rightPad(StringUtils.left(email, 0), index, "*").concat(StringUtils.mid(email, index, StringUtils.length(email))); } }
private String phone(String phone) { if (StringUtils.isBlank(phone)) { return ""; } return phone.replaceAll("(^\\d{0})\\d.*(\\d{4})", "$1****$2"); }
private String idNum(String idNumber) { if (StringUtils.isBlank(idNumber)) { return ""; } if (idNumber.length() == 15 || idNumber.length() == 18) { return idNumber.replaceAll("(\\w{4})\\w*(\\w{4})", "$1*********$2"); } if (idNumber.length() > 4) { return idNumber.replaceAll("(\\w{0})\\w*(\\w{4})", "$1*********$2"); } return "*********"; } }
|