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、測試結果