网易云信消息抄送php,网易云信消息抄送

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值