parent
3a0549b384
commit
b875362e79
9 changed files with 398 additions and 212 deletions
@ -0,0 +1,122 @@ |
||||
package org.springblade.hospital.controller; |
||||
|
||||
import com.google.common.collect.Lists; |
||||
import org.apache.commons.codec.binary.Base64; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.crypto.Mac; |
||||
import javax.crypto.SecretKey; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.security.InvalidKeyException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.security.Security; |
||||
import java.util.*; |
||||
|
||||
public class AIoTRequestDemo { |
||||
private static final Logger log = LoggerFactory.getLogger(AIoTRequestDemo.class); |
||||
private static final String CHARSET = "UTF-8"; |
||||
private static final String SIGNATURE_ALGORITHM = "HmacSHA256"; |
||||
|
||||
static { |
||||
Security.addProvider(new BouncyCastleProvider()); |
||||
} |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
LinkedHashMap<String, List<String>> parameters = new LinkedHashMap<>(); |
||||
parameters.put("lang", Lists.newArrayList("zh_CN")); |
||||
String appKey = "9aON8USr"; |
||||
String appSecret = "VHc1CBWM0YS204rydm+wWxkQrqJSVyXAAgFRktVpOF4="; |
||||
String nonce = "123456"; |
||||
String timestampStr = "123456789"; |
||||
String correctSignature = "RdYq6cvBpeF84HO8iQErRF6Aq9XdE2fDx5w4qjDWfrg="; |
||||
String signature = renderSignature( |
||||
"/report/data/device", |
||||
parameters, |
||||
nonce, |
||||
timestampStr, |
||||
"", |
||||
appKey, |
||||
appSecret |
||||
); |
||||
System.out.println(signature.equals(correctSignature)); |
||||
} |
||||
|
||||
/* ———————————————————————————————————————— 封装逻辑 |
||||
———————————————————————————————————————— */ |
||||
public static String renderSignature( |
||||
String uri, |
||||
LinkedHashMap<String, List<String>> parameters, |
||||
String nonce, |
||||
String timestampStr, |
||||
String requestBody, |
||||
String appKey, |
||||
String appSecret |
||||
) throws Exception { |
||||
long timestamp; |
||||
if (StringUtils.isBlank(nonce)) { |
||||
throw new Exception("请求nonce未定义"); |
||||
} |
||||
if (StringUtils.isBlank(timestampStr)) { |
||||
throw new Exception("请求timestamp未定义"); |
||||
} |
||||
try { |
||||
timestamp = Long.parseLong(timestampStr); |
||||
} catch (NumberFormatException exception) { |
||||
throw new Exception("请求timestamp不合法"); |
||||
} |
||||
if (StringUtils.isBlank(appKey)) { |
||||
throw new Exception("请求appKey未定义"); |
||||
} |
||||
return signature(uri, parameters, nonce, timestamp, requestBody, appKey, appSecret); |
||||
} |
||||
|
||||
private static String signature(String requestURI, LinkedHashMap<String, List<String>> parameters, String |
||||
nonce, long timestamp, String requestBody, String appKey, String appSecret) throws NoSuchAlgorithmException, |
||||
InvalidKeyException, UnsupportedEncodingException { |
||||
parameters = parametersRank(parameters); |
||||
StringBuilder builder = new StringBuilder(requestURI); |
||||
for (Map.Entry<String, List<String>> paramEntry : parameters.entrySet()) { |
||||
for (String value : paramEntry.getValue()) { |
||||
builder |
||||
.append(builder.indexOf("?") == -1 ? "?" : "&") |
||||
.append(paramEntry.getKey()) |
||||
.append("=") |
||||
.append(value); |
||||
} |
||||
} |
||||
String url = builder.toString(); |
||||
String signInfo = String.format( |
||||
"%sappKey=%s&nonce=%s×tamp=%s%s", |
||||
String.format("%s%s", url, url.contains("?") ? "&" : "?"), |
||||
appKey, |
||||
nonce, |
||||
timestamp, |
||||
requestBody |
||||
); |
||||
SecretKey secretKey = new SecretKeySpec(appSecret.getBytes(CHARSET), SIGNATURE_ALGORITHM); |
||||
Mac mac = Mac.getInstance(SIGNATURE_ALGORITHM); |
||||
mac.init(secretKey); |
||||
String sign = Base64.encodeBase64String(mac.doFinal(signInfo.getBytes(CHARSET))); |
||||
log.info("signature completed ...... url [{}] payload [{}] sign [{}]", url, signInfo, sign); |
||||
return sign; |
||||
} |
||||
|
||||
private static LinkedHashMap<String, List<String>> parametersRank(LinkedHashMap<String, List<String>> |
||||
parameters) { |
||||
LinkedHashMap<String, List<String>> rankedParameters = new LinkedHashMap<>(parameters.size()); |
||||
if (parameters.size() != 0) { |
||||
List<String> keys = new ArrayList<>(parameters.keySet()); |
||||
Collections.sort(keys); |
||||
for (String key : keys) { |
||||
List<String> values = parameters.get(key); |
||||
values.sort(Comparator.naturalOrder()); |
||||
rankedParameters.put(key, values); |
||||
} |
||||
} |
||||
return rankedParameters; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue