Step By Step
1、建立獨享實力,并在獨顯實力下面建立産品,
控制台位址
2、pom.xml
<dependencies>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
</dependencies>
3、Code Sample
import com.alibaba.fastjson.JSONObject;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* 裝置動态注冊。
*/
public class DynamicRegisterInstance {
// 定義加密方式。可選MAC算法:HmacMD5、HmacSHA1、HmacSHA256,需和signmethod取值一緻。
private static final String HMAC_ALGORITHM = "hmacsha1";
// 接收物聯網平台下發裝置證書的Topic。無需建立,無需訂閱,直接使用。
private static final String REGISTER_TOPIC = "/ext/regnwl";
/**
* 動态注冊。
*
* @param productKey 産品key
* @param productSecret 産品密鑰
* @param deviceName 裝置名稱
* @throws Exception
*/
public void register(String productKey, String productSecret, String deviceName, String instanceId) throws Exception {
// 接入域名,隻能使用TLS。
String broker = "ssl://"+instanceId+".mqtt.iothub.aliyuncs.com:1883";
// 表示用戶端ID,建議使用裝置的MAC位址或SN碼,64字元内。
String clientId = productKey + "@" + deviceName;
// 擷取随機值。
Random r = new Random();
int random = r.nextInt(1000000);
// securemode隻能為2表示隻能使用TLS;signmethod指定簽名算法。
String clientOpts = "|securemode=-2,authType=regnwl,signmethod=" + HMAC_ALGORITHM + ",random=" + random + ""+",instanceId="+instanceId+"|";
// MQTT接入用戶端ID。
String mqttClientId = clientId + clientOpts;
// MQTT接入使用者名。
String mqttUsername = deviceName + "&" + productKey;
// MQTT接入密碼,即簽名。
JSONObject params = new JSONObject();
params.put("productKey", productKey);
params.put("deviceName", deviceName);
params.put("random", random);
String mqttPassword = sign(params, productSecret);
// 通過MQTT connect封包進行動态注冊。
connect(broker, mqttClientId, mqttUsername, mqttPassword);
}
/**
* 通過MQTT connect封包發送動态注冊資訊。
*
* @param serverURL 動态注冊域名位址
* @param clientId 用戶端ID
* @param username MQTT使用者名
* @param password MQTT密碼
*/
@SuppressWarnings("resource")
private void connect(String serverURL, String clientId, String username, String password) {
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient sampleClient = new MqttClient(serverURL, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setMqttVersion(4);// MQTT 3.1.1
connOpts.setUserName(username);// 使用者名
connOpts.setPassword(password.toCharArray());// 密碼
connOpts.setAutomaticReconnect(false); // MQTT動态注冊協定規定必須關閉自動重連。
System.out.println("----- register params -----");
System.out.print("server=" + serverURL + ",clientId=" + clientId);
System.out.println(",username=" + username + ",password=" + password);
sampleClient.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// 僅處理動态注冊傳回消息。
if (REGISTER_TOPIC.equals(topic)) {
String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
System.out.println("----- register result -----");
System.out.println(payload);
sampleClient.disconnect();
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
@Override
public void connectionLost(Throwable cause) {
}
});
sampleClient.connect(connOpts);
} catch (MqttException e) {
System.out.print("register failed: clientId=" + clientId);
System.out.println(",username=" + username + ",password=" + password);
System.out.println("reason " + e.getReasonCode());
System.out.println("msg " + e.getMessage());
System.out.println("loc " + e.getLocalizedMessage());
System.out.println("cause " + e.getCause());
System.out.println("excep " + e);
e.printStackTrace();
}
}
/**
* 動态注冊簽名。
*
* @param params 簽名參數
* @param productSecret 産品密鑰
* @return 簽名十六進制字元串
*/
private String sign(JSONObject params, String productSecret) {
// 請求參數按字典順序排序。
Set<String> keys = getSortedKeys(params);
// sign、signMethod除外。
keys.remove("sign");
keys.remove("signMethod");
// 組裝簽名明文。
StringBuffer content = new StringBuffer();
for (String key : keys) {
content.append(key);
content.append(params.getString(key));
}
// 計算簽名。
String sign = encrypt(content.toString(), productSecret);
System.out.println("sign content=" + content);
System.out.println("sign result=" + sign);
return sign;
}
/**
* 擷取JSON對象排序後的key集合。
*
* @param json 需要排序的JSON對象
* @return 排序後的key集合
*/
private Set<String> getSortedKeys(JSONObject json) {
SortedMap<String, String> map = new TreeMap<String, String>();
for (String key : json.keySet()) {
String vlaue = json.getString(key);
map.put(key, vlaue);
}
return map.keySet();
}
/**
* 使用HMAC_ALGORITHM加密。
*
* @param content 明文
* @param secret 密鑰
* @return 密文
*/
private String encrypt(String content, String secret) {
try {
byte[] text = content.getBytes(StandardCharsets.UTF_8);
byte[] key = secret.getBytes(StandardCharsets.UTF_8);
SecretKeySpec secretKey = new SecretKeySpec(key, HMAC_ALGORITHM);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return byte2hex(mac.doFinal(text));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 二進制轉十六進制字元串。
*
* @param b 二進制數組
* @return 十六進制字元串
*/
private String byte2hex(byte[] b) {
StringBuffer sb = new StringBuffer();
for (int n = 0; b != null && n < b.length; n++) {
String stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
sb.append('0');
}
sb.append(stmp);
}
return sb.toString().toUpperCase();
}
public static void main(String[] args) throws Exception {
String productKey = "g7xec******";
String productSecret = "YMEVVG**********";
String deviceName = "device3";// 自定義的裝置名稱,運作後會在控制台自動建立裝置
String instanceId = "iot-06******"; // 企業版獨享實力id
// 進行動态注冊。
DynamicRegisterInstance client = new DynamicRegisterInstance();
client.register(productKey, productSecret, deviceName,instanceId);
}
}
4、測試結果