package com.doumee.device; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; @RestController @RequestMapping("/electronic") public class ElectronicSubscribeController { @RequestMapping("/subscribe") public String subscribe(@RequestBody(required=false) String body, @RequestHeader(value="sign", required=false) String sign) { if(body==null) { body = ""; } if(!checkSign(body, sign)) { return "sign check failed"; } if("".equals(body)) { System.out.println("接收到服务器路径测试请求"); } else { System.out.println("接收到订阅消息"); System.out.println(body); //-----------加入业务逻辑----------- //-------------------------------- } return "SUCCESS"; } private boolean checkSign(String response_content, String sign) { // 随机字符串 后台获取 String token = "7O2wtcxNCvtSL7MtIOLs"; String buf = response_content + token; String encode = getMD5(buf); return encode.equalsIgnoreCase(sign); } // md5加密 private String getMD5(String password) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { throw new RuntimeException(e); } byte[] byteArray = password.getBytes(StandardCharsets.UTF_8); byte[] md5Bytes = md5.digest(byteArray); StringBuilder hexValue = new StringBuilder(); for (byte md5Byte : md5Bytes) { int val = ((int) md5Byte) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } }