zoukankan      html  css  js  c++  java
  • 网易云信消息抄送

    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; } }

      

  • 相关阅读:
    查看电脑或者服务器sqlserver端口命令
    cvc-elt.1: Cannot find the declaration of element 'beans'Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-3.0.xsd'
    彻底卸载注册表、流氓软件的工具Uninstall Tool
    sqlserver安装报错:an error was encountered 数据无效
    导入虚拟机vmware,此主机支持Intel VT-x,但Intel VT-x处于禁用状态和黑屏
    设计模式----观察者模式通俗实例
    设计模式----策略模式通俗实例
    设计模式----代理模式通俗实例
    简单的纯js三级联动
    linux安装windows启动盘
  • 原文地址:https://www.cnblogs.com/EveningWind/p/10826511.html
Copyright © 2011-2022 走看看