JavaSpringBoot
    项目有于安全问题,需要对接口整体进行加密处理。
    SpringBoot 接口数据加解密 - 图1
    和产品、前端同学对外需求后,梳理了相关技术方案, 主要的需求点如下:

    1. 尽量少改动,不影响之前的业务逻辑;
    2. 考虑到时间紧迫性,可采用对称性加密方式,服务需要对接安卓、IOS、H5三端,另外考虑到H5端存储密钥安全性相对来说会低一些,故分针对H5和安卓、IOS分配两套密钥;
    3. 要兼容低版本的接口,后面新开发的接口可不用兼容;
    4. 接口有GET和POST两种接口,需要都要进行加解密;

    需求解析:

    1. 服务端、客户端和H5统一拦截加解密,网上有成熟方案,也可以按其他服务中实现的加解密流程来搞;
    2. 使用AES放松加密,考虑到H5端存储密钥安全性相对来说会低一些,故分针对H5和安卓、IOS分配两套密钥;
    3. 本次涉及客户端和服务端的整体改造,经讨论,新接口统一加 /secret/ 前缀来区分

    按本次需求来简单还原问题,定义两个对象,后面用得着,
    用户类:

    1. @Data
    2. public class User {
    3. private Integer id;
    4. private String name;
    5. private UserType userType = UserType.COMMON;
    6. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    7. private LocalDateTime registerTime;
    8. }

    用户类型枚举类:

    1. @Getter
    2. @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    3. public enum UserType {
    4. VIP("VIP用户"),
    5. COMMON("普通用户");
    6. private String code;
    7. private String type;
    8. UserType(String type) {
    9. this.code = name();
    10. this.type = type;
    11. }
    12. }

    构造一个简单的用户列表查询示例:

    1. @RestController
    2. @RequestMapping(value = {"/user", "/secret/user"})
    3. public class UserController {
    4. @RequestMapping("/list")
    5. ResponseEntity<List<User>> listUser() {
    6. List<User> users = new ArrayList<>();
    7. User u = new User();
    8. u.setId(1);
    9. u.setName("boyka");
    10. u.setRegisterTime(LocalDateTime.now());
    11. u.setUserType(UserType.COMMON);
    12. users.add(u);
    13. ResponseEntity<List<User>> response = new ResponseEntity<>();
    14. response.setCode(200);
    15. response.setData(users);
    16. response.setMsg("用户列表查询成功");
    17. return response;
    18. }
    19. }

    调用:localhost:8080/user/list
    查询结果如下,没毛病:

    1. {
    2. "code": 200,
    3. "data": [{
    4. "id": 1,
    5. "name": "boyka",
    6. "userType": {
    7. "code": "COMMON",
    8. "type": "普通用户"
    9. },
    10. "registerTime": "2022-03-24 23:58:39"
    11. }],
    12. "msg": "用户列表查询成功"
    13. }

    目前主要是利用ControllerAdvice来对请求和响应体进行拦截,主要定义SecretRequestAdvice对请求进行加密和SecretResponseAdvice对响应进行加密(实际情况会稍微复杂一点,项目中又GET类型请求,自定义了一个Filter进行不同的请求解密处理)。
    网上的ControllerAdvice使用示例非常多,把两个核心方法展示一下,相信大佬们一看就晓得了,不需多言。上代码:
    SecretRequestAdvice请求解密:

    1. @ControllerAdvice
    2. @Order(Ordered.HIGHEST_PRECEDENCE)
    3. @Slf4j
    4. public class SecretRequestAdvice extends RequestBodyAdviceAdapter {
    5. @Override
    6. public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
    7. return true;
    8. }
    9. @Override
    10. public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
    11. //如果支持加密消息,进行消息解密。
    12. String httpBody;
    13. if (Boolean.TRUE.equals(SecretFilter.secretThreadLocal.get())) {
    14. httpBody = decryptBody(inputMessage);
    15. } else {
    16. httpBody = StreamUtils.copyToString(inputMessage.getBody(), Charset.defaultCharset());
    17. }
    18. //返回处理后的消息体给messageConvert
    19. return new SecretHttpMessage(new ByteArrayInputStream(httpBody.getBytes()), inputMessage.getHeaders());
    20. }
    21. /**
    22. * 解密消息体
    23. *
    24. * @param inputMessage 消息体
    25. * @return 明文
    26. */
    27. private String decryptBody(HttpInputMessage inputMessage) throws IOException {
    28. InputStream encryptStream = inputMessage.getBody();
    29. String requestBody = StreamUtils.copyToString(encryptStream, Charset.defaultCharset());
    30. // 验签过程
    31. HttpHeaders headers = inputMessage.getHeaders();
    32. if (CollectionUtils.isEmpty(headers.get("clientType"))
    33. || CollectionUtils.isEmpty(headers.get("timestamp"))
    34. || CollectionUtils.isEmpty(headers.get("salt"))
    35. || CollectionUtils.isEmpty(headers.get("signature"))) {
    36. throw new ResultException(SECRET_API_ERROR, "请求解密参数错误,clientType、timestamp、salt、signature等参数传递是否正确传递");
    37. }
    38. String timestamp = String.valueOf(Objects.requireNonNull(headers.get("timestamp")).get(0));
    39. String salt = String.valueOf(Objects.requireNonNull(headers.get("salt")).get(0));
    40. String signature = String.valueOf(Objects.requireNonNull(headers.get("signature")).get(0));
    41. String privateKey = SecretFilter.clientPrivateKeyThreadLocal.get();
    42. ReqSecret reqSecret = JSON.parseObject(requestBody, ReqSecret.class);
    43. String data = reqSecret.getData();
    44. String newSignature = "";
    45. if (!StringUtils.isEmpty(privateKey)) {
    46. newSignature = Md5Utils.genSignature(timestamp + salt + data + privateKey);
    47. }
    48. if (!newSignature.equals(signature)) {
    49. // 验签失败
    50. throw new ResultException(SECRET_API_ERROR, "验签失败,请确认加密方式是否正确");
    51. }
    52. try {
    53. String decrypt = EncryptUtils.aesDecrypt(data, privateKey);
    54. if (StringUtils.isEmpty(decrypt)) {
    55. decrypt = "{}";
    56. }
    57. return decrypt;
    58. } catch (Exception e) {
    59. log.error("error: ", e);
    60. }
    61. throw new ResultException(SECRET_API_ERROR, "解密失败");
    62. }
    63. }

    SecretResponseAdvice响应加密:

    1. @ControllerAdvice
    2. public class SecretResponseAdvice implements ResponseBodyAdvice {
    3. private Logger logger = LoggerFactory.getLogger(SecretResponseAdvice.class);
    4. @Override
    5. public boolean supports(MethodParameter methodParameter, Class aClass) {
    6. return true;
    7. }
    8. @Override
    9. public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    10. // 判断是否需要加密
    11. Boolean respSecret = SecretFilter.secretThreadLocal.get();
    12. String secretKey = SecretFilter.clientPrivateKeyThreadLocal.get();
    13. // 清理本地缓存
    14. SecretFilter.secretThreadLocal.remove();
    15. SecretFilter.clientPrivateKeyThreadLocal.remove();
    16. if (null != respSecret && respSecret) {
    17. if (o instanceof ResponseBasic) {
    18. // 外层加密级异常
    19. if (SECRET_API_ERROR == ((ResponseBasic) o).getCode()) {
    20. return SecretResponseBasic.fail(((ResponseBasic) o).getCode(), ((ResponseBasic) o).getData(), ((ResponseBasic) o).getMsg());
    21. }
    22. // 业务逻辑
    23. try {
    24. String data = EncryptUtils.aesEncrypt(JSON.toJSONString(o), secretKey);
    25. // 增加签名
    26. long timestamp = System.currentTimeMillis() / 1000;
    27. int salt = EncryptUtils.genSalt();
    28. String dataNew = timestamp + "" + salt + "" + data + secretKey;
    29. String newSignature = Md5Utils.genSignature(dataNew);
    30. return SecretResponseBasic.success(data, timestamp, salt, newSignature);
    31. } catch (Exception e) {
    32. logger.error("beforeBodyWrite error:", e);
    33. return SecretResponseBasic.fail(SECRET_API_ERROR, "", "服务端处理结果数据异常");
    34. }
    35. }
    36. }
    37. return o;
    38. }
    39. }

    代码Demo好了,试运行一波:
    请求方法:

    1. localhost:8080/secret/user/list
    2. Content-Type:application/json
    3. signature:55efb04a83ca083dd1e6003cde127c45
    4. timestamp:1648308048
    5. salt:123456
    6. clientType:ANDORID
    7. // 原始请求体
    8. {
    9. "page": 1,
    10. "size": 10
    11. }
    12. // 加密后的请求体
    13. {
    14. "data": "1ZBecdnDuMocxAiW9UtBrJzlvVbueP9K0MsIxQccmU3OPG92oRinVm0GxBwdlXXJ"
    15. }
    16. // 加密响应体:
    17. {
    18. "data": "fxHYvnIE54eAXDbErdrDryEsIYNvsOOkyEKYB1iBcre/QU1wMowHE2BNX/je6OP3NlsCtAeDqcp7J1N332el8q2FokixLvdxAPyW5Un9JiT0LQ3MB8p+nN23pTSIvh9VS92lCA8KULWg2nViSFL5X1VwKrF0K/dcVVZnpw5h227UywP6ezSHjHdA+Q0eKZFGTEv3IzNXWqq/otx5fl1gKQ==",
    19. "code": 200,
    20. "signature": "aa61f19da0eb5d99f13c145a40a7746b",
    21. "msg": "",
    22. "timestamp": 1648480034,
    23. "salt": 632648
    24. }
    25. // 解密后的响应体:
    26. {
    27. "code": 200,
    28. "data": [{
    29. "id": 1,
    30. "name": "boyka",
    31. "registerTime": "2022-03-27T00:19:43.699",
    32. "userType": "COMMON"
    33. }],
    34. "msg": "用户列表查询成功",
    35. "salt": 0
    36. }

    客户端请求加密-》发起请求-》服务端解密-》业务处理-》服务端响应加密-》客户端解密展示,看起来没啥问题,次日,安卓端反馈,这个加解密有问题,解密后的数据格式和之前不一样,仔细一看,擦,这个userType和registerTime是不对劲,开始思考:这个能是哪儿的问题呢?1s之后,初步定位,应该是响应体的JSON.toJSONString的问题:

    1. String data = EncryptUtils.aesEncrypt(JSON.toJSONString(o)),

    Debug断点调试,果然,是JSON.toJSONString(o)这一步骤转换出了问题,那JSON转换时是不是有高级属性可以配置生成想要的序列化格式呢?FastJson在序列化时提供重载方法,找到其中一个”SerializerFeature”参数可以琢磨一下,这个参数是可以对序列化进行配置的,它提供了很多配置类型,其中感觉这几个比较沾边:

    1. WriteEnumUsingToString,
    2. WriteEnumUsingName,
    3. UseISO8601DateFormat

    对枚举类型来说,默认是使用的WriteEnumUsingName(枚举的Name), 另一种WriteEnumUsingToString是重新toString方法,理论上可以转换成想要的样子,即这个样子:

    1. @Getter
    2. @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    3. public enum UserType {
    4. VIP("VIP用户"),
    5. COMMON("普通用户");
    6. private String code;
    7. private String type;
    8. UserType(String type) {
    9. this.code = name();
    10. this.type = type;
    11. }
    12. @Override
    13. public String toString() {
    14. return "{" +
    15. "\"code\":\"" + name() + '\"' +
    16. ", \"type\":\"" + type + '\"' +
    17. '}';
    18. }
    19. }

    结果转换出来的数据是字符串类型"{"code":"COMMON", "type":"普通用户"}",这个方法好像行不通,还有什么好办法呢?思前想后,看文章开始定义的User和UserType类,标记数据序列化格式@JsonFormat,再突然想起之前看到过的一些文章,SpringMVC底层默认是使用Jackson进行序列化的,那好了,就用Jacksong实施呗,将SecretResponseAdvice中的序列化方法替换一下:

    1. String data = EncryptUtils.aesEncrypt(JSON.toJSONString(o), secretKey);
    2. // 换为:
    3. String data =EncryptUtils.aesEncrypt(new ObjectMapper().writeValueAsString(o), secretKey);

    重新运行一波,走起:

    1. {
    2. "code": 200,
    3. "data": [{
    4. "id": 1,
    5. "name": "boyka",
    6. "userType": {
    7. "code": "COMMON",
    8. "type": "普通用户"
    9. },
    10. "registerTime": {
    11. "month": "MARCH",
    12. "year": 2022,
    13. "dayOfMonth": 29,
    14. "dayOfWeek": "TUESDAY",
    15. "dayOfYear": 88,
    16. "monthValue": 3,
    17. "hour": 22,
    18. "minute": 30,
    19. "nano": 453000000,
    20. "second": 36,
    21. "chronology": {
    22. "id": "ISO",
    23. "calendarType": "iso8601"
    24. }
    25. }
    26. }],
    27. "msg": "用户列表查询成功"
    28. }

    解密后的userType枚举类型和非加密版本一样了,舒服了,== 好像还不对,registerTime怎么变成这个样子了?原本是”2022-03-24 23:58:39”这种格式的,网上有很多解决方案,不过用在目前这个需求里面,就是有损改装了,不太可取,遂去Jackson官网上查找一下相关文档,当然Jackson也提供了ObjectMapper的序列化配置,重新再初始化配置ObjectMpper对象:

    1. String DATE_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    2. ObjectMapper objectMapper = new Jackson2ObjectMapperBuilder()
    3. .findModulesViaServiceLoader(true)
    4. .serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(
    5. DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER)))
    6. .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(
    7. DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER)))
    8. .build();

    转换结果:

    1. {
    2. "code": 200,
    3. "data": [{
    4. "id": 1,
    5. "name": "boyka",
    6. "userType": {
    7. "code": "COMMON",
    8. "type": "普通用户"
    9. },
    10. "registerTime": "2022-03-29 22:57:33"
    11. }],
    12. "msg": "用户列表查询成功"
    13. }

    和非加密版的终于一致了,完了吗?感觉还是可能存在些什么问题,首先业务代码的时间序列化需求不一样,有”yyyy-MM-dd hh:mm:ss”的,也有”yyyy-MM-dd”的,还可能其他配置思考不到位的,导致和之前非加密版返回数据不一致的问题,到时候联调测出来了也麻烦,有没有一劳永逸的办法呢?哎,这个时候如果看过 Spring 源码的话,就应该知道spring框架自身是怎么序列化的,照着配置应该就行嘛,好像有点道理,这里不从0开始分析源码了。
    跟着执行链路,找到具体的响应序列化,重点就是RequestResponseBodyMethodProcessor

    1. protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType, ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
    2. // 获取响应的拦截器链并执行beforeBodyWrite方法,也就是执行了我们自定义的SecretResponseAdvice中的beforeBodyWrite啦
    3. body = this.getAdvice().beforeBodyWrite(body, returnType, selectedMediaType, converter.getClass(), inputMessage, outputMessage);
    4. if (body != null) {
    5. // 执行响应体序列化工作
    6. if (genericConverter != null) {
    7. genericConverter.write(body, (Type)targetType, selectedMediaType, outputMessage);
    8. } else {
    9. converter.write(body, selectedMediaType, outputMessage);
    10. }
    11. }
    12. }

    进而通过实例化的AbstractJackson2HttpMessageConverter对象找到执行序列化的核心方法
    -> AbstractGenericHttpMessageConverter

    1. public final void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    2. ...
    3. this.writeInternal(t, type, outputMessage);
    4. outputMessage.getBody().flush();
    5. }

    -> 找到Jackson序列化 AbstractJackson2HttpMessageConverter

    1. // 从spring容器中获取并设置的ObjectMapper实例
    2. protected ObjectMapper objectMapper;
    3. protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    4. MediaType contentType = outputMessage.getHeaders().getContentType();
    5. JsonEncoding encoding = this.getJsonEncoding(contentType);
    6. JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    7. this.writePrefix(generator, object);
    8. Object value = object;
    9. Class<?> serializationView = null;
    10. FilterProvider filters = null;
    11. JavaType javaType = null;
    12. if (object instanceof MappingJacksonValue) {
    13. MappingJacksonValue container = (MappingJacksonValue)object;
    14. value = container.getValue();
    15. serializationView = container.getSerializationView();
    16. filters = container.getFilters();
    17. }
    18. if (type != null && TypeUtils.isAssignable(type, value.getClass())) {
    19. javaType = this.getJavaType(type, (Class)null);
    20. }
    21. ObjectWriter objectWriter = serializationView != null ? this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer();
    22. if (filters != null) {
    23. objectWriter = objectWriter.with(filters);
    24. }
    25. if (javaType != null && javaType.isContainerType()) {
    26. objectWriter = objectWriter.forType(javaType);
    27. }
    28. SerializationConfig config = objectWriter.getConfig();
    29. if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
    30. objectWriter = objectWriter.with(this.ssePrettyPrinter);
    31. }
    32. // 重点进行序列化
    33. objectWriter.writeValue(generator, value);
    34. this.writeSuffix(generator, object);
    35. generator.flush();
    36. }

    那么,可以看出SpringMVC在进行响应序列化的时候是从容器中获取的ObjectMapper实例对象,并会根据不同的默认配置条件进行序列化,那处理方法就简单了,也可以从Spring容器拿数据进行序列化。SecretResponseAdvice进行如下进一步改造:

    1. @ControllerAdvice
    2. public class SecretResponseAdvice implements ResponseBodyAdvice {
    3. @Autowired
    4. private ObjectMapper objectMapper;
    5. @Override
    6. public Object beforeBodyWrite(....) {
    7. .....
    8. String dataStr =objectMapper.writeValueAsString(o);
    9. String data = EncryptUtils.aesEncrypt(dataStr, secretKey);
    10. .....
    11. }
    12. }

    经测试,响应数据和非加密版万全一致了。