cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
68
69
70
71
72
73
74
75
76
package cn.ksource.web.facade.timetask;
 
import java.io.IOException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
 
 
public class LoginUtil {
    @SuppressWarnings("rawtypes")
    public static Map union400Login(String appver,String user,String pwd) throws ParseException, NoSuchAlgorithmException, IOException{
        
            //接收并处理传递参数
            Map<String, String> map = new HashMap<String, String>();
            map.put("appver", appver);
            map.put("user", user);
            //获取当前时间
            Timestamp endtime = new Timestamp(new Date().getTime());
            String time = String.valueOf(endtime);
            String time1 = time.replaceAll("-", "");
            String time2 = time1.replaceAll(":", "");
            String time3 = time2.replaceAll(" ", "");
            String time4 = time3.substring(0, time3.indexOf("."));//http请求时间为当前时间
            //计算Secrect
            map.put("timestamp", time4);
            String secret = signTopRequest(map, pwd);
            map.put("secret", secret);
            return map;
    }
    public static String signTopRequest(Map<String, String> params, String token) throws IOException, NoSuchAlgorithmException {
        //1 删除secret
        params.remove("secret");
        
        //2检查参数是否已经排序
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);
     
        //3把所有参数名和参数值串在一起
        StringBuilder query = new StringBuilder();
        for (String key : keys) {
            String value = params.get(key);
            if (!"".equals(key) && key!=null && !"".equals(value) && value!=null) {
                query.append(URLEncoder.encode(key,"utf8")).append(URLEncoder.encode(value,"utf8"));
            }
        }
     
        //4使用MD5加密
        query.append(token);
        byte[] bytes=encryptMD5(query.toString());
        
        //5转换为字符串
        return byte2hex(bytes);
    }
    public  static byte[] encryptMD5(String data) throws IOException, NoSuchAlgorithmException {
        MessageDigest md5=MessageDigest.getInstance("MD5");
        return md5.digest(data.getBytes("utf-8"));
    }
    public  static String byte2hex(byte[] bytes) {
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                sign.append("0");
            }
            sign.append(hex.toUpperCase());
        }
        return sign.toString();
    }
}