|
@@ -10,9 +10,12 @@ import io.vertx.ext.web.RoutingContext;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
/**
|
|
|
* IoT Emqx 连接认证的 Vert.x Handler
|
|
|
- * <a href="https://docs.emqx.com/zh/emqx/latest/access-control/authn/http.html">...</a>
|
|
|
+ * <a href=
|
|
|
+ * "https://docs.emqx.com/zh/emqx/latest/access-control/authn/http.html">...</a>
|
|
|
*
|
|
|
* @author haohao
|
|
|
*/
|
|
@@ -27,30 +30,29 @@ public class IotDeviceAuthVertxHandler implements Handler<RoutingContext> {
|
|
|
@Override
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public void handle(RoutingContext routingContext) {
|
|
|
- // TODO @haohao:try catch 兜底异常
|
|
|
- JsonObject json = routingContext.body().asJsonObject();
|
|
|
- String clientId = json.getString("clientid");
|
|
|
- String username = json.getString("username");
|
|
|
- String password = json.getString("password");
|
|
|
-
|
|
|
- IotDeviceEmqxAuthReqDTO authReqDTO = buildDeviceEmqxAuthReqDTO(clientId, username, password);
|
|
|
-
|
|
|
- CommonResult<Boolean> authResult = deviceUpstreamApi.authenticateEmqxConnection(authReqDTO);
|
|
|
- if (authResult.getCode() != 0 || !authResult.getData()) {
|
|
|
- denyAccess(routingContext);
|
|
|
- return;
|
|
|
+ try {
|
|
|
+ JsonObject json = routingContext.body().asJsonObject();
|
|
|
+ String clientId = json.getString("clientid");
|
|
|
+ String username = json.getString("username");
|
|
|
+ String password = json.getString("password");
|
|
|
+
|
|
|
+ // 构建认证请求DTO
|
|
|
+ IotDeviceEmqxAuthReqDTO authReqDTO = new IotDeviceEmqxAuthReqDTO()
|
|
|
+ .setClientId(clientId)
|
|
|
+ .setUsername(username)
|
|
|
+ .setPassword(password);
|
|
|
+
|
|
|
+ // 调用认证API
|
|
|
+ CommonResult<Boolean> authResult = deviceUpstreamApi.authenticateEmqxConnection(authReqDTO);
|
|
|
+ if (authResult.getCode() != 0 || !authResult.getData()) {
|
|
|
+ IotPluginCommonUtils.writeJson(routingContext, Collections.singletonMap("result", "deny"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ IotPluginCommonUtils.writeJson(routingContext, Collections.singletonMap("result", "allow"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[handle][EMQX认证异常]", e);
|
|
|
+ IotPluginCommonUtils.writeJson(routingContext, Collections.singletonMap("result", "deny"));
|
|
|
}
|
|
|
- // TODO @haohao:貌似可以考虑封装一个 writeJson ,里面有个参数是 data,然后里面去 JsonUtils.toJsonString(data)
|
|
|
- IotPluginCommonUtils.writeJson(routingContext, "{\"result\": \"allow\"}");
|
|
|
- }
|
|
|
-
|
|
|
- // TODO @haohao:下面两个简单方法,貌似可以考虑不抽小方法哈。
|
|
|
- private void denyAccess(RoutingContext routingContext) {
|
|
|
- IotPluginCommonUtils.writeJson(routingContext, "{\"result\": \"deny\"}");
|
|
|
}
|
|
|
-
|
|
|
- private IotDeviceEmqxAuthReqDTO buildDeviceEmqxAuthReqDTO(String clientId, String username, String password) {
|
|
|
- return new IotDeviceEmqxAuthReqDTO().setClientId(clientId).setUsername(username).setPassword(password);
|
|
|
- }
|
|
|
-
|
|
|
}
|