From 0a28faf7808b7995c13fff64e718eb61a960f20a Mon Sep 17 00:00:00 2001 From: baishou <2710684311@qq.com> Date: Thu, 16 May 2019 14:57:25 +0800 Subject: [PATCH] 获取Token代码整合,加入刷新Token的方法 --- springboot/src/main/java/com/sincre/springboot/common/Encript.java | 1 - springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------- springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 53 deletions(-) create mode 100644 springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java diff --git a/springboot/src/main/java/com/sincre/springboot/common/Encript.java b/springboot/src/main/java/com/sincre/springboot/common/Encript.java index 480f84e..6b9e42a 100644 --- a/springboot/src/main/java/com/sincre/springboot/common/Encript.java +++ b/springboot/src/main/java/com/sincre/springboot/common/Encript.java @@ -37,7 +37,6 @@ public class Encript { sha256_HMAC.init(secret_key); byte[] bytes = sha256_HMAC.doFinal(message.getBytes()); hash = byteArrayToHexString(bytes); - System.out.println(hash); } catch (Exception e) { System.out.println("Error HmacSHA256 ===========" + e.getMessage()); } diff --git a/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java b/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java index a4b817f..45240f3 100644 --- a/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java +++ b/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java @@ -2,12 +2,14 @@ package com.sincre.springboot.controller; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sincre.springboot.ApiModel.*; +import com.sincre.springboot.ApiModel.DeviceToken; +import com.sincre.springboot.ApiModel.TuYaDevicesList; +import com.sincre.springboot.ApiModel.TuYaResResult; +import com.sincre.springboot.ApiModel.UserInfo; import com.sincre.springboot.ApiPlatform.TuYaCloudService; -import com.sincre.springboot.common.EhcacheUtil; import com.sincre.springboot.utils.ApiHelper; +import com.sincre.springboot.utils.CacheHelper; import io.swagger.annotations.Api; -import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.HashMap; @@ -18,42 +20,12 @@ import java.util.Map; @Api(value = "涂鸦云服务API调用") public class TuYaYunController { - @GetMapping("GetToken") - public TuYaResResult getTuYaToken(){ - - String apiUrl = "/v1.0/token?grant_type=1"; - Long t = System.currentTimeMillis(); - //Token获取前的签名 - String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+t,TuYaCloudService.Secret); - Map map = new HashMap(); - map.put("client_id", TuYaCloudService.ClientId); - map.put("sign",sign); - map.put("sign_method",TuYaCloudService.Sign_method); - map.put("t",t.toString()); - String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); - - TuYaResResult resResult = new TuYaResResult<>(); - try{ - ObjectMapper objectMapper = new ObjectMapper(); - resResult = objectMapper.readValue(result,new TypeReference>(){}); - - String access_Token = resResult.getResult().getAccess_token(); //存储第一次获取的Token - EhcacheUtil ehcacheUtil = new EhcacheUtil(); - ehcacheUtil.setex("TuYa_Token",access_Token,60); - } catch(Exception ex){ - System.out.println(ex.getMessage()); - } - - return resResult; - } - @GetMapping("GetDevicesInfo") public TuYaResResult getTuYaDevicesInfo(){ - EhcacheUtil ehcacheUtil = new EhcacheUtil(); String apiUrl = "/v1.0/devices?device_ids="; Long t = System.currentTimeMillis(); - String access_Token = ehcacheUtil.get("TuYa_Token"); + String access_Token = CacheHelper.getTuYaToken(); String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); Map map = new HashMap(); map.put("client_id", TuYaCloudService.ClientId); @@ -68,7 +40,6 @@ public class TuYaYunController { ObjectMapper objectMapper = new ObjectMapper(); TypeReference> reference = new TypeReference>(){}; resResult = objectMapper.readValue(result,reference); - System.out.println(resResult.getResult().total); } catch(Exception ex){ System.out.println(ex.getMessage()); } @@ -88,12 +59,9 @@ public class TuYaYunController { @GetMapping("{AppId}/GetTuYaAPPUser") public String getTuYaAPPUser(@PathVariable("AppId") String AppId, @RequestParam int pageIndex, @RequestParam int pageSize){ - EhcacheUtil ehcacheUtil = new EhcacheUtil(); - String apiUrl = String.format("/v1.0/apps/%s/users?page_no=%d&page_size=%d",AppId,pageIndex,pageSize);//iotshunzhi Long t = System.currentTimeMillis(); - String access_Token = ehcacheUtil.get("TuYa_Token"); - System.out.println(access_Token); + String access_Token = CacheHelper.getTuYaToken(); //Token获取后的签名 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); Map headMap = new HashMap(); @@ -112,11 +80,9 @@ public class TuYaYunController { @PostMapping("{AppId}/AddTuYaAPPUser") public String addTuYaAPPUser(@PathVariable("AppId") String AppId,@RequestBody UserInfo userInfo){ - EhcacheUtil ehcacheUtil = new EhcacheUtil(); - String apiUrl = String.format("/v1.0/apps/%s/user",AppId);//应用标识iotshunzhi Long t = System.currentTimeMillis(); - String access_Token = ehcacheUtil.get("TuYa_Token"); + String access_Token = CacheHelper.getTuYaToken(); //Token获取后的签名 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); Map headMap = new HashMap(); @@ -131,7 +97,6 @@ public class TuYaYunController { String jsonResult = ""; try { jsonResult = objectMapper.writeValueAsString(userInfo); - System.out.println(jsonResult); }catch (com.fasterxml.jackson.core.JsonProcessingException ex){ ex.printStackTrace(); } @@ -144,20 +109,41 @@ public class TuYaYunController { } /** + * 根据用户ID来获取用户账号下的所有设备 + * @param uid + * @return + */ + @GetMapping("{uid}/GetDevicesByUid") + public String getDevicesByUid(@PathVariable("uid") String uid){ + + String apiUrl = String.format("/v1.0/users/%s/devices",uid); + Long t = System.currentTimeMillis(); + String access_Token = CacheHelper.getTuYaToken(); + //Token获取后的签名 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); + Map map = new HashMap(); + map.put("client_id", TuYaCloudService.ClientId); + map.put("access_token",access_Token); + map.put("sign",sign); + map.put("sign_method",TuYaCloudService.Sign_method); + map.put("t",t.toString()); + + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + + return result; + } + /** * 生成配网的Token * @return */ @GetMapping("AddDevicesToken") public String addDevicesToken(@RequestParam String UId){ - EhcacheUtil ehcacheUtil = new EhcacheUtil(); - String apiUrl = String.format("/v1.0/devices/token"); Long t = System.currentTimeMillis(); - String access_Token = ehcacheUtil.get("TuYa_Token"); + String access_Token = CacheHelper.getTuYaToken(); //Token获取后的签名 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); - System.out.println("签名"+sign); Map map = new HashMap(); map.put("client_id", TuYaCloudService.ClientId); map.put("access_token",access_Token); @@ -165,10 +151,6 @@ public class TuYaYunController { map.put("sign_method",TuYaCloudService.Sign_method); map.put("t",t.toString()); - if(StringUtils.isNotBlank(UId)){ - UId = "ay1557735327941UvZAG"; - } - ObjectMapper objectMapper = new ObjectMapper(); DeviceToken deviceToken = new DeviceToken(); deviceToken.setUid(UId); @@ -177,7 +159,6 @@ public class TuYaYunController { String jsonResult = ""; try { jsonResult = objectMapper.writeValueAsString(deviceToken); - System.out.println(jsonResult); }catch (com.fasterxml.jackson.core.JsonProcessingException ex){ ex.printStackTrace(); } @@ -185,4 +166,24 @@ public class TuYaYunController { return result; } + + @GetMapping("{token}/GetDevicesByToken") + public String getDevicesByToken(@PathVariable("token") String token){ + + String apiUrl = String.format("/v1.0/devices/tokens/%s",token); + Long t = System.currentTimeMillis(); + String access_Token = CacheHelper.getTuYaToken(); + //Token获取后的签名 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); + Map map = new HashMap(); + map.put("client_id", TuYaCloudService.ClientId); + map.put("access_token",access_Token); + map.put("sign",sign); + map.put("sign_method",TuYaCloudService.Sign_method); + map.put("t",t.toString()); + + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + + return result; + } } diff --git a/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java b/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java new file mode 100644 index 0000000..41d9c80 --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java @@ -0,0 +1,94 @@ +package com.sincre.springboot.utils; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sincre.springboot.ApiModel.TuYaResResult; +import com.sincre.springboot.ApiModel.TuYaResTokenObj; +import com.sincre.springboot.ApiPlatform.TuYaCloudService; +import com.sincre.springboot.common.EhcacheUtil; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashMap; +import java.util.Map; + +public class CacheHelper { + + static EhcacheUtil ehcacheUtil = new EhcacheUtil(); + static String TuYaTokenKey = "TuYa_Token"; + static String TuYaRefreshTokenKey = "TuYa_Re_Token"; + private static void setTuYaToken(){ + + String apiUrl = "/v1.0/token?grant_type=1"; + Long t = System.currentTimeMillis(); + //Token获取前的签名 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+t,TuYaCloudService.Secret); + Map map = new HashMap(); + map.put("client_id", TuYaCloudService.ClientId); + map.put("sign",sign); + map.put("sign_method",TuYaCloudService.Sign_method); + map.put("t",t.toString()); + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + + TuYaResResult resResult ; + try{ + ObjectMapper objectMapper = new ObjectMapper(); + resResult = objectMapper.readValue(result,new TypeReference>(){}); + + String access_Token = resResult.getResult().getAccess_token(); //存储第一次获取的Token + String refresh_Token = resResult.getResult().getRefresh_token(); + // 第一次缓存数据的时候,不刷新,后期刷新 + if(StringUtils.isNotBlank(ehcacheUtil.get(TuYaTokenKey))){ + TuYaRefresh(resResult.getResult()); + }else { + ehcacheUtil.setex(TuYaTokenKey, access_Token, resResult.getResult().getExpire_time()); // 储存二个小时 + ehcacheUtil.setex(TuYaRefreshTokenKey,refresh_Token,60 * 60); // 储存一个小时 + System.out.println("获取Token:"+ access_Token); + System.out.println("刷新Token:"+ refresh_Token); + + } + } catch(Exception ex){ + System.out.println(ex.getMessage()); + } + } + + /** + * 获取涂鸦Api 访问的Token + * @return + */ + public static String getTuYaToken(){ + + if(StringUtils.isBlank(ehcacheUtil.get(TuYaRefreshTokenKey))){ + setTuYaToken(); + } + + return ehcacheUtil.get(TuYaTokenKey); + } + + private static void TuYaRefresh(TuYaResTokenObj tuYaResTokenObj){ + + String apiUrl = "/v1.0/token/"+tuYaResTokenObj.getRefresh_token(); + Long t = System.currentTimeMillis(); + + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+t,TuYaCloudService.Secret); + Map map = new HashMap(); + map.put("client_id", TuYaCloudService.ClientId); + map.put("sign",sign); + map.put("sign_method",TuYaCloudService.Sign_method); + map.put("t",t.toString()); + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + + TuYaResResult resResult; + try{ + ObjectMapper objectMapper = new ObjectMapper(); + TypeReference> reference = new TypeReference>(){}; + resResult = objectMapper.readValue(result,reference); + String re_access_Token = resResult.getResult().getAccess_token(); //重新刷新后获取的Token + String refresh_Token = resResult.getResult().getRefresh_token(); + System.out.println("刷新后的Token:"+re_access_Token); + ehcacheUtil.setex(TuYaTokenKey,re_access_Token,resResult.getResult().getExpire_time()); // 储存两个小时 + ehcacheUtil.setex(TuYaRefreshTokenKey,refresh_Token,60 * 60); //储存一个小时 + } catch(Exception ex){ + System.out.println(ex.getMessage()); + } + } +} -- libgit2 0.21.0