项目安全要求把所有返回值做加密处理,利用SpringAOP的返回切面可以简单方便的做到该需求。
java">@Aspect
public class ResponseDataEncryptAspect {private ObjectMapper objectMapper;public ResponseDataEncryptAspect () {this.objectMapper = new ObjectMapper();// 保持空值也被序列化objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);}// 拦截Controller内带@RequestMapper的方法及衍生注解即可@AfterReturning(value = PointCut.CONTROLLER_POINT_CUT, returning = "res")public void jsonDateEncrypt(JoinPoint joinPoint, Object res){// 只处理JsonResult返回值if (res instanceof JsonResult) {JsonResult jsonResult = (JsonResult) res;Object data = jsonResult.getData();if (null != data) {// 把data转成json串,再把json串加密,再替换原来的data,vue拦截到data作全局解密,不影响已绑定的功能。String json = objectMapper.writeValueAsString(data);String cryptResult = ZYCryptUtils.encryptAES(json);// 在全局响应结果里添加一个布尔值,方便前端判断到底是密文还是明文jsonResult.setEncrypt(true);jsonResult.setData(cryptResult);}}}
}
实际效果: