| | |
| | | package com.fzzy.gateway.hx2023.service; |
| | | |
| | | import com.fzzy.api.data.AuthToken; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.fzzy.api.data.PushProtocol; |
| | | import com.fzzy.api.entity.ApiLog; |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.api.utils.MyMD5Util; |
| | | import com.fzzy.api.utils.RSAUtils; |
| | | import com.fzzy.api.utils.RedisConst; |
| | | import com.fzzy.api.utils.RedisUtil; |
| | | import com.fzzy.api.view.repository.ApiLogRep; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.fzzy.gateway.util.GatewayHttpUtil; |
| | | import com.fzzy.gateway.api.GatewayRemoteService; |
| | | import com.fzzy.gateway.data.GatewayResponse; |
| | | import com.fzzy.gateway.entity.GatewayConf; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.push.gb2022.HttpClientUtil; |
| | | import com.fzzy.gateway.hx2023.data.CloudResp; |
| | | import com.fzzy.gateway.util.GatewayRSAUtils; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | |
| | | |
| | | @Resource |
| | | private ApiLogRep apiLogRep; |
| | | |
| | | |
| | | @Resource |
| | | private RedisUtil redisUtil; |
| | | private GatewayConfService gatewayConfService; |
| | | |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public GatewayResponse authorize(GatewayConf conf) { |
| | | |
| | | //添加LOG |
| | | ApiLog apiLog = new ApiLog(); |
| | | apiLog.setData("鉴权接口"); |
| | | apiLog.setId(ContextUtil.getUUID()); |
| | | apiLog.setKqdm(conf.getKqdm()); |
| | | |
| | | public void init(GatewayConf gatewayConf) { |
| | | try { |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("username", conf.getUserName()); |
| | | map.put("username", conf.getPassword()); |
| | | if (StringUtils.isEmpty(gatewayConf.getApiUrl())) { |
| | | return; |
| | | } |
| | | |
| | | log.debug("-----------------数据报文----------------{}", map); |
| | | //获取公私钥接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("appId", gatewayConf.getGatewayId()); |
| | | |
| | | String url = gatewayConf.getApiUrl() + "reserver/api/key/apply"; |
| | | String jsonStr = GatewayHttpUtil.doGet(url, params); |
| | | |
| | | log.info("---获取公私钥接口-返回---{}", jsonStr); |
| | | |
| | | CloudResp respKey = JSONObject.parseObject(jsonStr, CloudResp.class); |
| | | |
| | | if (ScConstant.CODE_200 == respKey.getCode()) { |
| | | JSONObject object = respKey.getData(); |
| | | String pubKey = (String) object.get("pubKey"); |
| | | String priKey = (String) object.get("priKey"); |
| | | |
| | | gatewayConf.setPublicKey(pubKey); |
| | | gatewayConf.setPrivateKey(priKey); |
| | | } |
| | | |
| | | |
| | | String url = conf.getApiUrl() + ScConstant.API_URL_AUTH; |
| | | //获取 AccessToken 接口 |
| | | String sign = getSign(params, gatewayConf.getPrivateKey()); |
| | | params.put("sign", sign); |
| | | url = gatewayConf.getApiUrl() + "reserver/api/token/apply"; |
| | | jsonStr = GatewayHttpUtil.doGet(url, params); |
| | | log.info("---获取AccessToken接口-返回---{}", jsonStr); |
| | | CloudResp respToken = JSONObject.parseObject(jsonStr, CloudResp.class); |
| | | |
| | | if (ScConstant.CODE_200 == respToken.getCode()) { |
| | | JSONObject object = respKey.getData(); |
| | | if (null != object) { |
| | | String token = (String) object.get("token"); |
| | | gatewayConf.setAccessToken(token); |
| | | } |
| | | } |
| | | |
| | | GatewayResponse responseDto = HttpClientUtil.pushGateway(url, map); |
| | | apiLog.setStatus(responseDto.getStatus()); |
| | | apiLog.setResult(responseDto.getMessage()); |
| | | apiLogRep.save(apiLog); |
| | | //更新缓存 |
| | | updateAuthToken(gatewayConf); |
| | | |
| | | updateAuthToken(responseDto, conf); |
| | | |
| | | return responseDto; |
| | | } catch (Exception e) { |
| | | apiLog.setStatus(99); |
| | | apiLog.setResult("失败:" + e.getMessage()); |
| | | apiLogRep.save(apiLog); |
| | | log.error(e.getMessage(), e); |
| | | return new GatewayResponse(99, e.getMessage()); |
| | | log.error("------初始化失败-----{}", e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void heartbeat(GatewayConf gatewayConf) { |
| | | try { |
| | | |
| | | if (StringUtils.isEmpty(gatewayConf.getApiUrl())) { |
| | | return; |
| | | } |
| | | |
| | | gatewayConf = getCacheConf(gatewayConf.getKqdm()); |
| | | //网关心跳接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("token", gatewayConf.getAccessToken()); |
| | | params.put("gatewayId", gatewayConf.getGatewayId()); |
| | | params.put("gatewayIp", gatewayConf.getGatewayIp()); |
| | | params.put("gatewayMac", gatewayConf.getGatewayMac()); |
| | | params.put("heartbeat", "1"); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | |
| | | String sign = getSign(params, gatewayConf.getPrivateKey()); |
| | | params.put("sign", sign); |
| | | |
| | | String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat"; |
| | | |
| | | String jsonStr = GatewayHttpUtil.doGet(url, params); |
| | | log.info("---网关心跳接口-返回---{}", jsonStr); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("------网关心跳接口--执行失败-----{}", e); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void pushInfo(GatewayConf gatewayConf) { |
| | | try { |
| | | |
| | | if (StringUtils.isEmpty(gatewayConf.getApiUrl())) { |
| | | return; |
| | | } |
| | | |
| | | gatewayConf = getCacheConf(gatewayConf.getKqdm()); |
| | | //网关心跳接口 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("token", gatewayConf.getAccessToken()); |
| | | params.put("gatewayId", gatewayConf.getGatewayId()); |
| | | params.put("gatewayIp", gatewayConf.getGatewayIp()); |
| | | params.put("gatewayMac", gatewayConf.getGatewayMac()); |
| | | params.put("gatewayCPU", gatewayConf.getGatewayCPU()); |
| | | params.put("gatewayMem", gatewayConf.getGatewayMem()); |
| | | params.put("gatewayHardDisk", gatewayConf.getGatewayHardDisk()); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | |
| | | String sign = getSign(params, gatewayConf.getPrivateKey()); |
| | | params.put("sign", sign); |
| | | |
| | | String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat"; |
| | | |
| | | String jsonStr = GatewayHttpUtil.doGet(url, params); |
| | | log.info("---推送网关信息-返回---{}", jsonStr); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("------推送网关信息--执行失败-----{}", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | public String getSign(Map<String, String> parames, String priKey) { |
| | | //参数拼接 |
| | | String msg = ""; |
| | | for (Map.Entry<String, String> param : parames.entrySet()) { |
| | | msg += param.getKey() + "=" + param.getValue() + "&"; |
| | | } |
| | | msg = msg.substring(0, msg.length() - 1); |
| | | |
| | | log.debug("------待加密信息-----{}", msg); |
| | | |
| | | //MD5加密 |
| | | String md5sign = MyMD5Util.getMD5(msg); |
| | | log.debug("------md5加密-----{}", md5sign); |
| | | |
| | | //RSA加密 |
| | | String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey); |
| | | log.debug("------RSA加密-----{}", result); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param kqdm |
| | | * @return |
| | | */ |
| | | public AuthToken getAuthToken(String kqdm) { |
| | | try { |
| | | String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, kqdm); |
| | | |
| | | AuthToken token = (AuthToken) redisUtil.get(key); |
| | | |
| | | if (null == token) { |
| | | log.error("------------------未获取到TOKEN---------------"); |
| | | return null; |
| | | public GatewayConf getCacheConf(String kqdm) { |
| | | return gatewayConfService.getCacheConf(kqdm); |
| | | } |
| | | |
| | | return token; |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | return null; |
| | | } |
| | | } |
| | | private void updateAuthToken(GatewayConf conf) { |
| | | |
| | | private void updateAuthToken(GatewayResponse dto, GatewayConf conf) { |
| | | String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm()); |
| | | AuthToken token = getAuthToken(conf.getKqdm()); |
| | | gatewayConfService.updateCache(conf); |
| | | |
| | | if (null == token) { |
| | | token = new AuthToken(); |
| | | token.setKqdm(conf.getKqdm()); |
| | | } |
| | | |
| | | if (null != dto.getResult()) { |
| | | token.setToken(dto.getResult().getToken()); |
| | | } |
| | | |
| | | redisUtil.set(key, token); |
| | | } |
| | | |
| | | |