From 4bcf7f6452d6ae04f210fc5c8408ad928d6602f4 Mon Sep 17 00:00:00 2001
From: zhangjian <zhangjianrock@163.com>
Date: 星期一, 10 七月 2023 15:20:46 +0800
Subject: [PATCH] 无人机转发接口反射调用
---
ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java | 7 ++-
ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java | 78 +++++++++++++++++++++++----------------
ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java | 22 ++++++++--
3 files changed, 67 insertions(+), 40 deletions(-)
diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
index a78bb9e..cf246b5 100644
--- a/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
+++ b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
@@ -11,14 +11,22 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;
+import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
+import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
+
+import static org.springframework.http.HttpMethod.GET;
+import static org.springframework.http.HttpMethod.POST;
/**
* 鐩告満璁惧Controller
@@ -36,20 +44,27 @@
public static final String SALT = "0123456789012345";
//token 鐧婚檰鍚庢瘡娆¤姹�,鍦╤eader涓惡甯�
- private String token = null;
+ private String token;//鐧诲綍鎴愬姛杩斿洖鐨則oken
private ObjectMapper om = new ObjectMapper();
@Autowired
private UavClient uavClient;
+ @PostConstruct
+ public void created() {
+ this.uavLogin();
+ }
+
public void uavLogin() {
String codedPassword = this.Encrypt(PASSWORD, SALT);
String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
try {
- String res = uavClient.post("login", null, body);
+ String res = uavClient.POST("login", null, body);
Map resMap = om.readValue(res, Map.class);
Map data = (Map) resMap.get("data");
- this.token = (String) data.get("access_token");
+ if (data != null) {
+ this.token = (String) data.get("access_token");
+ }
} catch (ForestNetworkException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
@@ -59,50 +74,49 @@
}
}
- public String getToken() {
- if (StringUtil.isEmpty(this.token)) {
- this.uavLogin();
- }
- return this.token;
- }
-
@GetMapping("/")
@ApiOperation("鏃犱汉鏈篻et鎺ュ彛")
public Object get(@RequestParam String url, @RequestParam String data) {
+ return doUavRequest(GET, url, data);
+
+ }
+
+ @Nullable
+ private String doUavRequest(HttpMethod method, String url, String data) {
String res = null;
- String token = getToken();
+ Method requestMethod = null;
+ //鑾峰彇method
try {
- res = uavClient.get(url, token, data);
- } catch (ForestNetworkException e) {
- if (e.getStatusCode() == 401) {
+ requestMethod = UavClient.class.getMethod(method.name(), String.class, String.class, String.class);
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ }
+ //鎵цmethod
+ try {
+ res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
+ } catch (InvocationTargetException e) {
+ ForestNetworkException fe = (ForestNetworkException) e.getCause();
+ if (fe.getStatusCode() == 401) {//token澶辨晥,閲嶆柊鐧诲綍
this.uavLogin();
- token = getToken();
- res = uavClient.get(url, token, data);
}
+ try {//鍐嶆璋冪敤鎺ュ彛
+ res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
+ } catch (IllegalAccessException ex) {
+ throw new RuntimeException(ex);
+ } catch (InvocationTargetException ex) {
+ throw new RuntimeException(ex);
+ }
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
}
return res;
-
}
@PostMapping("/")
@ApiOperation("鏃犱汉鏈簆ost鎺ュ彛")
public Object post(@RequestParam String url, @RequestParam String data) {
- String res = null;
- String token = getToken();
- try {
- res = uavClient.post(url, token, data);
- } catch (ForestNetworkException e) {
- if (e.getStatusCode() == 401) {
- this.uavLogin();
- token = getToken();
- res = uavClient.post(url, token, data);
- }
- }catch (Exception e){
- System.out.println(e.getMessage());
- }
-
- return res;
+ return doUavRequest(POST, url, data);
}
public String Encrypt(String sSrc, String sKey) {
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
index 3173958..83aea24 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
@@ -13,7 +13,18 @@
*/
@Override
public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
- System.out.println("Invoke Method '" + method.getMethodName() + "' Arguments: " + args);
+
+ System.out.println("!!!!!onInvokeMethod!!!!!! '" + method.getMethodName() + "' Arguments>>>>>>>>>: ");
+ /* for (Object arg :
+ args) {
+ System.out.println(arg);
+ }
+ String token = (String) args[1];
+ if (token != null) {
+ request.addHeader("x-auth-token", token);
+ }*/
+
+ System.out.println("=================");
}
/**
@@ -22,10 +33,11 @@
@Override
public boolean beforeExecute(ForestRequest request) {
String token = (String) getAttribute(request, "token");
- System.out.println(" UavAuthLifeCircle token:"+token);
- if(token!=null){
- request.addHeader("x-auth-token", token);
- }
+ System.out.println("!!!!beforeExecute!!!!");
+ System.out.println("request.getMethod().getMethodName():" + request.getMethod().getMethodName());
+ System.out.println(" token:" + token);
+ System.out.println("=================");
+ request.addHeader("x-auth-token", token);
return true;
}
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
index 4efe3bb..b7d86d4 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
@@ -5,8 +5,9 @@
import com.dtflys.forest.annotation.JSONBody;
import com.dtflys.forest.annotation.Post;
import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
-@Component
+@Service
public interface UavClient {
@Get("http://www.baidu.com")
String test();
@@ -16,11 +17,11 @@
@UavAuth(token = "${token}")
@Get(IP + "${url}")
- String get(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
+ String GET(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
@UavAuth(token = "${token}")
@Post(IP + "${url}")
- String post(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
+ String POST(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
}
--
Gitblit v1.9.3