# 鉴权
String appkey = request.getHeader("appkey");
String signature = request.getHeader("signature");
String timestamp = request.getHeader("timestamp");
String appsecret = "appsecret";
String si1 = MD5Utils.md5(timestamp + appsecret).toLowerCase();
String signature1 = si1.substring(si1.length() - 16);
if(signature1.equals(signature)&&appkey.equals("appkey")) {
System.out.println("相等");
}else {
System.out.println("不相等");
throw new ApiRRException("请先登录", 401);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
MD5Utils.java
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;
public class MD5Utils {
/**
* 使用md5的算法进行加密
*/
public static String md5(String plainText, String charset) {
byte[] secretBytes = null;
String md5code = null ;
if(plainText != null){
try {
byte[] bs = null;
if (StringUtils.isBlank(charset)) {
bs = plainText.getBytes();
} else {
bs = plainText.getBytes(charset);
}
secretBytes = MessageDigest.getInstance("md5").digest(bs);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("转编码[" + charset + "]出错:" + e.getMessage(), e);
}
md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
// 如果生成数字未满32位,需要前面补0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
return null;
}
/**
* 使用md5的算法进行加密
*/
public static String md5(String plainText) {
return md5(plainText, null);
}
public static void main(String[] args) {
System.out.println(md5("123456"));
}
}
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
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