| | |
| | | <version>3.0.9</version> |
| | | </dependency> |
| | | |
| | | |
| | | |
| | | <!-- hutoolå å¯è§£å¯å·¥å
·ç±» --> |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-all</artifactId> |
| | | <version>5.0.7</version> |
| | | </dependency> |
| | | |
| | | <!-- éæhttpclient --> |
| | | <dependency> |
| | |
| | | return contextDeviceMap.get(sn); |
| | | } |
| | | |
| | | public static void updateCacheOnline(String sn, String ip, Integer port, boolean isCreate) { |
| | | ApiCommonDevice device = getCommonDeviceCache(sn); |
| | | if (null == device && isCreate) { |
| | | device = new ApiCommonDevice(); |
| | | public static ApiCommonDevice getCommonDeviceCacheByIp(String ip) { |
| | | if (null == ip) return null; |
| | | |
| | | if (contextDeviceMap.isEmpty()) return null; |
| | | |
| | | for (ApiCommonDevice device : contextDeviceMap.values()) { |
| | | if (ip.equals(device)) return device; |
| | | } |
| | | if (null != device) { |
| | | device.setIp(ip); |
| | | device.setPort(port); |
| | | device.setStatus(Constant.YN_Y); |
| | | device.setCode("SUCCESS"); |
| | | device.setMsg("设å¤å¨çº¿"); |
| | | device.setSn(ip); |
| | | contextDeviceMap.put(device.getSn(), device); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static void updateCacheOffline(String sn, String ip, Integer port, boolean isCreate) { |
| | | ApiCommonDevice device = getCommonDeviceCache(sn); |
| | | public static ApiCommonDevice updateCacheOffline(String ip, Integer port) { |
| | | ApiCommonDevice device = getCommonDeviceCacheByIp(ip); |
| | | |
| | | if (null == device && isCreate) { |
| | | device = new ApiCommonDevice(); |
| | | if (null == device) { |
| | | return null; |
| | | } |
| | | |
| | | if (null != device) { |
| | | device.setIp(ip); |
| | | device.setPort(port); |
| | | device.setStatus(Constant.YN_N); |
| | | device.setCode("ERROR"); |
| | | device.setMsg("设å¤ç¦»çº¿"); |
| | | |
| | | contextDeviceMap.put(device.getSn(), device); |
| | | } |
| | | |
| | | return device; |
| | | } |
| | | |
| | | public static void updateCache(ApiCommonDevice device) { |
| | |
| | | package com.fzzy.api.utils; |
| | | |
| | | import org.springframework.util.DigestUtils; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.MessageDigest; |
| | | |
| | |
| | | * |
| | | */ |
| | | public class MyMD5Util { |
| | | |
| | | |
| | | |
| | | public static String charset = "UTF-8"; |
| | | |
| | | /** |
| | | * 1.javaåçç¨æ³ |
| | |
| | | public static String encrypt(String dataStr) { |
| | | try { |
| | | MessageDigest m = MessageDigest.getInstance("MD5"); |
| | | m.update(dataStr.getBytes(StandardCharsets.UTF_8)); |
| | | m.update(dataStr.getBytes(charset)); |
| | | byte[] s = m.digest(); |
| | | StringBuilder result = new StringBuilder(); |
| | | for (byte b : s) { |
| | |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getMD5(String str) { |
| | | String md5 = DigestUtils.md5DigestAsHex(str.getBytes()); |
| | | public static String getMD5(String str) throws Exception{ |
| | | String md5 = DigestUtils.md5DigestAsHex(str.getBytes(charset)); |
| | | return md5; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | String data = AESUtils.decrypt("QG5j4Ihk04GTNSzbsvNmttCBQCLbC57si3lGric1bpVyRv2Fjiy+JG2W1G4r8pYwOUkGFdAu7Ou2Xcz/3C4w9rk8mJtva92ZOdYD1kJxDF9MxKC9LDTHo3XRIPfD5w5Hg38CHzamDcL1pMu8b8d4gOppa0EgWzC28rp+iedNnbY+4Zwu9ANRdW/1d0FYNjSFY3/2Oa8AqA4ufbsDp4U28mBpxQ9hxM6LzTFnbMSXe2xr12+LbyDtkvDaxDGyojZKEGKq+VItw4j8kKx5uD5TGPa+7jienfuiKbdkYFOkzZKY0nokqhqRMBCkJ4xx4aSbZjyP3l5VzB0z1WqKEocD0PNdI3PML9M8tt+AGjd9TWUgklhOl6v+yLorX815Wp9A08nsbDJK1klVlx84vc2dSHyR4u8ITYRlH74PmfeshxR3WGBB4OhD8/GVMUgPd4q8grW8vZoqqzqFrQcoVDc70P5t+Fsqz8BlxDtbwssVhpbF3/iuX2i+VrXKFtUQ1nIHLWJEZIf87BE/DtHrbm1epRJvsZlEWtQf63jydaM+gsnb18Mn5yUnEnESD4OtlgkbSvOdzxk3MKHsUoka9fn0h5y55hLy6EQGm4zlQopvxa0=","a34de38afad2fff932f569e4434fed35"); |
| | | System.out.println(getMD5(data)); |
| | | System.out.println(encrypt("{\"bgdh\":\"0754-88843381\",\"cfs\":17,\"czbz\":\"u\",\"czhm\":\"0754-88323996\",\"dwdm\":\"91440500190354212L\",\"dwlx\":\"19\",\"dwmc\":\"æ±å¤´å¸ç²®é£ä¼ä¸éå¢å
¬å¸\",\"dzyx\":\"lsjtccb@163.com\",\"fddbr\":\"æè¯å¿ \",\"frlxfs\":\"0754-88626738\",\"frsfzh\":\"511131196308124236\",\"jd\":116.73651,\"kqs\":3,\"qylxr\":\"ææ³³ç\",\"wd\":23.37235,\"xzqhdm\":\"440500\",\"ygs\":0,\"yzbm\":\"515000\",\"zcdz\":\"æ±å¤´å¸ç æ± è·¯33å·\",\"zcrq\":\"1992-12-10\",\"zczb\":2328.0,\"zcze\":7524.71,\"zhgxsj\":\"2022-10-20 18:42:51\"}")); |
| | | System.out.println(getMD5( "{\\\"bgdh\\\":\\\"0754-88843381\\\",\\\"cfs\\\":17,\\\"czbz\\\":\\\"u\\\",\\\"czhm\\\":\\\"0754-88323996\\\",\\\"dwdm\\\":\\\"91440500190354212L\\\",\\\"dwlx\\\":\\\"19\\\",\\\"dwmc\\\":\\\"æ±å¤´å¸ç²®é£ä¼ä¸éå¢å
¬å¸\\\",\\\"dzyx\\\":\\\"lsjtccb@163.com\\\",\\\"fddbr\\\":\\\"æè¯å¿ \\\",\\\"frlxfs\\\":\\\"0754-88626738\\\",\\\"frsfzh\\\":\\\"511131196308124236\\\",\\\"jd\\\":116.73651,\\\"kqs\\\":3,\\\"qylxr\\\":\\\"ææ³³ç\\\",\\\"wd\\\":23.37235,\\\"xzqhdm\\\":\\\"440500\\\",\\\"ygs\\\":0,\\\"yzbm\\\":\\\"515000\\\",\\\"zcdz\\\":\\\"æ±å¤´å¸ç æ± è·¯33å·\\\",\\\"zcrq\\\":\\\"1992-12-10\\\",\\\"zczb\\\":2328.0,\\\"zcze\\\":7524.71,\\\"zhgxsj\\\":\\\"2022-10-20 18:42:51\\\"}")); |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public void run(String... args) throws Exception { |
| | | |
| | | log.info("---------MQTTåå§å--------------"); |
| | | mqttPublishService.init(); |
| | | //æ§è¡åå§åæ¹æ¡ |
| | | |
| | | //ç½å
³åå§å |
| | | log.info("---------ç½å
³è®¾å¤åå§å--------------"); |
| | | apiInitService.init(); |
| | | |
| | | |
| | |
| | | scheduled.doWeatherExe(); |
| | | |
| | | //æ´æ°è®¾å¤ç¼å |
| | | log.info("---------设å¤ç¼ååå§å--------------"); |
| | | apiInitService.updateDeviceCache(); |
| | | |
| | | //åå§å车çè¯å« |
| | | log.info("---------车çè¯å«åå§å--------------"); |
| | | apiInitService.initAllLpr(); |
| | | } |
| | | |
| | |
| | | * <p> |
| | | * åºå®æ¶é´ï¼æ¯é´é10åéæ§è¡ä¸æ¬¡ |
| | | */ |
| | | @Scheduled(cron = "0 0/10 * * * ?") |
| | | @Scheduled(cron = "0 0/1 * * * ?") |
| | | public void scheduled() { |
| | | |
| | | //ç½å
³çå¿è·³æ§è¡ |
| | |
| | | public static Map<String, GatewayDevice> cacheMapDeviceId = new HashMap<>(); |
| | | |
| | | /** |
| | | * 设å¤ç¼å |
| | | */ |
| | | public static Map<String, GatewayDevice> cacheMapDeviceSn = new HashMap<>(); |
| | | |
| | | /** |
| | | * 设å¤ç¼å-åªé对 |
| | | */ |
| | | public static Map<String, GatewayDevice> cacheMapDeviceWeight = new HashMap<>(); |
| | |
| | | |
| | | public static void add2Cache(GatewayDevice device) { |
| | | cacheMapDeviceId.put(device.getDeviceId(), device); |
| | | cacheMapDeviceSn.put(device.getDeviceSn(), device); |
| | | |
| | | //ä¿åå°ç£
|
| | | if (GatewayDeviceType.TYPE_01.getCode().equals(device.getType())) { |
| | | cacheMapDeviceWeight.put(device.getDeviceId(), device); |
| | | } |
| | | } |
| | | |
| | | public static Collection<GatewayDevice> allCacheDevice() { |
| | | return cacheMapDeviceId.values(); |
| | | } |
| | | |
| | | public static GatewayDevice getCacheByDeviceId(String deviceId) { |
| | |
| | | } |
| | | |
| | | public static GatewayDevice getCacheByDeviceSn(String deviceSn) { |
| | | return cacheMapDeviceSn.get(deviceSn); |
| | | Collection<GatewayDevice> list = allCacheDevice(); |
| | | if (null == list || list.isEmpty()) return null; |
| | | |
| | | for (GatewayDevice device : list) { |
| | | if (deviceSn.equals(device.getDeviceSn())) return device; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * é对ä¸ä¸ªéè®¯åæºå¯¹å¤ä¸ªä»æ
åµ |
| | | * |
| | | * @param deviceSn |
| | | * @return |
| | | */ |
| | | public static List<GatewayDevice> getCacheByDeviceSn2(String deviceSn) { |
| | | Collection<GatewayDevice> list = allCacheDevice(); |
| | | if (null == list || list.isEmpty()) return null; |
| | | |
| | | List<GatewayDevice> result = new ArrayList<>(); |
| | | for (GatewayDevice device : list) { |
| | | if (deviceSn.equals(device.getDeviceSn())) result.add(device); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | public static List<GatewayDevice> getCacheByDeviceType(String deviceType) { |
| | | List<GatewayDevice> result = new ArrayList<>(); |
| | |
| | | |
| | | public static void removeCache(GatewayDevice data) { |
| | | cacheMapDeviceId.remove(data.getDeviceId()); |
| | | cacheMapDeviceSn.remove(data.getDeviceSn()); |
| | | //cacheMapDeviceSn.remove(data.getDeviceSn()); |
| | | } |
| | | |
| | | public static String getStatus(String sn) { |
| | |
| | | * @param gatewayConf |
| | | */ |
| | | void pushInfo(GatewayConf gatewayConf); |
| | | |
| | | |
| | | /** |
| | | * è·åå½åç½å
³ä¸ç设å¤å表ï¼å¹¶ä¸æ¥è®¾å¤ç¶æ |
| | | * @param gatewayConf |
| | | */ |
| | | void pushDeviceStatus(GatewayConf gatewayConf); |
| | | } |
| | |
| | | |
| | | public static String MESSAGE_TYPE_INVOKE_FUNCTION = "INVOKE_FUNCTION"; |
| | | |
| | | |
| | | |
| | | /** |
| | | * ç²®æ
éæä»¤ |
| | | */ |
| | |
| | | return UUID.randomUUID().toString().replaceAll("-", ""); |
| | | } |
| | | |
| | | // public static String getMd5MessageId() { |
| | | // return MyMD5Util.getMD5(getUUID()); |
| | | // } |
| | | |
| | | |
| | | |
| | | //API_SOURCE-å¿è·³ |
| | | public static String API_SOURCE_TARGET_EQUIPMENT_HEARTBEAT = "TARGET_EQUIPMENT_HEARTBEAT"; |
| | | //API_SOURCE-ç½å
³è®¾å¤ç¶ææ¶æ¯ç |
| | | public static String API_SOURCE_TARGET_EQUIPMENT_STATUS = "TARGET_EQUIPMENT_STATUS"; |
| | | |
| | | /** |
| | | * ä¸åæä»¤å夿¥ætopic |
| | |
| | | */ |
| | | public static String TOPIC_MESSAGE_REPORT = "/device/${productId}/${deviceId}/message/property/report"; |
| | | |
| | | |
| | | /** |
| | | * ç½å
³è®¾å¤ç¶ææ¶æ¯æ¶æ¯ topicï¼TOPIC_EQUIPMENT_HEARTBEAT_{åºç¹ç¼ç } |
| | | */ |
| | | public static String TOPIC_EQUIPMENT_HEARTBEAT = "TOPIC_EQUIPMENT_HEARTBEAT_{kqdm}"; |
| | | |
| | | /** |
| | | * ç½å
³è®¾å¤ç¶ææ¶æ¯æ¶æ¯ topicï¼TOPIC_EQUIPMENT_STATUS_{åºç¹ç¼ç } |
| | | */ |
| | | public static String TOPIC_EQUIPMENT_STATUS = "TOPIC_EQUIPMENT_STATUS_{kqdm}"; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class DeviceStatusData { |
| | | |
| | | //UUID,æ¶æ¯ç¼å· |
| | | private String messageId; |
| | | |
| | | //模åè¯å«ç |
| | | private String apISource; |
| | | |
| | | //ç½å
³ ID |
| | | private String gatewayId; |
| | | |
| | | //ç½å
³ IP |
| | | private String gatewayIp; |
| | | |
| | | //产å ID |
| | | private String productId; |
| | | |
| | | //设å¤ç¼å· |
| | | private String equipId; |
| | | |
| | | //0 代表设å¤ä¸å¨çº¿ 1ï¼1 代表设å¤å¨çº¿ |
| | | private int status = 1; |
| | | |
| | | //䏿¥æ¶é´ yyyy-MM-dd hh:mm:ss |
| | | private String reportTime; |
| | | |
| | | //䏿¥ä¿¡æ¯ä¸äºè¯´æ |
| | | private String memo; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class HeartBeatData { |
| | | |
| | | //UUID,æ¶æ¯ç¼å· |
| | | private String messageId; |
| | | |
| | | //模åè¯å«ç |
| | | private String apISource; |
| | | |
| | | //ç½å
³ ID |
| | | private String gatewayId; |
| | | |
| | | //ç½å
³ IP |
| | | private String gatewayIp; |
| | | |
| | | //ç½å
³ mac å°å |
| | | private String gatewayMac; |
| | | |
| | | //heartbeat é»è®¤ 1 |
| | | private int heartbeat = 1; |
| | | |
| | | //䏿¥æ¶é´ yyyy-MM-dd hh:mm:ss |
| | | private String reportTime; |
| | | |
| | | //䏿¥ä¿¡æ¯ä¸äºè¯´æ |
| | | private String memo; |
| | | } |
| | |
| | | package com.fzzy.gateway.hx2023.service; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.fzzy.api.Constant; |
| | | import com.fzzy.api.data.GatewayDeviceType; |
| | | import com.fzzy.api.data.PushProtocol; |
| | | import com.fzzy.api.utils.*; |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.api.view.repository.ApiLogRep; |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.fzzy.gateway.util.GatewayHttpUtil; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.api.GatewayRemoteService; |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.entity.GatewayConf; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.gateway.hx2023.data.CloudResp; |
| | | import com.fzzy.gateway.util.GatewayRSAUtils; |
| | | import com.fzzy.gateway.hx2023.data.DeviceStatusData; |
| | | import com.fzzy.gateway.hx2023.data.HeartBeatData; |
| | | import com.fzzy.gateway.hx2023.kafka.KafkaDeviceReportService; |
| | | import com.fzzy.gateway.hx2023.util.OpenApiSignatureUtils; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.fzzy.gateway.util.GatewayHttpUtil; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.apache.commons.lang.time.DateFormatUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Comparator; |
| | | import java.util.Collection; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.TreeMap; |
| | | |
| | | |
| | | @Slf4j |
| | |
| | | public class HxGatewayRemoteServiceImpl implements GatewayRemoteService { |
| | | |
| | | |
| | | String charset = "utf-8"; |
| | | |
| | | @Resource |
| | | private ApiLogRep apiLogRep; |
| | | |
| | | @Resource |
| | | private GatewayConfService gatewayConfService; |
| | | @Resource |
| | | private KafkaDeviceReportService kafkaDeviceReportService; |
| | | |
| | | |
| | | @Override |
| | |
| | | |
| | | |
| | | //è·å AccessToken æ¥å£ |
| | | String sign = getSign(params, gatewayConf.getPrivateKey()); |
| | | String sign = getSign(params, gatewayConf); |
| | | 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); |
| | |
| | | |
| | | @Override |
| | | public void heartbeat(GatewayConf gatewayConf) { |
| | | |
| | | log.info("------------宿¶å¿è·³æ§è¡---------{}", gatewayConf.getKqmc()); |
| | | |
| | | 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"; |
| | | HeartBeatData heartBeatData = new HeartBeatData(); |
| | | |
| | | String jsonStr = GatewayHttpUtil.doGet(url, params); |
| | | // log.info("---ç½å
³å¿è·³æ¥å£-è¿å---{}", jsonStr); |
| | | heartBeatData.setMessageId(ContextUtil.getUUID()); |
| | | heartBeatData.setApISource(ScConstant.API_SOURCE_TARGET_EQUIPMENT_HEARTBEAT); |
| | | heartBeatData.setGatewayId(gatewayConf.getGatewayId()); |
| | | heartBeatData.setGatewayIp(gatewayConf.getGatewayIp()); |
| | | heartBeatData.setGatewayMac(null == gatewayConf.getGatewayMac() ? "æ " : gatewayConf.getGatewayMac()); |
| | | heartBeatData.setHeartbeat(1); |
| | | heartBeatData.setReportTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss")); |
| | | heartBeatData.setMemo("FZZY"); |
| | | |
| | | String topic = ScConstant.TOPIC_EQUIPMENT_HEARTBEAT; |
| | | topic = topic.replace("{kqdm}", gatewayConf.getKqdm()); |
| | | |
| | | String messageInfo = JSONObject.toJSONString(heartBeatData); |
| | | |
| | | log.info("---ç½å
³å¿è·³æ¨é--{}-{}", topic, messageInfo); |
| | | |
| | | kafkaDeviceReportService.publishWithTopic(messageInfo, topic); |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | log.error("------ç½å
³å¿è·³æ¥å£--æ§è¡å¤±è´¥-----{}", e); |
| | | } |
| | | |
| | | //æ§è¡å½åç½å
³ç设å¤ç¶ææ¨é |
| | | pushDeviceStatus(gatewayConf); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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("gatewayMac", null == gatewayConf.getGatewayMac() ? "æ " : gatewayConf.getGatewayMac()); |
| | | params.put("gatewayCPU", null == gatewayConf.getGatewayCPU() ? "æ " : gatewayConf.getGatewayCPU()); |
| | | params.put("gatewayMem", null == gatewayConf.getGatewayMem() ? "æ " : gatewayConf.getGatewayMem()); |
| | | params.put("gatewayHardDisk", null == gatewayConf.getGatewayHardDisk() ? "æ " : gatewayConf.getGatewayHardDisk()); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | |
| | | String sign = getSign(params, gatewayConf.getPrivateKey()); |
| | | String sign = getSign(params, gatewayConf); |
| | | params.put("sign", sign); |
| | | |
| | | String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat"; |
| | |
| | | } |
| | | |
| | | |
| | | public String getSign(Map<String, String> parames, String priKey) { |
| | | //åæ°æ¼æ¥ |
| | | String msg = ""; |
| | | parames = sortMapKey(parames); |
| | | for (Map.Entry<String, String> param : parames.entrySet()) { |
| | | msg += param.getKey() + "=" + param.getValue() + "&"; |
| | | } |
| | | msg = msg.substring(0, msg.length() - 1); |
| | | @Override |
| | | public void pushDeviceStatus(GatewayConf gatewayConf) { |
| | | |
| | | log.debug("------å¾
å å¯ä¿¡æ¯-----{}", msg); |
| | | //è·å设å¤å表 |
| | | Collection<GatewayDevice> list = GatewayUtils.allCacheDevice(); |
| | | if (null == list || list.isEmpty()) { |
| | | log.info("--------ç³»ç»æªè·åå°å½åç³»ç»è®¾å¤å表ï¼ä¸æ§è¡ç¶ææ¨é-----"); |
| | | return; |
| | | } |
| | | |
| | | //å°è£
è®¾å¤æ¥æä¿¡æ¯ |
| | | DeviceStatusData statusData; |
| | | String messageInfo; |
| | | String topic = ScConstant.TOPIC_EQUIPMENT_STATUS; |
| | | topic = topic.replace("{kqdm}", gatewayConf.getKqdm()); |
| | | |
| | | for (GatewayDevice device : list) { |
| | | //å¦æè®¾å¤æ²¡æé
ç½®productIdç´æ¥è·³è¿ |
| | | if (StringUtils.isEmpty(device.getProductId())) continue; |
| | | statusData = new DeviceStatusData(); |
| | | statusData.setMessageId(ContextUtil.getUUID()); |
| | | statusData.setApISource(ScConstant.API_SOURCE_TARGET_EQUIPMENT_STATUS); |
| | | statusData.setGatewayId(gatewayConf.getGatewayId()); |
| | | statusData.setGatewayIp(gatewayConf.getGatewayIp()); |
| | | statusData.setProductId(device.getProductId()); |
| | | statusData.setEquipId(device.getDeviceId()); |
| | | statusData.setReportTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss")); |
| | | statusData.setMemo("FZZY"); |
| | | |
| | | //é对粮æ
è®¾å¤ |
| | | if (GatewayDeviceType.TYPE_07.getCode().equals(device.getType())) { |
| | | if (Constant.YN_N.equals(device.getStatus())) statusData.setStatus(0); |
| | | } |
| | | |
| | | messageInfo = JSONObject.toJSONString(statusData); |
| | | //æ¨é设å¤ç¶æ |
| | | kafkaDeviceReportService.publishWithTopic(messageInfo, topic); |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | public String getSign(Map<String, String> parames, GatewayConf gatewayConf) throws Exception { |
| | | |
| | | //åæ°è°æ´ |
| | | String signContent = OpenApiSignatureUtils.getSignContent(parames); |
| | | log.debug("------å¾
å å¯ä¿¡æ¯-----{}", signContent); |
| | | |
| | | //MD5å å¯ |
| | | String md5sign = MyMD5Util.getMD5(msg); |
| | | log.debug("------md5å å¯-----{}", md5sign); |
| | | String md5sign = OpenApiSignatureUtils.getMd5Content(signContent, charset); |
| | | |
| | | //RSAå å¯ |
| | | String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey); |
| | | log.debug("------RSAå å¯-----{}", result); |
| | | String singValue = OpenApiSignatureUtils.doSignByHex(gatewayConf.getGatewayId(), md5sign, charset, gatewayConf.getPublicKey(), gatewayConf.getPrivateKey()); |
| | | log.debug("------RSAå å¯ç¾å-----{}", singValue); |
| | | |
| | | return result; |
| | | } |
| | | public static String getSign2(Map<String, String> parames, String priKey) { |
| | | //åæ°æ¼æ¥ |
| | | String msg = ""; |
| | | parames = sortMapKey(parames); |
| | | 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; |
| | | return singValue; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | private void updateAuthToken(GatewayConf conf) { |
| | | |
| | | gatewayConfService.updateCache(conf); |
| | | |
| | | } |
| | | |
| | | private static Map<String, String> sortMapKey(Map<String, String> map){ |
| | | if(map == null || map.isEmpty()){ |
| | | return null; |
| | | } |
| | | |
| | | |
| | | Map<String, String> sortMap = new TreeMap<>(new Comparator<String>() { |
| | | @Override |
| | | public int compare(String o1, String o2) { |
| | | return o1.compareTo(o2); |
| | | } |
| | | }); |
| | | sortMap.putAll(map); |
| | | return sortMap; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) throws UnsupportedEncodingException { |
| | | //ç½å
³å¿è·³æ¥å£ |
| | | // Map<String, String> params = new HashMap<>(); |
| | | // //params.put("token", ""); |
| | | // params.put("gatewayId", "6e3d92ff71b911eea5e50250f2000002"); |
| | | // params.put("gatewayIp", "123"); |
| | | // params.put("gatewayMac", "123"); |
| | | // params.put("gatewayCPU","123"); |
| | | // params.put("gatewayMem", "123"); |
| | | // params.put("gatewayHardDisk", "123"); |
| | | // params.put("timestamp", "2023-10-31 12:12:12"); |
| | | // String sign = getSign2(params,"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA0lg35sJGRyUFEDzt\np9pWvc0JD+0+SmiZIgMbUaEM5gqBsvUJ9LnzB0rpMTbuqAxAFbUH1Nw4qXdJVnKS\nRYfPgQIDAQABAkBPkxIq2Uou9gFRYEtkGcSA70VDYjQTgDiWVmiKXAv+JkEJmOno\\nMrHYQGAiP0q4xW66F+CLNCrGPVJns2ywMxwxAiEA/vyNX5M0/t+RHAxpMp484i9T\nb5Qf+HvepOV3c0UGZx0CIQDTLj4+qgoXGOLCM9/dzVJZ04VIjK6u546z22hXFWgI\ntQIhAMWFP8JMlx3kc1UF7Cuw1jrkLD7fwJBM7CBSPODHFHmlAiEAhv4Hcf02B/+z\nbBrG3rzSCHOKvo6XV1zTvqrhLtGTOAUCIGmoaIDgTuS0ZF71+c9mYvPVNL0T02GQ\nE8Y3ExreQphx"); |
| | | // params.put("sign", sign); |
| | | // |
| | | // System.out.println(sign); |
| | | byte[] encryptedData = GatewayRSAUtils.decryptBase64("yx7jNj9DGS7AkH/sXo5IrwaGNCSUnAZFmjXwWovMBfvYGVQur1RELXUu4fqIMZZ/ck6CAA3ESOJCS+aCD0aVdA=="); |
| | | StringBuilder hexString = new StringBuilder(); |
| | | for (byte b : encryptedData) { |
| | | String hex = Integer.toHexString(0xFF & b); |
| | | if (hex.length() == 1) { |
| | | hexString.append('0'); |
| | | } |
| | | hexString.append(hex); |
| | | } |
| | | String hexStringResult = hexString.toString(); |
| | | System.out.println(hexStringResult); |
| | | System.out.println(BytesUtil.bytesToString(GatewayRSAUtils.decryptBase64("mgtJOOn5iZBvmQI3hzvaw2J4BiZUcXhLTsgLk8++Ig2iIZcPaky7V+ylkA+BAYxIBS5ovHXjRo4w26TTEq9k7Q=="))); |
| | | |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.service; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.fzzy.api.data.PushProtocol; |
| | | import com.fzzy.api.view.repository.ApiLogRep; |
| | | import com.fzzy.gateway.api.GatewayRemoteService; |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.entity.GatewayConf; |
| | | import com.fzzy.gateway.hx2023.data.CloudResp; |
| | | import com.fzzy.gateway.hx2023.util.OpenApiSignatureUtils; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.fzzy.gateway.util.GatewayHttpUtil; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | |
| | | @Slf4j |
| | | @Data |
| | | @Component |
| | | public class HxGatewayRemoteServiceImpl2 implements GatewayRemoteService { |
| | | |
| | | |
| | | String charset = "utf-8"; |
| | | |
| | | @Resource |
| | | private ApiLogRep apiLogRep; |
| | | |
| | | @Resource |
| | | private GatewayConfService gatewayConfService; |
| | | |
| | | |
| | | @Override |
| | | public String getProtocol() { |
| | | //return PushProtocol.GATEWAY_SC_2023.getCode(); |
| | | return "GATEWAY_SC_2023-æå使ç¨"; |
| | | } |
| | | |
| | | @Override |
| | | public void init(GatewayConf gatewayConf) { |
| | | try { |
| | | |
| | | if (StringUtils.isEmpty(gatewayConf.getApiUrl())) { |
| | | return; |
| | | } |
| | | |
| | | //è·åå
¬ç§é¥æ¥å£ |
| | | 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 (BaseResp.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); |
| | | } |
| | | |
| | | |
| | | //è·å AccessToken æ¥å£ |
| | | String sign = getSign(params, gatewayConf); |
| | | 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 (BaseResp.CODE_200 == respToken.getCode()) { |
| | | JSONObject object = respKey.getData(); |
| | | if (null != object) { |
| | | String token = (String) object.get("token"); |
| | | gatewayConf.setAccessToken(token); |
| | | } |
| | | } |
| | | |
| | | //æ´æ°ç¼å |
| | | updateAuthToken(gatewayConf); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("------åå§å失败-----{}", e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void heartbeat(GatewayConf gatewayConf) { |
| | | |
| | | log.info("------------宿¶å¿è·³æ§è¡---------{}", gatewayConf.getKqmc()); |
| | | |
| | | 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); |
| | | 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); |
| | | 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 pushDeviceStatus(GatewayConf gatewayConf) { |
| | | |
| | | } |
| | | |
| | | |
| | | public String getSign(Map<String, String> parames, GatewayConf gatewayConf) throws Exception { |
| | | |
| | | //åæ°è°æ´ |
| | | String signContent = OpenApiSignatureUtils.getSignContent(parames); |
| | | log.debug("------å¾
å å¯ä¿¡æ¯-----{}", signContent); |
| | | |
| | | //MD5å å¯ |
| | | String md5sign = OpenApiSignatureUtils.getMd5Content(signContent, charset); |
| | | |
| | | //RSAå å¯ |
| | | String singValue = OpenApiSignatureUtils.doSignByHex(gatewayConf.getGatewayId(), md5sign, charset, gatewayConf.getPublicKey(), gatewayConf.getPrivateKey()); |
| | | log.debug("------RSAå å¯ç¾å-----{}", singValue); |
| | | |
| | | return singValue; |
| | | } |
| | | |
| | | /** |
| | | * @param kqdm |
| | | * @return |
| | | */ |
| | | public GatewayConf getCacheConf(String kqdm) { |
| | | return gatewayConfService.getCacheConf(kqdm); |
| | | } |
| | | |
| | | private void updateAuthToken(GatewayConf conf) { |
| | | gatewayConfService.updateCache(conf); |
| | | } |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.util; |
| | | |
| | | /** |
| | | * Base64Utils |
| | | */ |
| | | public class Base64Utils { |
| | | static private final int BASELENGTH = 128; |
| | | static private final int LOOKUPLENGTH = 64; |
| | | static private final int TWENTYFOURBITGROUP = 24; |
| | | static private final int EIGHTBIT = 8; |
| | | static private final int SIXTEENBIT = 16; |
| | | static private final int FOURBYTE = 4; |
| | | static private final int SIGN = -128; |
| | | static private final char PAD = '='; |
| | | static private final boolean fDebug = false; |
| | | static final private byte[] base64Alphabet = new byte[BASELENGTH]; |
| | | static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; |
| | | |
| | | static { |
| | | for (int i = 0; i < BASELENGTH; ++i) { |
| | | base64Alphabet[i] = -1; |
| | | } |
| | | for (int i = 'Z'; i >= 'A'; i--) { |
| | | base64Alphabet[i] = (byte) (i - 'A'); |
| | | } |
| | | for (int i = 'z'; i >= 'a'; i--) { |
| | | base64Alphabet[i] = (byte) (i - 'a' + 26); |
| | | } |
| | | |
| | | for (int i = '9'; i >= '0'; i--) { |
| | | base64Alphabet[i] = (byte) (i - '0' + 52); |
| | | } |
| | | |
| | | base64Alphabet['+'] = 62; |
| | | base64Alphabet['/'] = 63; |
| | | |
| | | for (int i = 0; i <= 25; i++) { |
| | | lookUpBase64Alphabet[i] = (char) ('A' + i); |
| | | } |
| | | |
| | | for (int i = 26, j = 0; i <= 51; i++, j++) { |
| | | lookUpBase64Alphabet[i] = (char) ('a' + j); |
| | | } |
| | | |
| | | for (int i = 52, j = 0; i <= 61; i++, j++) { |
| | | lookUpBase64Alphabet[i] = (char) ('0' + j); |
| | | } |
| | | lookUpBase64Alphabet[62] = '+'; |
| | | lookUpBase64Alphabet[63] = '/'; |
| | | |
| | | } |
| | | |
| | | private static boolean isWhiteSpace(char octect) { |
| | | return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); |
| | | } |
| | | |
| | | private static boolean isPad(char octect) { |
| | | return (octect == PAD); |
| | | } |
| | | |
| | | private static boolean isData(char octect) { |
| | | return (octect < BASELENGTH && base64Alphabet[octect] != -1); |
| | | } |
| | | |
| | | /** |
| | | * Encodes hex octects into Base64 |
| | | * |
| | | * @param binaryData Array containing binaryData |
| | | * @return Encoded Base64 array |
| | | */ |
| | | public static String encode(byte[] binaryData) { |
| | | |
| | | if (binaryData == null) { |
| | | return null; |
| | | } |
| | | |
| | | int lengthDataBits = binaryData.length * EIGHTBIT; |
| | | if (lengthDataBits == 0) { |
| | | return ""; |
| | | } |
| | | |
| | | int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; |
| | | int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; |
| | | int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets; |
| | | char[] encodedData = null; |
| | | |
| | | encodedData = new char[numberQuartet * 4]; |
| | | |
| | | byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; |
| | | |
| | | int encodedIndex = 0; |
| | | int dataIndex = 0; |
| | | if (fDebug) { |
| | | System.out.println("number of triplets = " + numberTriplets); |
| | | } |
| | | |
| | | for (int i = 0; i < numberTriplets; i++) { |
| | | b1 = binaryData[dataIndex++]; |
| | | b2 = binaryData[dataIndex++]; |
| | | b3 = binaryData[dataIndex++]; |
| | | |
| | | if (fDebug) { |
| | | System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3); |
| | | } |
| | | |
| | | l = (byte) (b2 & 0x0f); |
| | | k = (byte) (b1 & 0x03); |
| | | |
| | | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); |
| | | byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); |
| | | byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc); |
| | | |
| | | if (fDebug) { |
| | | System.out.println("val2 = " + val2); |
| | | System.out.println("k4 = " + (k << 4)); |
| | | System.out.println("vak = " + (val2 | (k << 4))); |
| | | } |
| | | |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f]; |
| | | } |
| | | |
| | | // form integral number of 6-bit groups |
| | | if (fewerThan24bits == EIGHTBIT) { |
| | | b1 = binaryData[dataIndex]; |
| | | k = (byte) (b1 & 0x03); |
| | | if (fDebug) { |
| | | System.out.println("b1=" + b1); |
| | | System.out.println("b1<<2 = " + (b1 >> 2)); |
| | | } |
| | | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4]; |
| | | encodedData[encodedIndex++] = PAD; |
| | | encodedData[encodedIndex++] = PAD; |
| | | } else if (fewerThan24bits == SIXTEENBIT) { |
| | | b1 = binaryData[dataIndex]; |
| | | b2 = binaryData[dataIndex + 1]; |
| | | l = (byte) (b2 & 0x0f); |
| | | k = (byte) (b1 & 0x03); |
| | | |
| | | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); |
| | | byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); |
| | | |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2]; |
| | | encodedData[encodedIndex++] = PAD; |
| | | } |
| | | |
| | | return new String(encodedData); |
| | | } |
| | | |
| | | /** |
| | | * Decodes Base64 data into octects |
| | | * |
| | | * @param encoded string containing Base64 data |
| | | * @return Array containind decoded data. |
| | | */ |
| | | public static byte[] decode(String encoded) { |
| | | |
| | | if (encoded == null) { |
| | | return null; |
| | | } |
| | | |
| | | char[] base64Data = encoded.toCharArray(); |
| | | // remove white spaces |
| | | int len = removeWhiteSpace(base64Data); |
| | | |
| | | if (len % FOURBYTE != 0) { |
| | | return null;//should be divisible by four |
| | | } |
| | | |
| | | int numberQuadruple = (len / FOURBYTE); |
| | | |
| | | if (numberQuadruple == 0) { |
| | | return new byte[0]; |
| | | } |
| | | |
| | | byte[] decodedData = null; |
| | | byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; |
| | | char d1 = 0, d2 = 0, d3 = 0, d4 = 0; |
| | | |
| | | int i = 0; |
| | | int encodedIndex = 0; |
| | | int dataIndex = 0; |
| | | decodedData = new byte[(numberQuadruple) * 3]; |
| | | |
| | | for (; i < numberQuadruple - 1; i++) { |
| | | |
| | | if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++])) |
| | | || !isData((d3 = base64Data[dataIndex++])) |
| | | || !isData((d4 = base64Data[dataIndex++]))) { |
| | | return null; |
| | | }//if found "no data" just return null |
| | | |
| | | b1 = base64Alphabet[d1]; |
| | | b2 = base64Alphabet[d2]; |
| | | b3 = base64Alphabet[d3]; |
| | | b4 = base64Alphabet[d4]; |
| | | |
| | | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| | | decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); |
| | | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); |
| | | } |
| | | |
| | | if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) { |
| | | return null;//if found "no data" just return null |
| | | } |
| | | |
| | | b1 = base64Alphabet[d1]; |
| | | b2 = base64Alphabet[d2]; |
| | | |
| | | d3 = base64Data[dataIndex++]; |
| | | d4 = base64Data[dataIndex++]; |
| | | if (!isData((d3)) || !isData((d4))) {//Check if they are PAD characters |
| | | if (isPad(d3) && isPad(d4)) { |
| | | if ((b2 & 0xf) != 0)//last 4 bits should be zero |
| | | { |
| | | return null; |
| | | } |
| | | byte[] tmp = new byte[i * 3 + 1]; |
| | | System.arraycopy(decodedData, 0, tmp, 0, i * 3); |
| | | tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); |
| | | return tmp; |
| | | } else if (!isPad(d3) && isPad(d4)) { |
| | | b3 = base64Alphabet[d3]; |
| | | if ((b3 & 0x3) != 0)//last 2 bits should be zero |
| | | { |
| | | return null; |
| | | } |
| | | byte[] tmp = new byte[i * 3 + 2]; |
| | | System.arraycopy(decodedData, 0, tmp, 0, i * 3); |
| | | tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| | | tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); |
| | | return tmp; |
| | | } else { |
| | | return null; |
| | | } |
| | | } else { //No PAD e.g 3cQl |
| | | b3 = base64Alphabet[d3]; |
| | | b4 = base64Alphabet[d4]; |
| | | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| | | decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); |
| | | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); |
| | | |
| | | } |
| | | |
| | | return decodedData; |
| | | } |
| | | |
| | | /** |
| | | * remove WhiteSpace from MIME containing encoded Base64 data. |
| | | * |
| | | * @param data the byte array of base64 data (with WS) |
| | | * @return the new length |
| | | */ |
| | | private static int removeWhiteSpace(char[] data) { |
| | | if (data == null) { |
| | | return 0; |
| | | } |
| | | |
| | | // count characters that's not whitespace |
| | | int newSize = 0; |
| | | int len = data.length; |
| | | for (int i = 0; i < len; i++) { |
| | | if (!isWhiteSpace(data[i])) { |
| | | data[newSize++] = data[i]; |
| | | } |
| | | } |
| | | return newSize; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.util; |
| | | |
| | | import java.security.*; |
| | | import java.security.interfaces.RSAPrivateKey; |
| | | import java.security.interfaces.RSAPublicKey; |
| | | import java.security.spec.InvalidKeySpecException; |
| | | import java.security.spec.PKCS8EncodedKeySpec; |
| | | import java.security.spec.X509EncodedKeySpec; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import javax.crypto.BadPaddingException; |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.IllegalBlockSizeException; |
| | | import javax.crypto.NoSuchPaddingException; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * RSAç¾åéªç¾ç±» |
| | | * |
| | | */ |
| | | @Slf4j |
| | | @Data |
| | | public class OpenApiRsaSigner { |
| | | |
| | | /** |
| | | * åç»ç¨äºç¼åå
¬é¥åç§é¥ |
| | | */ |
| | | private static Map<String, OpenApiRsaSigner> signerMap = new HashMap<>(); |
| | | |
| | | private PublicKey publicKey; |
| | | private PrivateKey privateKey; |
| | | |
| | | /** |
| | | * åèæ°æ®è½¬å符串ä¸ç¨éå |
| | | */ |
| | | private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| | | |
| | | private OpenApiRsaSigner(PublicKey publicKey, PrivateKey privateKey) { |
| | | this.publicKey = publicKey; |
| | | this.privateKey = privateKey; |
| | | } |
| | | |
| | | public static OpenApiRsaSigner initSigner(String publicKey, String privateKey) { |
| | | try { |
| | | PublicKey publicKeyObj = loadPublicKeyByStr(publicKey); |
| | | PrivateKey privateKeyObj = loadPrivateKeyByStr(privateKey); |
| | | return new OpenApiRsaSigner(publicKeyObj, privateKeyObj); |
| | | }catch (Exception e) { |
| | | log.error("initSigner(),exception happens.", e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * ä»å符串ä¸å è½½å
¬é¥ |
| | | * |
| | | * @param publicKeyStr å
¬é¥æ°æ®å符串 |
| | | * @throws Exception å è½½å
¬é¥æ¶äº§ççå¼å¸¸ |
| | | */ |
| | | public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception { |
| | | try { |
| | | byte[] buffer = Base64Utils.decode(publicKeyStr); |
| | | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| | | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); |
| | | return (RSAPublicKey) keyFactory.generatePublic(keySpec); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new Exception("æ æ¤ç®æ³"); |
| | | } catch (InvalidKeySpecException e) { |
| | | throw new Exception("å
¬é¥éæ³"); |
| | | } catch (NullPointerException e) { |
| | | throw new Exception("å
¬é¥æ°æ®ä¸ºç©º"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @param privateKeyStr |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr) throws Exception { |
| | | try { |
| | | byte[] buffer = Base64Utils.decode(privateKeyStr); |
| | | PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); |
| | | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| | | return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new Exception("æ æ¤ç®æ³"); |
| | | } catch (InvalidKeySpecException e) { |
| | | throw new Exception("ç§é¥éæ³"); |
| | | } catch (NullPointerException e) { |
| | | throw new Exception("ç§é¥æ°æ®ä¸ºç©º"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å
¬é¥å å¯è¿ç¨ |
| | | * |
| | | * @param publicKey å
¬é¥ |
| | | * @param plainTextData æææ°æ® |
| | | * @return |
| | | * @throws Exception å å¯è¿ç¨ä¸çå¼å¸¸ä¿¡æ¯ |
| | | */ |
| | | public static byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws Exception { |
| | | if (publicKey == null) { |
| | | throw new Exception("å å¯å
¬é¥ä¸ºç©º, 请设置"); |
| | | } |
| | | Cipher cipher = null; |
| | | try { |
| | | // 使ç¨é»è®¤RSA |
| | | cipher = Cipher.getInstance("RSA"); |
| | | // cipher= Cipher.getInstance("RSA", new BouncyCastleProvider()); |
| | | cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
| | | byte[] output = cipher.doFinal(plainTextData); |
| | | return output; |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new Exception("æ æ¤å å¯ç®æ³"); |
| | | } catch (NoSuchPaddingException e) { |
| | | log.error("exception happens.", e); |
| | | return null; |
| | | } catch (InvalidKeyException e) { |
| | | throw new Exception("å å¯å
¬é¥éæ³,è¯·æ£æ¥"); |
| | | } catch (IllegalBlockSizeException e) { |
| | | throw new Exception("ææé¿åº¦éæ³"); |
| | | } catch (BadPaddingException e) { |
| | | throw new Exception("æææ°æ®å·²æå"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç§é¥å å¯è¿ç¨ |
| | | * |
| | | * @param privateKey ç§é¥ |
| | | * @param plainTextData æææ°æ® |
| | | * @return |
| | | * @throws Exception å å¯è¿ç¨ä¸çå¼å¸¸ä¿¡æ¯ |
| | | */ |
| | | public static byte[] encrypt(RSAPrivateKey privateKey, byte[] plainTextData) throws Exception { |
| | | if (privateKey == null) { |
| | | throw new Exception("å å¯ç§é¥ä¸ºç©º, 请设置"); |
| | | } |
| | | Cipher cipher = null; |
| | | try { |
| | | // 使ç¨é»è®¤RSA |
| | | cipher = Cipher.getInstance("RSA"); |
| | | cipher.init(Cipher.ENCRYPT_MODE, privateKey); |
| | | byte[] output = cipher.doFinal(plainTextData); |
| | | return output; |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new Exception("æ æ¤å å¯ç®æ³"); |
| | | } catch (NoSuchPaddingException e) { |
| | | log.error("exception happens.", e); |
| | | return null; |
| | | } catch (InvalidKeyException e) { |
| | | throw new Exception("å å¯ç§é¥éæ³,è¯·æ£æ¥"); |
| | | } catch (IllegalBlockSizeException e) { |
| | | throw new Exception("ææé¿åº¦éæ³"); |
| | | } catch (BadPaddingException e) { |
| | | throw new Exception("æææ°æ®å·²æå"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç§é¥è§£å¯è¿ç¨ |
| | | * |
| | | * @param privateKey ç§é¥ |
| | | * @param cipherData å¯ææ°æ® |
| | | * @return ææ |
| | | * @throws Exception è§£å¯è¿ç¨ä¸çå¼å¸¸ä¿¡æ¯ |
| | | */ |
| | | public static byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws Exception { |
| | | if (privateKey == null) { |
| | | throw new Exception("è§£å¯ç§é¥ä¸ºç©º, 请设置"); |
| | | } |
| | | |
| | | if (cipherData == null) { |
| | | return null; |
| | | } |
| | | |
| | | Cipher cipher = null; |
| | | try { |
| | | // 使ç¨é»è®¤RSA |
| | | cipher = Cipher.getInstance("RSA"); |
| | | // cipher= Cipher.getInstance("RSA", new BouncyCastleProvider()); |
| | | cipher.init(Cipher.DECRYPT_MODE, privateKey); |
| | | byte[] output = cipher.doFinal(cipherData); |
| | | return output; |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new Exception("æ æ¤è§£å¯ç®æ³"); |
| | | } catch (NoSuchPaddingException e) { |
| | | log.error("exception happens.", e); |
| | | return null; |
| | | } catch (InvalidKeyException e) { |
| | | throw new Exception("è§£å¯ç§é¥éæ³,è¯·æ£æ¥"); |
| | | } catch (IllegalBlockSizeException e) { |
| | | throw new Exception("坿é¿åº¦éæ³"); |
| | | } catch (BadPaddingException e) { |
| | | throw new Exception("å¯ææ°æ®å·²æå"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å
¬é¥è§£å¯è¿ç¨ |
| | | * |
| | | * @param publicKey å
¬é¥ |
| | | * @param cipherData å¯ææ°æ® |
| | | * @return ææ |
| | | * @throws Exception è§£å¯è¿ç¨ä¸çå¼å¸¸ä¿¡æ¯ |
| | | */ |
| | | public static byte[] decrypt(RSAPublicKey publicKey, byte[] cipherData) throws Exception { |
| | | if (publicKey == null) { |
| | | throw new Exception("è§£å¯å
¬é¥ä¸ºç©º, 请设置"); |
| | | } |
| | | Cipher cipher = null; |
| | | try { |
| | | // 使ç¨é»è®¤RSA |
| | | cipher = Cipher.getInstance("RSA"); |
| | | // cipher= Cipher.getInstance("RSA", new BouncyCastleProvider()); |
| | | cipher.init(Cipher.DECRYPT_MODE, publicKey); |
| | | byte[] output = cipher.doFinal(cipherData); |
| | | return output; |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new Exception("æ æ¤è§£å¯ç®æ³"); |
| | | } catch (NoSuchPaddingException e) { |
| | | log.error("exception happens.", e); |
| | | return null; |
| | | } catch (InvalidKeyException e) { |
| | | throw new Exception("è§£å¯å
¬é¥éæ³,è¯·æ£æ¥"); |
| | | } catch (IllegalBlockSizeException e) { |
| | | throw new Exception("坿é¿åº¦éæ³"); |
| | | } catch (BadPaddingException e) { |
| | | throw new Exception("å¯ææ°æ®å·²æå"); |
| | | } |
| | | } |
| | | |
| | | public static boolean verify(String content, String sign, RSAPrivateKey privateKey) throws Exception { |
| | | byte[] singBytes = encrypt(privateKey, content.getBytes()); |
| | | |
| | | return sign.equals(new String(singBytes)); |
| | | } |
| | | |
| | | /** |
| | | * åèæ°æ®è½¬åå
è¿å¶å符串 |
| | | * |
| | | * @param data è¾å
¥æ°æ® |
| | | * @return åå
è¿å¶å
容 |
| | | */ |
| | | public static String byteArrayToString(byte[] data) { |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | for (int i = 0; i < data.length; i++) { |
| | | // ååºåèçé«åä½ ä½ä¸ºç´¢å¼å¾å°ç¸åºçåå
è¿å¶æ è¯ç¬¦ æ³¨ææ ç¬¦å·å³ç§» |
| | | stringBuilder.append(HEX_CHAR[(data[i] & 0xf0) >>> 4]); |
| | | // ååºåèçä½åä½ ä½ä¸ºç´¢å¼å¾å°ç¸åºçåå
è¿å¶æ è¯ç¬¦ |
| | | stringBuilder.append(HEX_CHAR[(data[i] & 0x0f)]); |
| | | if (i < data.length - 1) { |
| | | stringBuilder.append(' '); |
| | | } |
| | | } |
| | | return stringBuilder.toString(); |
| | | } |
| | | |
| | | public static OpenApiRsaSigner getInstance(String appId, String publicKey, String privateKey) { |
| | | OpenApiRsaSigner signer = signerMap.get(appId); |
| | | if (signer != null) { |
| | | return signer; |
| | | } |
| | | try { |
| | | signer = initSigner(publicKey, privateKey); |
| | | if(signer != null){ |
| | | signerMap.put(appId, signer); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("exception happens.", e); |
| | | } |
| | | return signerMap.get(appId); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.util; |
| | | |
| | | import cn.hutool.crypto.digest.MD5; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.commons.codec.binary.Hex; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.interfaces.RSAPrivateKey; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * ç¾åå·¥å
·ç±» |
| | | */ |
| | | @Slf4j |
| | | @Data |
| | | public class OpenApiSignatureUtils { |
| | | |
| | | public static String getMd5Content(String content, String charset) { |
| | | try { |
| | | byte[] md5 = MD5.create().digest(content); |
| | | return new String(md5, charset); |
| | | } catch (UnsupportedEncodingException e) { |
| | | log.error("getMd5Content(),exception happens.", e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static String getSignContent(final Map<String, String> sortedParams) { |
| | | StringBuffer content = new StringBuffer(); |
| | | List<String> keys = new ArrayList(sortedParams.keySet()); |
| | | Collections.sort(keys); |
| | | int index = 0; |
| | | for (int i = 0; i < keys.size(); i++) { |
| | | String key = keys.get(i); |
| | | String value = sortedParams.get(key); |
| | | if (key.equals("sign") || key.equals("sign_type")) { |
| | | continue; |
| | | } |
| | | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)) { |
| | | content.append((index == 0 ? "" : "&") + key + "=" + value); |
| | | index++; |
| | | } |
| | | } |
| | | return content.toString(); |
| | | } |
| | | |
| | | public static String getSignContentWithQuotes(final Map<String, String> sortedParams) { |
| | | StringBuffer content = new StringBuffer(); |
| | | List<String> keys = new ArrayList<String>(sortedParams.keySet()); |
| | | Collections.sort(keys); |
| | | int index = 0; |
| | | for (int i = 0; i < keys.size(); i++) { |
| | | String key = keys.get(i); |
| | | String value = sortedParams.get(key); |
| | | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)) { |
| | | content.append((index == 0 ? "" : "&") + key + "=\"" + value + "\""); |
| | | index++; |
| | | } |
| | | } |
| | | return content.toString(); |
| | | } |
| | | |
| | | public static boolean doCheckV1(String appId, Map<String, String> params, String charset, String publicKey, String privateKye) throws Exception { |
| | | String sign = params.get("sign"); |
| | | String content = getSignCheckContentWithoutSign(params); |
| | | return doRsaCheckContent(appId, content, sign, charset, publicKey, privateKye); |
| | | } |
| | | |
| | | public static String doSignByBase64(String appId, String content, String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | byte[] signData = OpenApiRsaSigner.encrypt((RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(), content.getBytes(charset)); |
| | | //return new String(signData, charset); |
| | | return new String(Base64.encodeBase64(signData), StandardCharsets.UTF_8); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";charset=" + charset, e); |
| | | } |
| | | } |
| | | |
| | | public static String doSignByHex(String appId, String content, String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | byte[] signData = OpenApiRsaSigner.encrypt((RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(), content.getBytes(charset)); |
| | | //return new String(signData, charset); |
| | | return new String(Hex.encodeHex(signData)); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";charset=" + charset, e); |
| | | } |
| | | } |
| | | |
| | | public static String doSignByTranscode(String appId, String content, String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | byte[] signData = OpenApiRsaSigner.encrypt((RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(), content.getBytes(charset)); |
| | | return new String(signData, charset); |
| | | //return new String(Base64.encodeBase64(signData), StandardCharsets.UTF_8); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";charset=" + charset, e); |
| | | } |
| | | } |
| | | |
| | | public static String getSignCheckContentWithoutSign(Map<String, String> params) { |
| | | if (params == null) { |
| | | return null; |
| | | } |
| | | params.remove("sign"); |
| | | params.remove("sign_type"); |
| | | |
| | | StringBuffer content = new StringBuffer(); |
| | | List<String> keys = new ArrayList<String>(params.keySet()); |
| | | Collections.sort(keys); |
| | | |
| | | for (int i = 0; i < keys.size(); i++) { |
| | | String key = keys.get(i); |
| | | String value = params.get(key); |
| | | content.append((i == 0 ? "" : "&") + key + "=" + value); |
| | | } |
| | | return content.toString(); |
| | | } |
| | | |
| | | public static boolean doRsaCheckContent(String appId, String content, String sign, |
| | | String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | RSAPrivateKey privateKey = (RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(); |
| | | return OpenApiRsaSigner.verify(content, sign, privateKey); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";sign=" + sign + ";charset=" |
| | | + charset, e); |
| | | } |
| | | } |
| | | |
| | | public static boolean doCheckByBase64(String appId, String content, String sign, |
| | | String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | RSAPrivateKey privateKey = (RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(); |
| | | return OpenApiRsaSigner.verify(content, new String(Base64.decodeBase64(sign), StandardCharsets.UTF_8), privateKey); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";sign=" + sign + ";charset=" |
| | | + charset, e); |
| | | } |
| | | } |
| | | |
| | | public static boolean doCheckByHex(String appId, String content, String sign, |
| | | String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | RSAPrivateKey privateKey = (RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(); |
| | | return OpenApiRsaSigner.verify(content, new String(Hex.decodeHex(sign.toCharArray())), privateKey); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";sign=" + sign + ";charset=" |
| | | + charset, e); |
| | | } |
| | | } |
| | | |
| | | public static boolean doCheckByTranscode(String appId, String content, String sign, |
| | | String charset, String publicKey, String privateKye) throws RuntimeException { |
| | | try { |
| | | RSAPrivateKey privateKey = (RSAPrivateKey) OpenApiRsaSigner.getInstance(appId, publicKey, privateKye).getPrivateKey(); |
| | | return OpenApiRsaSigner.verify(content, sign, privateKey); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("key=" + appId + ";content=" + content + ";sign=" + sign + ";charset=" |
| | | + charset, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å请æ±åæ° |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static Map<String, String> getParameterMap(HttpServletRequest request) { |
| | | Enumeration<String> parameters = request.getParameterNames(); |
| | | if (parameters == null) { |
| | | return new HashMap<>(); |
| | | } |
| | | |
| | | HashMap resultMap = new HashMap<>(); |
| | | while (parameters.hasMoreElements()) { |
| | | String key = parameters.nextElement(); |
| | | String value = request.getParameter(key); |
| | | resultMap.put(key, value); |
| | | } |
| | | |
| | | return resultMap; |
| | | } |
| | | |
| | | /** |
| | | * æµè¯é»è¾ |
| | | * |
| | | * @param args |
| | | * @throws UnsupportedEncodingException |
| | | */ |
| | | public static void testSign(String[] args) throws UnsupportedEncodingException { |
| | | Map<String, String> sortedParams = new HashMap<String, String>(); |
| | | sortedParams.put("appId", "6e3d896b71b911eea5e50250f2000002"); |
| | | // sortedParams.put("method", "test"); |
| | | // sortedParams.put("format", "json"); |
| | | // sortedParams.put("charset", "utf-8"); |
| | | // sortedParams.put("sign_type", "RSA"); |
| | | // sortedParams.put("timestamp", "2019-01-01 00:00:00"); |
| | | // sortedParams.put("version", "1.0"); |
| | | // sortedParams.put("biz_content", "{\"name\":\"å¼ ä¸\",\"age\":18}"); |
| | | } |
| | | } |
| | |
| | | import com.fzzy.api.Constant; |
| | | import com.fzzy.api.data.ApiCommonDevice; |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.async.fzzy40.Fzzy40CommonService; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.api.GatewayRemoteManager; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.service.repository.GatewayDeviceRep; |
| | | import com.fzzy.mqtt.MqttProviderConfig; |
| | | import com.fzzy.mqtt.MqttPublishService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.data.domain.Sort; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | |
| | | |
| | | List<GatewayDevice> result = new ArrayList<>(); |
| | | |
| | | ApiCommonDevice apiCommonDevice; |
| | | GatewayDevice cacheDevice; |
| | | for (GatewayDevice device : list) { |
| | | |
| | | apiCommonDevice = Constant.getCommonDeviceCache(device.getDeviceSn()); |
| | | if (null != apiCommonDevice) { |
| | | device.setIp(apiCommonDevice.getIp()); |
| | | device.setPort(apiCommonDevice.getPort()); |
| | | device.setStatus(apiCommonDevice.getStatus()); |
| | | device.setOnlineTime(apiCommonDevice.getOnlineTime()); |
| | | device.setStatus(Constant.YN_Y); |
| | | cacheDevice = GatewayUtils.getCacheByDeviceId(device.getDeviceId()); |
| | | if (null != cacheDevice) { |
| | | device.setIp(cacheDevice.getIp()); |
| | | device.setPort(cacheDevice.getPort()); |
| | | device.setStatus(cacheDevice.getStatus()); |
| | | device.setOnlineTime(cacheDevice.getOnlineTime()); |
| | | } |
| | | result.add(device); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | // /** |
| | | // * gatewayDeviceService#getQuery |
| | | // * |
| | | // * @return |
| | | // */ |
| | | // @DataProvider |
| | | // public GateWayTestParam getQuery() { |
| | | // GateWayTestParam param = new GateWayTestParam(); |
| | | // param.setDayTime(new Date()); |
| | | // param.setCarNumber("å·A12345"); |
| | | // param.setEnd(new Date()); |
| | | // param.setStart(new Date()); |
| | | // param.setWeight(25000.00); |
| | | // return param; |
| | | // } |
| | | |
| | | /** |
| | | * gatewayDeviceService#updateSave |
| | |
| | | log.info("-----------test-------------------"); |
| | | return "SUCCESS"; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®å®é
éè®¯åæºè®¾ç½®ï¼å½ååæºå¨çº¿ |
| | | * |
| | | * @param commonDevice å®é
éè®¯è®¾å¤ |
| | | */ |
| | | public void onlineByCommonDevice(ApiCommonDevice commonDevice) { |
| | | List<GatewayDevice> list = GatewayUtils.getCacheByDeviceSn2(commonDevice.getSn()); |
| | | if (null == list || list.isEmpty()) return; |
| | | |
| | | for (GatewayDevice device : list) { |
| | | device.setIp(commonDevice.getIp()); |
| | | device.setPort(commonDevice.getPort()); |
| | | device.setOnlineTime(new Date()); |
| | | device.setStatus(Constant.YN_Y); |
| | | |
| | | GatewayUtils.add2Cache(device); |
| | | } |
| | | } |
| | | |
| | | public void OfflineByCommonDevice(ApiCommonDevice commonDevice) { |
| | | List<GatewayDevice> list = GatewayUtils.getCacheByDeviceSn2(commonDevice.getSn()); |
| | | if (null == list || list.isEmpty()) return; |
| | | |
| | | for (GatewayDevice device : list) { |
| | | device.setIp(commonDevice.getIp()); |
| | | device.setPort(commonDevice.getPort()); |
| | | //device.setOnlineTime(new Date()); |
| | | device.setStatus(Constant.YN_N); |
| | | |
| | | GatewayUtils.add2Cache(device); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | HttpGet method = new HttpGet(getUrl); |
| | | method.setHeader("Accept", "application/json"); |
| | | method.setHeader("charset", "UTF-8"); |
| | | method.setHeader("charset", "utf-8"); |
| | | |
| | | response = client.execute(method); |
| | | // response.setHeader("Accept", "application/json"); |
| | |
| | | import com.fzzy.api.data.ApiCommonDevice; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.service.GatewayDeviceService; |
| | | import com.fzzy.protocol.fzzy.builder.ObjectCommandBuilder; |
| | | import com.fzzy.protocol.fzzy.builder.SimpleCommandBuilder; |
| | | import com.fzzy.protocol.fzzy.cmd.BaseRemoteImpl; |
| | |
| | | import org.apache.commons.lang3.time.DateFormatUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | |
| | | public class AnalysisSystem extends BaseRemoteImpl { |
| | | |
| | | public static final String BEAN_ID = "fzzy.analysisSystem"; |
| | | |
| | | |
| | | @Resource |
| | | private GatewayDeviceService gatewayDeviceService; |
| | | |
| | | |
| | | public void analysis1001(ReMessage reMessage) { |
| | |
| | | device.setSn(reMessage.getSn()); |
| | | device.setId(reMessage.getIedId()); |
| | | device.setOnlineTime(new Date()); |
| | | device.setCode("SUCCESS"); |
| | | Constant.updateCache(device); |
| | | |
| | | //æ´æ°è®¾å¤å¨çº¿ |
| | | gatewayDeviceService.onlineByCommonDevice(device); |
| | | |
| | | |
| | | //è¿åå½åç³»ç»æ¶é´ |
| | | Response1001 response = new Response1001(); |
| | |
| | | package com.fzzy.protocol.fzzy.server; |
| | | |
| | | import com.fzzy.api.Constant; |
| | | import com.fzzy.api.data.ApiCommonDevice; |
| | | import com.fzzy.api.utils.SpringUtil; |
| | | import com.fzzy.gateway.service.GatewayDeviceService; |
| | | import com.ld.io.api.IoSession; |
| | | import com.ld.io.api.IoSessionListener; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | @Slf4j |
| | | public class SessionListener implements IoSessionListener { |
| | | |
| | | private GatewayDeviceService gatewayDeviceService; |
| | | |
| | | @Override |
| | | public void onCreate(IoSession session) { |
| | |
| | | @Override |
| | | public void onDestroy(IoSession session) { |
| | | log.info("----FZZY-æ§å¶ä¸ªåè®®æè®¾å¤ç¦»çº¿-----IP={}ï¼PORT={}", session.getAddress(), session.getPort()); |
| | | |
| | | //è®¾ç½®åæºæçº¿ |
| | | ApiCommonDevice commonDevice = Constant.updateCacheOffline(session.getAddress(), session.getPort()); |
| | | |
| | | if (null == commonDevice) return; |
| | | |
| | | |
| | | if (null == gatewayDeviceService) { |
| | | gatewayDeviceService = SpringUtil.getBean(GatewayDeviceService.class); |
| | | |
| | | gatewayDeviceService.OfflineByCommonDevice(commonDevice); |
| | | } |
| | | |
| | | } |
| | | } |
| | |
| | | |
| | | package com.fzzy.protocol.sdkhk.common; |
| | | |
| | | import com.fzzy.api.data.GatewayDeviceType; |
| | | import com.fzzy.api.utils.SpringUtil; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.api.GatewayRemoteManager; |
| | |
| | | package com.fzzy.protocol.weightyh; |
| | | |
| | | import com.fzzy.api.data.GatewayDeviceType; |
| | | import com.fzzy.api.utils.BytesUtil; |
| | | import com.fzzy.api.utils.SpringUtil; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | |
| | | max-idle: 10 |
| | | min-idle: 0 |
| | | timeout: 6000 |
| | | |
| | | kafka: |
| | | bootstrap-servers: 103.203.217.42:9092 |
| | | security-protocol: SASL_PLAINTEXT |
| | |
| | | sasl-jaas-config: org.apache.kafka.common.security.scram.ScramLoginModule required username=\"{username}\" password=\"{password}\";" |
| | | sasl-username: sc001 |
| | | sasl-password: wCV0ISwmoKwbx1lpBKMW |
| | | enabled: false |
| | | producer: |
| | | retries: 0 |
| | | acks: 1 |
| | |
| | | max-idle: 10 |
| | | min-idle: 0 |
| | | timeout: 6000 |
| | | |
| | | kafka: |
| | | bootstrap-servers: 103.203.217.16:9092 |
| | | enabled: false |
| | | producer: |
| | | retries: 0 |
| | | acks: 1 |
| | |
| | | timeout: 6000 |
| | | kafka: |
| | | bootstrap-servers: 103.203.217.16:9092 |
| | | enabled: false |
| | | producer: |
| | | retries: 0 |
| | | acks: 1 |
| | |
| | | timeout: 6000 |
| | | kafka: |
| | | bootstrap-servers: 103.203.217.16:9092 |
| | | enabled: false |
| | | producer: |
| | | retries: 0 |
| | | acks: 1 |