App key简称API接口验证序号,是用于验证API接入合法性的。接入哪个网站的API接口,就需要这个网站允许才能够接入,如果简单比喻的话:可以理解成是登陆网站的用户名。
App Secret简称API接口密钥,是跟App Key配套使用的,可以简单理解成是密码。
App Key 和 App Secret 配合在一起,通过其他网站的协议要求,就可以接入API接口调用或使用API提供的各种功能和数据。
比如淘宝联盟的API接口,就是淘宝客网站开发的必要接入,淘客程序通过API接口直接对淘宝联盟的数据库调用近亿商品实时数据。做到了轻松维护,自动更新。
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 |
原文:https://www.cnblogs.com/3xiaoleilei/p/13736452.html