App key和App Secret
App key簡稱API接口驗證序號,是用於驗證API接入合法性的。接入哪個網站的API接口,就需要這個網站允許才能夠接入,如果簡單比喻的話:可以理解成是登陸網站的用戶名。
App Secret簡稱API接口密鑰,是跟App Key配套使用的,可以簡單理解成是密碼。
App Key 和 App Secret 配合在一起,通過其他網站的協議要求,就可以接入API接口調用或使用API提供的各種功能和數據。
比如淘寶聯盟的API接口,就是淘寶客網站開發的必要接入,淘客程序通過API接口直接對淘寶聯盟的數據庫調用近億商品實時數據。做到了輕松維護,自動更新。
UUID
UUID是指在一台機器在同一時間中生成的數字在所有機器中都是唯一的。按照開放軟件基金會(OSF)制定的標准計算,用到了以太網卡地址、納秒級時間、芯片ID碼和許多可能的數字
UUID由以下幾部分的組合:
(1)當前日期和時間。
(2)時鍾序列。
(3)全局唯一的IEEE機器識別號,如果有網卡,從網卡MAC地址獲得,沒有網卡以其他方式獲得。
標准的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),以連字號分為五段形式的36個字符,示例:550e8400-e29b-41d4-a716-446655440000
Java標准類庫中已經提供了UUID的API。
1
|
UUID.randomUUID()
|
我采用的是短8位UUID方式。
代碼實現
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
77
78
79
80
81
82
83
84
85
86
87
|
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
import
java.util.Arrays;
import
java.util.UUID;
/**
* @author mazhq
* @Title: AppUtils
* @Description: 隨機產生唯一的app_key和app_secret
* @date 2019/8/27 16:12
*/
public
class
AppUtils {
//生成 app_secret 密鑰
private
final
static
String SERVER_NAME =
"mazhq_abc123"
;
private
final
static
String[] chars =
new
String[]{
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
,
"g"
,
"h"
,
"i"
,
"j"
,
"k"
,
"l"
,
"m"
,
"n"
,
"o"
,
"p"
,
"q"
,
"r"
,
"s"
,
"t"
,
"u"
,
"v"
,
"w"
,
"x"
,
"y"
,
"z"
,
"0"
,
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"A"
,
"B"
,
"C"
,
"D"
,
"E"
,
"F"
,
"G"
,
"H"
,
"I"
,
"J"
,
"K"
,
"L"
,
"M"
,
"N"
,
"O"
,
"P"
,
"Q"
,
"R"
,
"S"
,
"T"
,
"U"
,
"V"
,
"W"
,
"X"
,
"Y"
,
"Z"
};
/**
* @Description: <p>
* 短8位UUID思想其實借鑒微博短域名的生成方式,但是其重復概率過高,而且每次生成4個,需要隨即選取一個。
* 本算法利用62個可打印字符,通過隨機生成32位UUID,由於UUID都為十六進制,所以將UUID分成8組,每4個為一組,然后通過模62操作,結果作為索引取出字符,
* 這樣重復率大大降低。
* 經測試,在生成一千萬個數據也沒有出現重復,完全滿足大部分需求。
* </p>
* @author mazhq
* @date 2019/8/27 16:16
*/
public
static
String getAppId() {
StringBuffer shortBuffer =
new
StringBuffer();
String uuid = UUID.randomUUID().toString().replace(
"-"
,
""
);
for
(
int
i =
0
; i <
8
; i++) {
String str = uuid.substring(i *
4
, i *
4
+
4
);
int
x = Integer.parseInt(str,
16
);
shortBuffer.append(chars[x %
0x3E
]);
}
return
shortBuffer.toString();
}
/**
* <p>
* 通過appId和內置關鍵詞生成APP Secret
* </P>
* @author mazhq
* @date 2019/8/27 16:32
*/
public
static
String getAppSecret(String appId) {
try
{
String[] array =
new
String[]{appId, SERVER_NAME};
StringBuffer sb =
new
StringBuffer();
// 字符串排序
Arrays.sort(array);
for
(
int
i =
0
; i < array.length; i++) {
sb.append(array[i]);
}
String str = sb.toString();
MessageDigest md = MessageDigest.getInstance(
"SHA-1"
);
md.update(str.getBytes());
byte
[] digest = md.digest();
StringBuffer hexstr =
new
StringBuffer();
String shaHex =
""
;
for
(
int
i =
0
; i < digest.length; i++) {
shaHex = Integer.toHexString(digest[i] &
0xFF
);
if
(shaHex.length() <
2
) {
hexstr.append(
0
);
}
hexstr.append(shaHex);
}
return
hexstr.toString();
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
throw
new
RuntimeException();
}
}
public
static
void
main(String[] args) {
String appId = getAppId();
String appSecret = getAppSecret(appId);
System.out.println(
"appId: "
+appId);
System.out.println(
"appSecret: "
+appSecret);
}
}
|
運行結果輸出:
1
2
|
appId: Jx3wQMD1
appSecret: d68397c4fb671bc024e24e1964b067cc35388818
|