ard-work/pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ard-work/src/main/java/com/ruoyi/utils/forest/UavAuth.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
ard-work/pom.xml
@@ -16,6 +16,17 @@ <dependencies> <!-- éç¨å·¥å ·--> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-common</artifactId> </dependency> <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-common</artifactId> </dependency> ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,132 @@ package com.ruoyi.device.uav.controller; import com.dtflys.forest.exceptions.ForestNetworkException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.util.StringUtil; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.utils.forest.UavClient; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.*; import org.apache.commons.codec.binary.Base64; /** * ç¸æºè®¾å¤Controller * * @author ruoyi * @date 2023-01-31 */ @Slf4j(topic = "uav") @RestController @RequestMapping("/device/uav") @Api(tags = "æ 人æºå¤é¨æ¥å£") public class ArdUavController extends BaseController { public static final String USERNAME = "ardbailu"; public static final String PASSWORD = "ardkj12345"; public static final String SALT = "0123456789012345"; //token ç»éåæ¯æ¬¡è¯·æ±,å¨header䏿ºå¸¦ private String token = null; private ObjectMapper om = new ObjectMapper(); @Autowired private UavClient uavClient; public void uavLogin() { String codedPassword = this.Encrypt(PASSWORD, SALT); String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}"; try { 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"); } catch (ForestNetworkException e) { e.printStackTrace(); } catch (JsonMappingException e) { throw new RuntimeException(e); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } public String getToken() { if (StringUtil.isEmpty(this.token)) { this.uavLogin(); } return this.token; } @GetMapping("/") @ApiOperation("æ 人æºgetæ¥å£") public Object get(@RequestParam String url, @RequestParam String data) { String res = null; String token = getToken(); try { res = uavClient.get(url, token, data); } catch (ForestNetworkException e) { if (e.getStatusCode() == 401) { this.uavLogin(); token = getToken(); res = uavClient.get(url, token, data); } } return res; } @PostMapping("/") @ApiOperation("æ 人æºpostæ¥å£") 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; } public String Encrypt(String sSrc, String sKey) { if (sKey == null) { System.out.print("Key为空null"); return null; } // 夿Keyæ¯å¦ä¸º16ä½ if (sKey.length() != 16) { System.out.print("Keyé¿åº¦ä¸æ¯16ä½"); return null; } byte[] encrypted = null; try { byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"ç®æ³/模å¼/è¡¥ç æ¹å¼" cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); } catch (Exception e) { e.printStackTrace(); ; } return new Base64().encodeToString(encrypted);//æ¤å¤ä½¿ç¨BASE64å转ç åè½ï¼åæ¶è½èµ·å°2次å å¯çä½ç¨ã } } ard-work/src/main/java/com/ruoyi/utils/forest/UavAuth.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,18 @@ package com.ruoyi.utils.forest; import com.dtflys.forest.annotation.MethodLifeCycle; import com.dtflys.forest.annotation.RequestAttributes; import java.lang.annotation.*; @MethodLifeCycle(UavAuthLifeCircle.class) @Documented /** éç¹ï¼ @MethodLifeCycle注解æå®è¯¥æ³¨è§£ççå½å¨æç±»*/ @RequestAttributes @Retention(RetentionPolicy.RUNTIME) /** æå®è¯¥æ³¨è§£å¯ç¨äºç±»ä¸ææ¹æ³ä¸ */ @Target({ElementType.TYPE, ElementType.METHOD}) public @interface UavAuth { String token(); } ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,40 @@ package com.ruoyi.utils.forest; import com.dtflys.forest.http.ForestRequest; import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle; import com.dtflys.forest.reflection.ForestMethod; public class UavAuthLifeCircle implements MethodAnnotationLifeCycle<UavAuth, Object> { /** * 彿¹æ³è°ç¨æ¶è°ç¨æ¤æ¹æ³ï¼æ¤æ¶è¿æ²¡ææ§è¡è¯·æ±åé * æ¬¡æ¹æ³å¯ä»¥è·å¾è¯·æ±å¯¹åºçæ¹æ³è°ç¨ä¿¡æ¯ï¼ä»¥åå¨æä¼ å ¥çæ¹æ³è°ç¨åæ°å表 */ @Override public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) { System.out.println("Invoke Method '" + method.getMethodName() + "' Arguments: " + args); } /** * åé请æ±åæ§è¡æ¤æ¹æ³ */ @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); } return true; } /** * æ¤æ¹æ³å¨è¯·æ±æ¹æ³åå§åçæ¶å被è°ç¨ */ @Override public void onMethodInitialized(ForestMethod forestMethod, UavAuth uavAuth) { System.out.println("Method '" + forestMethod.getMethodName()); } } ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,26 @@ package com.ruoyi.utils.forest; import com.dtflys.forest.annotation.DataVariable; import com.dtflys.forest.annotation.Get; import com.dtflys.forest.annotation.JSONBody; import com.dtflys.forest.annotation.Post; import org.springframework.stereotype.Component; @Component public interface UavClient { @Get("http://www.baidu.com") String test(); public static String IP = "http://112.98.126.2:6100/"; @UavAuth(token = "${token}") @Get(IP + "${url}") 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); }