import java.security.MessageDigest; /** * 网易云信 校验类 * User: NinetyOne * Date: 2019/3/15 * Time: 11:33 * To change this template use File | Setting | File Template. **/ public class CheckSumBuilder { // 计算并获取CheckSum public static String getCheckSum(String appSecret, String nonce, String curTime) { return encode("sha1", appSecret + nonce + curTime); } // 计算并获取md5值 public static String getMD5(String requestBody) { return encode("md5", requestBody); } private static String encode(String algorithm, String value) { if (value == null) { return null; } try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); messageDigest.update(value.getBytes()); return getFormattedText(messageDigest.digest()); } catch (Exception e) { throw new RuntimeException(e); } } private static String getFormattedText(byte[] bytes) { int len = bytes.length; StringBuilder buf = new StringBuilder(len * 2); for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); buf.append(HEX_DIGITS[bytes[j] & 0x0f]); } return buf.toString(); } private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; }
import com.alibaba.fastjson.JSONObject; import com.patient.core.config.NeteaseConfig; import com.patient.core.middleware.CheckSumBuilder; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; /** * 网易云信抄送 */ @RestController @RequestMapping(value = "/v1") public class RouteController { public static final Logger logger = LoggerFactory.getLogger(RouteController.class); @Autowired private NeteaseConfig neteaseConfig; @PostMapping("/route/mockClient") public JSONObject mockClient(HttpServletRequest request) throws Exception { JSONObject result = new JSONObject(); try { // 获取请求体 不可为空 byte[] body = readBody(request); if (body == null) { logger.warn("request wrong, empty body!"); result.put("code", 414); return result; } // 获取部分request header,并打印 String ContentType = request.getContentType(); String AppKey = request.getHeader("AppKey"); String CurTime = request.getHeader("CurTime"); String MD5 = request.getHeader("MD5"); String CheckSum = request.getHeader("CheckSum"); logger.info("request headers: ContentType = {}, AppKey = {}, CurTime = {}, " + "MD5 = {}, CheckSum = {}", ContentType, AppKey, CurTime, MD5, CheckSum); // 将请求体转成String格式,并打印 String requestBody = new String(body, "utf-8"); logger.info("request body = {}", requestBody); // 获取计算过的md5及checkSum String verifyMD5 = CheckSumBuilder.getMD5(requestBody); String verifyChecksum = CheckSumBuilder.getCheckSum(neteaseConfig.getAppSecret(), verifyMD5, CurTime); logger.debug("verifyMD5 = {}, verifyChecksum = {}", verifyMD5, verifyChecksum); // TODO: 比较md5、checkSum是否一致,以及后续业务处理 result.put("code", 200); return result; } catch (Exception ex) { logger.error(ex.getMessage(), ex); result.put("code", 414); return result; } }
// body如果为空 request.getContentLength() = 0 private byte[] readBody(HttpServletRequest request) throws IOException { if (request.getContentLength() > 0) { byte[] body = new byte[request.getContentLength()]; IOUtils.readFully(request.getInputStream(), body); return body; } else return null; } }