|  |  |  | 
|---|
|  |  |  | import javax.annotation.PostConstruct; | 
|---|
|  |  |  | import javax.crypto.Cipher; | 
|---|
|  |  |  | import javax.crypto.spec.SecretKeySpec; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.lang.reflect.InvocationTargetException; | 
|---|
|  |  |  | import java.lang.reflect.Method; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class UavService implements IUavService { | 
|---|
|  |  |  | public class UavService { | 
|---|
|  |  |  | public static final String USERNAME = "ardbailu";//用户名 | 
|---|
|  |  |  | public static final String PASSWORD = "ardkj12345";//密码 | 
|---|
|  |  |  | public static final String SALT = "0123456789012345";//盐 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //token 登陆后每次请求,在header中携带 | 
|---|
|  |  |  | private String token;//登录成功返回的token | 
|---|
|  |  |  | private Map uavUser;//登录的用户信息 | 
|---|
|  |  |  | private ObjectMapper om = new ObjectMapper(); | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private UavClient uavClient; | 
|---|
|  |  |  | 
|---|
|  |  |  | this.login(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String doUavRequest(HttpMethod method, String url, String data) { | 
|---|
|  |  |  | String res = null; | 
|---|
|  |  |  | Method requestMethod = null; | 
|---|
|  |  |  | 
|---|
|  |  |  | try {//再次调用接口 | 
|---|
|  |  |  | res = (String) requestMethod.invoke(this.uavClient, url, this.token, data); | 
|---|
|  |  |  | } catch (IllegalAccessException ex) { | 
|---|
|  |  |  | throw new RuntimeException(ex); | 
|---|
|  |  |  | ex.printStackTrace(); | 
|---|
|  |  |  | } catch (InvocationTargetException ex) { | 
|---|
|  |  |  | throw new RuntimeException(ex); | 
|---|
|  |  |  | ex.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (IllegalAccessException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map getUavUser() { | 
|---|
|  |  |  | if (this.uavUser==null){ | 
|---|
|  |  |  | this.login(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return this.uavUser; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void login() { | 
|---|
|  |  |  | System.out.println("登录无人机外部接口"); | 
|---|
|  |  |  | 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"); | 
|---|
|  |  |  | if (data != null) { | 
|---|
|  |  |  | this.token = (String) data.get("access_token"); | 
|---|
|  |  |  | this.uavUser = (Map) resMap.get("data"); | 
|---|
|  |  |  | if (this.uavUser != null) { | 
|---|
|  |  |  | this.token = (String) this.uavUser.get("access_token"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (ForestNetworkException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } catch (JsonMappingException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } catch (JsonProcessingException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。 | 
|---|
|  |  |  | } | 
|---|