MrShi
5 天以前 98a1749e3e614da9ce8afbf3af7c474cd0bf6702
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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();
    }
}