• 网易云信消息抄送


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

      

  • 相关阅读:
    min-width和width的区别
    组装电脑
    css背景透明,文字不透明
    三十九、前端基础之HTML
    三十八、事务、mysql索引,视图
    三十七、python操作mysql,和navicat
    三十六、单表与多表查询
    三十五、表与表之间的关系
    三十四、字段类型
    三十三、初识数据库及简单命令
  • 原文地址:https://www.cnblogs.com/EveningWind/p/10826511.html
Copyright © 2020-2023  润新知