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
   | 
 
 
 
  @ControllerAdvice @Slf4j public class RequestEncryptAdvice extends RequestBodyAdviceAdapter {
      @Resource     RSA rsa;
      @Resource     RedisTemplate redisTemplate;
      @Override     public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {         EncryptController methodAnnotation = methodParameter.getMethodAnnotation(EncryptController.class);         return methodAnnotation != null && methodAnnotation.requestEncrypt();     }
      @Override     public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage,                                            MethodParameter parameter,                                            Type targetType,                                            Class<? extends HttpMessageConverter<?>> converterType) throws IOException {         if (inputMessage.getHeaders().get(EncConstant.AES_HEADER).size() > 0){             return decryptBody(inputMessage);         }         return inputMessage;     }
 
      private HttpInputMessage decryptBody(HttpInputMessage inputMessage){                  String aesKey = Objects.requireNonNull(inputMessage.getHeaders().get(EncConstant.AES_HEADER)).get(0);                  if (Boolean.TRUE.equals(redisTemplate.hasKey(EncConstant.AES_MAP_PREFIX + aesKey))) {             aesKey = (String) redisTemplate.opsForValue().get(EncConstant.AES_MAP_PREFIX + aesKey);         } else {             aesKey = rsa.decryptStr(aesKey, KeyType.PrivateKey);             redisTemplate.opsForValue().set(EncConstant.AES_MAP_PREFIX + aesKey, aesKey);         }         try{             byte[] body = new byte[inputMessage.getBody().available()];             inputMessage.getBody().read(body);             String bodyStr = new String(body);             AES aes = new AES("CBC",                     "PKCS7Padding",                     aesKey.getBytes(StandardCharsets.UTF_8),                     "4382822409223508".getBytes(StandardCharsets.UTF_8));             log.debug("bodyStr:{}", bodyStr);             bodyStr = aes.decryptStr(bodyStr,CharsetUtil.CHARSET_UTF_8);             byte[] decrypt = bodyStr.getBytes(StandardCharsets.UTF_8);             final ByteArrayInputStream bais = new ByteArrayInputStream(decrypt);             return new HttpInputMessage() {                 @Override                 public InputStream getBody() {                     return bais;                 }                 @Override                 public HttpHeaders getHeaders() {                     return inputMessage.getHeaders();                 }             };         }catch (Exception e){             log.error("解密失败 {}",e);             throw new BusinessException(50000,"加密参数异常");         }     }
  }
 
 
  |