OAuth 驗簽工具
JKOS Sign
請求參數:
Secret Key:
1. 排序業務參數以後,在最前端加上 client_id、(若存在 access_token),並在最後加上 timestamp 參數,組成 json body
2. 將 secret、json body 與計算過後的 timestamp 做組合
3. 簽名結果
在本範例中引入以下相關依賴,建議直接進行相依套件引入
使用方法:
1. alibaba fastjson 去進行 json object 與字串型別轉換
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
2. apache codec 協助做 hash
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
3. 程式碼 <Java>
package com.jko.sign;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.codec.digest.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class JkosSignUtil {
private static final String CLIENT_ID = "client_id";
private static final String METHOD = "method";
private static final String SIGN = "sign";
private static final String SIGNMETHOD = "sign_method";
private static final String TIMESTAMP = "timestamp";
private static final String ACCESS_TOKEN = "access_token";
private static final Map<String, Boolean> generalKey = new HashMap(){
{
put(CLIENT_ID, true);
put(METHOD, true);
put(SIGN, true);
put(SIGNMETHOD, true);
put(TIMESTAMP, true);
put(ACCESS_TOKEN, true);
}
};
public static String getJkosSign(Map<String, Object> params, String secret) {
String signBodyString = getSignBodyFromParams(params);
long currentMilliSeconds = Long.parseLong(params.get(TIMESTAMP).toString());
return sign(secret, signBodyString, currentMilliSeconds);
}
private static String getSignBodyFromParams(Map<String, Object> params) {
Map<String, Object> signBody = new LinkedHashMap<>();
signBody.put(CLIENT_ID, params.get(CLIENT_ID));
if (Objects.nonNull(params.get(ACCESS_TOKEN))) {
signBody.put(ACCESS_TOKEN, params.get(ACCESS_TOKEN));
}
List<String> signKeyList = new ArrayList();
params.forEach(
(k,v) -> {
if (generalKey.containsKey(k)) {
return ;
}
signKeyList.add(k);
});
Collections.sort(signKeyList);
signKeyList.forEach(key -> signBody.put(key, params.get(key)));
signBody.put(TIMESTAMP, params.get(TIMESTAMP));
return new JSONObject(signBody).toString(SerializerFeature.WriteMapNullValue);
}
private static String sign(String secret, String jsonBody, long timestamp) {
StringBuilder sb = new StringBuilder();
sb.append(secret).append(jsonBody).append(timestamp / 1000 / 86400);
String body = sb.toString();
return getSHA256(body.toLowerCase()).toUpperCase();
}
private static String getSHA256(String body) {
return DigestUtils.sha256Hex(body.getBytes(StandardCharsets.UTF_8));
}
}
/*
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
*/