Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qmai-openapi-sign-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王敬磊
qmai-openapi-sign-util
Commits
a7c1571c
Commit
a7c1571c
authored
Nov 02, 2022
by
王敬磊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parents
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
274 additions
and
0 deletions
+274
-0
README.md
README.md
+37
-0
pom.xml
pom.xml
+29
-0
Example.java
src/main/java/com/qmai/openapi/sign/util/Example.java
+109
-0
QmaiTokenUtil.java
src/main/java/com/qmai/openapi/sign/util/QmaiTokenUtil.java
+99
-0
No files found.
README.md
0 → 100644
View file @
a7c1571c
## 开放平台验签SDK
### 数字签名sign
-
企迈云开放平台 API 会对每个访问的请求进行身份验证,即每个请求都需要在公共请求参数中包含签名信息以验证用户身份。签名信息由用户所执有的安全凭证生成,安全凭证包括 openId、grantCode 和
openKey,若用户还没有安全凭证,请联系企迈云开放平台客服,否则就无法调用API接口.
### 签名串有效时间
-
生成的签名串有效时间为300秒
### 签名串示例
```
cFw0t9IuvL9jVo9qAzk0qMcw5BM%3D
```
TOKEN生成工具类: com.qmai.openapi.sign.util.TokenUtil
开放平台接口请求示例: com.qmai.openapi.sign.util.Example
SDK MAVEN 依赖
```
xml
<dependencies>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.13
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
2.0.16.graal
</version>
</dependency>
</dependencies>
```
\ No newline at end of file
pom.xml
0 → 100644
View file @
a7c1571c
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.qmai
</groupId>
<artifactId>
qmai-openapi-sign-util
</artifactId>
<version>
1.0
</version>
<properties>
<maven.compiler.source>
8
</maven.compiler.source>
<maven.compiler.target>
8
</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.13
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
2.0.16.graal
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
src/main/java/com/qmai/openapi/sign/util/Example.java
0 → 100644
View file @
a7c1571c
package
com
.
qmai
.
openapi
.
sign
.
util
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.HttpStatus
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.apache.http.util.EntityUtils
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 类描述: token生成及接口请求示例
*
* @author Wang Jinglei
* @date 2022/11/1 4:54 下午
* @modifier Wang Jinglei
* @date 2022/11/1 4:54 下午
* @Version V1.0
*/
public
class
Example
{
private
static
String
grantCode
=
"f7a23ee212"
;
private
static
String
openId
=
"42206de20656a5d93a424e6279f636b6"
;
private
static
String
openKey
=
"ojTjKomMbA5KpSpaHaV96wYGqtLno2AWVxCxDuNQGw7sopnzqN"
;
// 随机正整数
private
static
Integer
nonce
=
166
;
// 当前时间戳
private
static
Long
timestamp
=
1667296049L
;
public
static
void
main
(
String
[]
args
)
{
String
url
=
"https://openapi.qmai.co/v3/baking/customer/userRecharge"
;
String
token
=
QmaiTokenUtil
.
getToken
(
grantCode
,
openId
,
openKey
,
nonce
,
timestamp
);
Map
<
String
,
Object
>
requestParam
=
new
HashMap
<>();
requestParam
.
put
(
"openId"
,
openId
);
requestParam
.
put
(
"grantCode"
,
grantCode
);
// 业务参数 content
HashMap
<
String
,
Object
>
bizParam
=
new
HashMap
<>();
bizParam
.
put
(
"shopCode"
,
18
);
bizParam
.
put
(
"activityId"
,
"785425500976746497"
);
bizParam
.
put
(
"amount"
,
300
);
bizParam
.
put
(
"giveAmount"
,
20
);
bizParam
.
put
(
"bizId"
,
"2102819060916404224"
);
bizParam
.
put
(
"source"
,
"8"
);
HashMap
<
String
,
Object
>
customerCodeType
=
new
HashMap
<>();
customerCodeType
.
put
(
"customerCode"
,
"778770795093545867"
);
customerCodeType
.
put
(
"type"
,
"9"
);
bizParam
.
put
(
"customerCodeType"
,
customerCodeType
);
bizParam
.
put
(
"remarks"
,
""
);
requestParam
.
put
(
"params"
,
bizParam
);
requestParam
.
put
(
"nonce"
,
nonce
);
requestParam
.
put
(
"timestamp"
,
timestamp
);
requestParam
.
put
(
"token"
,
token
);
String
response
=
doPost
(
url
,
requestParam
);
System
.
out
.
println
(
response
);
}
public
static
String
doPost
(
String
url
,
Map
<
String
,
Object
>
param
)
{
CloseableHttpClient
httpClient
=
HttpClientBuilder
.
create
().
build
();
try
{
if
(
httpClient
==
null
)
{
httpClient
=
HttpClientBuilder
.
create
().
build
();
}
HttpPost
post
=
new
HttpPost
(
url
);
StringEntity
stringEntity
=
new
StringEntity
(
JSONObject
.
toJSONString
(
param
),
StandardCharsets
.
UTF_8
);
//发送json数据需要设置contentType
stringEntity
.
setContentType
(
ContentType
.
APPLICATION_JSON
.
toString
());
//设置请求参数
post
.
setEntity
(
stringEntity
);
HttpResponse
response
=
httpClient
.
execute
(
post
);
if
(
response
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
//返回json格式
String
res
=
EntityUtils
.
toString
(
response
.
getEntity
());
return
res
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
httpClient
!=
null
)
{
try
{
httpClient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
return
null
;
}
}
src/main/java/com/qmai/openapi/sign/util/QmaiTokenUtil.java
0 → 100644
View file @
a7c1571c
package
com
.
qmai
.
openapi
.
sign
.
util
;
import
org.apache.commons.codec.binary.Base64
;
import
javax.crypto.Mac
;
import
javax.crypto.SecretKey
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLEncoder
;
import
java.security.GeneralSecurityException
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 类描述: 开放平台token生成工具类
*
* @author Wang Jinglei
* @date 2022/11/1 4:50 下午
* @modifier Wang Jinglei
* @date 2022/11/1 4:50 下午
* @Version V1.0
*/
public
class
QmaiTokenUtil
{
/**
* 生成开放平台Token
*
* @param grandCode 应用grandCode 由企迈侧提供
* @param openId 应用openId 由企迈侧提供
* @param openKey 应用openKey 由企迈侧提供
* @param nonce 随机正整数
* @param timestamp 当前时间戳
* @return 正常情况返回token字符串,token获取失败时返回null
*/
public
static
String
getToken
(
String
grandCode
,
String
openId
,
String
openKey
,
Integer
nonce
,
Long
timestamp
)
{
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>(
4
);
map
.
put
(
"openId"
,
openId
);
map
.
put
(
"grantCode"
,
grandCode
);
map
.
put
(
"timestamp"
,
timestamp
);
map
.
put
(
"nonce"
,
nonce
);
try
{
String
tokenNew
=
Sha1HexUtil
.
computeSignature
(
Sha1HexUtil
.
kSort
(
map
),
openKey
);
return
URLEncoder
.
encode
(
tokenNew
,
"utf-8"
);
}
catch
(
UnsupportedEncodingException
|
GeneralSecurityException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
private
static
class
Sha1HexUtil
{
/**
* 生成token字符串中间状态, 还需经过URL编码后获得最终的Token
* @param baseString
* @param keyString
* @return
* @throws GeneralSecurityException
*/
public
static
String
computeSignature
(
String
baseString
,
String
keyString
)
throws
GeneralSecurityException
{
SecretKey
secretKey
;
byte
[]
keyBytes
=
keyString
.
getBytes
();
secretKey
=
new
SecretKeySpec
(
keyBytes
,
"HmacSHA1"
);
Mac
mac
=
Mac
.
getInstance
(
"HmacSHA1"
);
mac
.
init
(
secretKey
);
byte
[]
text
=
baseString
.
getBytes
();
return
new
String
(
Base64
.
encodeBase64
(
mac
.
doFinal
(
text
))).
trim
();
}
/**
* 字段排序
* @param map
* @return
* @throws UnsupportedEncodingException
*/
public
static
String
kSort
(
Map
<
String
,
Object
>
map
)
throws
UnsupportedEncodingException
{
StringBuilder
sb
=
new
StringBuilder
();
String
[]
key
=
new
String
[
map
.
size
()];
int
index
=
0
;
for
(
String
k
:
map
.
keySet
())
{
key
[
index
]
=
k
;
index
++;
}
Arrays
.
sort
(
key
);
for
(
String
s
:
key
)
{
sb
.
append
(
s
).
append
(
"="
).
append
(
map
.
get
(
s
)).
append
(
"&"
);
}
sb
=
new
StringBuilder
(
sb
.
substring
(
0
,
sb
.
length
()
-
1
));
sb
=
new
StringBuilder
(
URLEncoder
.
encode
(
sb
.
toString
(),
"UTF-8"
));
sb
=
new
StringBuilder
(
sb
.
toString
().
replace
(
"%3D"
,
"="
).
replace
(
"%26"
,
"&"
));
return
sb
.
toString
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment