/*** 敏感信息解密*/@PostMapping(value= "/decryptor", produces =MediaType.APPLICATION_JSON_UTF8_VALUE)publicRestResponse decryptor(@RequestBody String encryptContent) {
RestResponse restResponse= newRestResponse();try{
Map openapiResult =JSON.parseObject(encryptContent,new TypeReference>() {
},
Feature.OrderedField);
String signType= StringUtils.defaultIfBlank(openapiResult.get("sign_type"), "RSA2");
String charset= StringUtils.defaultIfBlank(openapiResult.get("charset"), "UTF-8");
String encryptType= StringUtils.defaultIfBlank(openapiResult.get("encrypt_type"), "AES");
String sign= openapiResult.get("sign");
String content= openapiResult.get("response");
LOGGER.info(String.format("准备验签和解密,sign=[%s], signType=[%s], encryptType=[%s], encryptContent=[%s]", sign, signType, encryptType, encryptContent));//如果密文的
boolean isDataEncrypted = !content.startsWith("{");booleansignCheckPass;//2. 验签
String signContent =content;//支付宝应用公钥
String signVeriKey =ALIPAY_APPLET_PUBLIC_SECRET;//如果是加密的报文则需要在密文的前后添加双引号
if(isDataEncrypted) {
signContent= "\"" + signContent + "\"";
}try{
signCheckPass=AlipaySignature.rsaCheck(signContent, sign, signVeriKey, charset, signType);
}catch(AlipayApiException e) {//验签异常, 日志
LOGGER.error("验签异常,encryptContent=" +encryptContent, e);
restResponse.setRestStatus(RestStatus.FAIL_100021);returnrestResponse;
}if (!signCheckPass) {//验签不通过(异常或者报文被篡改),终止流程(不需要做解密)
LOGGER.error("验签失败,encryptContent=" +encryptContent);
restResponse.setRestStatus(RestStatus.FAIL_100021);returnrestResponse;
}//支付宝小程序AES秘钥
String encryptKey =ALIPAY_APPLET_AES_SECRET;//3. 解密
String plainData;if(isDataEncrypted) {try{
plainData=AlipayEncrypt.decryptContent(content, encryptType, encryptKey, charset);
}catch(AlipayApiException e) {//解密异常, 日志
LOGGER.error("解密异常,encryptContent=" +encryptContent, e);
restResponse.setRestStatus(RestStatus.FAIL_100021);returnrestResponse;
}
}else{
plainData=content;
}
restResponse.setData(plainData);
restResponse.setRestStatus(RestStatus.SUCCESS);
}catch(Exception e) {
LOGGER.error("验签解密异常,encryptContent=" +encryptContent, e);
restResponse.setRestStatus(RestStatus.FAIL_100021);
}returnrestResponse;
}