public void publishBytes(String topic, byte[] message, boolean retained, int qos) { // Check if client is connected if (isMqttConnected()) { // Create a new MqttMessage from the message string MqttMessage mqttMsg = new MqttMessage(message); // Set retained flag mqttMsg.setRetained(retained); // Set quality of service mqttMsg.setQos(qos); try { client.publish(topic, mqttMsg); } catch (MqttPersistenceException e) { e.printStackTrace(); } catch (MqttException e) { e.printStackTrace(); } } else { connectionLost(null); } }
@Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
super.messageArrived(topic, mqttMessage);
System.out.println("topic " + topic);
Matcher matcher = pattern.matcher(topic); if (matcher.matches()) { String deviceid = matcher.group(1); byte[] rawPayload = mqttMessage.getPayload(); String payload = IOTSecurityUtil.decryptString( rawPayload, strKey, uniqueParam); // Further processing of decrypted message as per your need } }
数字签名提供了验证消息完整性最全面的方法,但它们会影响性能且需要额外的资源。尽管简单的校验和很容易实现及验证,但 MAC 或数字签名需要更多的计算,应在合适的场景中使用,比如在底层网络不可靠或传输的消息需要额外的安全措施时。
Watson IoT Platform REST API 中的 API 安全性
Watson IoT Platform REST API 提供了各种安全措施来访问这些 REST API,读取加密的数据,并验证消息身份。
访问这些 REST API
IBM Watson IoT Platform 提供了一个类似 REST 的 API 来支持某些功能,包括管理设备和访问来自设备的数据。结合使用基本身份验证和 HTTPS 来保护对 API 的访问:
使用 HTTPS(端口 443)而不是 HTTP(端口 80)
使用应用程序的 API 密钥作为用户名
使用相应的授权令牌作为密码
要验证 API 调用,必须在 Watson IoT Platform 中创建一个 API 密钥。按照 Watson IoT Platform 文档 中创建 API 密钥的详细指南进行操作。
然后,就可以使用 API 密钥调用 API 客户端 com.ibm.iotf.client.api.APIClient,后者可调用 IBM Watson IoT Platform API。 实例化 APIClient 展示了如何创建 APIClient 实例。它从一个属性文件读取实例化 API 客户端所需的属性。
public void doApp() { // Read properties from the conf file Properties props = MqttUtil.readProperties("MyData/application.prop");
try { //Instantiate the class by passing the properties file this.apiClient = new APIClient(props);
System.out.println("Adding a new device.."); addDevice(); System.out.println("Get all devices.."); getAllDevices(); System.out.println("Delete a device.."); deleteDevice(); System.out.println("Success..Exiting..");
/** * This sample showcases how to retrieve all the devices in an organization using the Java Client Library. * @throws IoTFCReSTException */
private void getAllDevices() throws IoTFCReSTException { // Get all the devices of type SampleDT try { /** * The Java ibmiotf client library provides an one argument constructor * which can be used to control the output, for example, lets try to retrieve * the devices in a sorted order based on device ID. */
ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("_sort","deviceId"));
// The response will contain more parameters that will be used to issue // the next request. The result element will contain the current list of devices JsonArray devices = response.get("results").getAsJsonArray(); for(Iterator<JsonElement> iterator = devices.iterator(); iterator.hasNext(); ) { JsonElement deviceElement = iterator.next(); JsonObject responseJson = deviceElement.getAsJsonObject(); System.out.println(responseJson); } } catch(IoTFCReSTException e) { System.out.println("HttpCode :" + e.getHttpCode() +" ErrorMessage :: "+ e.getMessage()); // Print if there is a partial response System.out.println(e.getResponse()); } }
Show moreShow more icon
使用用于 IBM Watson IoT Platform 的 REST API 读取加密的消息
还可以使用 IBM Watson IoT Platform API 从 Watson IoT Platform 消息存储中读取历史数据。设备可以加密有效负载及添加其他校验和字段(checksum field)来保护数据。 IBM Watson IoT Platform API 流程 描述了此场景的流程。
IBM Watson IoT Platform API 流程
如上图所示,设备加密有效负载的数据部分并将它作为一个 JSON 元素发送。它另外生成数据部分的校验和(在加密及编码之前),并将此作为另一个 JSON 元素发送。应用程序使用 IBM Watson IoT Platform API 从 Watson IoT Platform 拉取消息,解密及解码数据部分,然后通过比较所生成的校验和以及消息中所存储的校验和来最终执行校验和的验证。
/** * Method to get the latest historical event and process it */ private void getAllHistoricalEventsByDeviceID() { // Get the historical events try { //Get the list of historical events by device type and device id JsonElement response = this.apiClient.getHistoricalEvents( DEVICE_TYPE, DEVICE_ID_TEST); JsonObject events = response.getAsJsonObject(); JsonArray eventArray = events.getAsJsonArray("events"); //Get the latest event JsonElement currentEvent = eventArray.get(0); JsonObject responseJson = currentEvent.getAsJsonObject(); System.out.println("Most recent event - " + responseJson.toString());
JsonObject evtObject = responseJson.getAsJsonObject("evt"); System.out.println("Complete raw payload -" + evtObject.toString());
String dString = evtObject.get("d").getAsString(); System.out.println("Encrypted data part -" + dString);
String processedData = IOTSecurityUtil.decryptDecodeString(dString.getBytes(), strKey, uniqueParam); System.out.println("Data part after decryption and decoding - " + processedData);
Reprint policy:
All articles in this blog are used except for special statements
CC BY 4.0
reprint policy. If reproduced, please indicate source
John Doe
!