From 1ddccea891c6ab169420893eed54138bcde79ef3 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Thu, 6 Aug 2020 16:54:42 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E9=9A=90?= =?UTF-8?q?=E7=A7=81=E4=BF=9D=E6=8A=A4=E5=8A=9F=E8=83=BD=202.=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E5=A2=9E=E5=8A=A0=E9=9A=90=E7=A7=81?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/application-local.properties | 2 + .../dispose/common/DisposeConfigValue.java | 2 + .../com/dispose/common/PrivacyHelper.java | 65 +++++++++++++++++++ .../com/dispose/config/DisposeConfigure.java | 5 ++ .../DisposeDeviceManagerController.java | 25 ++++++- .../java/com/dispose/setup/SystemInitial.java | 6 ++ .../java/com/dispose/test/debug/demo.java | 8 +++ 7 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/dispose/common/PrivacyHelper.java diff --git a/config/application-local.properties b/config/application-local.properties index 71c9bee7..f8741263 100644 --- a/config/application-local.properties +++ b/config/application-local.properties @@ -61,6 +61,8 @@ dispose.check-request-token=true dispose.split_char=, dispose.request-timeout-second=5 +dispose.used-privacy-protect=true + # 迪普设备配置 # 发送超时时间(ms) dptech.soap-conn-timeout-second=60 diff --git a/src/main/java/com/dispose/common/DisposeConfigValue.java b/src/main/java/com/dispose/common/DisposeConfigValue.java index 76237da2..317044fe 100644 --- a/src/main/java/com/dispose/common/DisposeConfigValue.java +++ b/src/main/java/com/dispose/common/DisposeConfigValue.java @@ -16,4 +16,6 @@ public class DisposeConfigValue { * The constant REQUEST_TIMEOUT_MS. */ public static volatile long REQUEST_TIMEOUT_MS = 5 * 1000; + + public static volatile boolean USED_PRIVACY_PROTECT = false; } diff --git a/src/main/java/com/dispose/common/PrivacyHelper.java b/src/main/java/com/dispose/common/PrivacyHelper.java new file mode 100644 index 00000000..b8966c61 --- /dev/null +++ b/src/main/java/com/dispose/common/PrivacyHelper.java @@ -0,0 +1,65 @@ +package com.dispose.common; + +import inet.ipaddr.IPAddress; +import inet.ipaddr.IPAddressSegment; +import inet.ipaddr.IPAddressString; + +/** + * The type Privacy helper. + * + * @author + */ +public class PrivacyHelper { + /** + * Ip address privacy string. + * + * @param ipAddr the ip addr + * @return the string + */ + public static String ipAddressPrivacy(String ipAddr) { + if (ipAddr == null || ipAddr.length() == 0) { + return ipAddr; + } + + IPAddressString addrString = new IPAddressString(ipAddr); + IPAddress addr = addrString.getAddress(); + + if (addr == null) { + return ipAddr; + } + + IPAddressSegment[] segments = addr.getSegments(); + + if (addr.isIPv4()) { + return segments[0].toString() + ".***.***." + segments[3].toString(); + } else { + StringBuilder privyAddr = new StringBuilder(segments[0].toString().replaceAll("0x", "")); + + for (int i = 1; i < segments.length - 1; i++) { + privyAddr.append(":***"); + } + + privyAddr.append(":").append(segments[segments.length - 1].toString().replaceAll("0x", "")); + + return privyAddr.toString(); + } + } + + /** + * Username privacy string. + * + * @param username the username + * @return the string + */ + public static String usernamePrivacy(String username) { + if (username == null || username.length() == 0) { + return username; + } + + if(username.length() < 8) { + return username.substring(0, username.length() / 2) + "****"; + } else { + return username.substring(0, 7) + "****"; + } + } +} diff --git a/src/main/java/com/dispose/config/DisposeConfigure.java b/src/main/java/com/dispose/config/DisposeConfigure.java index 4f5f209f..57a58d8e 100644 --- a/src/main/java/com/dispose/config/DisposeConfigure.java +++ b/src/main/java/com/dispose/config/DisposeConfigure.java @@ -32,4 +32,9 @@ public class DisposeConfigure { * The Request timeout second. */ private String requestTimeoutSecond; + + /** + * The Used privacy protect. + */ + private String usedPrivacyProtect; } diff --git a/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java b/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java index 4c5362b7..b969572a 100644 --- a/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java +++ b/src/main/java/com/dispose/controller/DisposeDeviceManagerController.java @@ -2,11 +2,13 @@ package com.dispose.controller; import com.dispose.common.CommonEnumHandler; import com.dispose.common.DisposeCapacityType; +import com.dispose.common.DisposeConfigValue; import com.dispose.common.DisposeDeviceType; import com.dispose.common.DisposeObjectType; import com.dispose.common.ErrorCode; import com.dispose.common.HttpType; import com.dispose.common.IpAddrType; +import com.dispose.common.PrivacyHelper; import com.dispose.pojo.dto.protocol.base.BaseRespStatus; import com.dispose.pojo.dto.protocol.base.IdArraysReq; import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO; @@ -61,6 +63,12 @@ public class DisposeDeviceManagerController { @Resource private DisposeDeviceManagerService disposeDeviceManagerService; + /** + * Request to device list list. + * + * @param req the req + * @return the list + */ private List requestToDeviceList(AddDeviceReq req) { List devs = new ArrayList<>(); @@ -185,6 +193,12 @@ public class DisposeDeviceManagerController { return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } + /** + * Upgrade dispose device protocol resp dto. + * + * @param mr the mr + * @return the protocol resp dto + */ @PostMapping("/upgrade") @ResponseBody @ApiOperation("更新处置能力节点") @@ -214,6 +228,12 @@ public class DisposeDeviceManagerController { return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } + /** + * Gets all dispose device. + * + * @param mr the mr + * @return the all dispose device + */ @PostMapping("/device_list") @ResponseBody @ApiOperation("删除处置能力节点") @@ -237,7 +257,8 @@ public class DisposeDeviceManagerController { ret.getSecondParam().forEach(v -> { GetDeviceDetail devInfo = new GetDeviceDetail(); devInfo.setId(v.getId().toString()); - devInfo.setIpAddr(v.getIpAddr()); + devInfo.setIpAddr(DisposeConfigValue.USED_PRIVACY_PROTECT ? + PrivacyHelper.ipAddressPrivacy(v.getIpAddr()) : v.getIpAddr()); devInfo.setIpPort(v.getIpPort()); devInfo.setDeviceType(v.getDeviceType().getValue()); devInfo.setAreaCode(v.getAreaCode()); @@ -245,6 +266,8 @@ public class DisposeDeviceManagerController { devInfo.setManufacturer(v.getManufacturer()); devInfo.setModel(v.getModel()); devInfo.setVersion(v.getVersion()); + devInfo.setUserName(DisposeConfigValue.USED_PRIVACY_PROTECT ? + PrivacyHelper.usernamePrivacy(v.getUserName()) : v.getUserName()); devInfo.setUrlType(v.getUrlType().getValue()); devInfo.setReadme(v.getReadme()); devInfo.setDevStatus(v.getStatus().getValue()); diff --git a/src/main/java/com/dispose/setup/SystemInitial.java b/src/main/java/com/dispose/setup/SystemInitial.java index 7795283a..31c93be2 100644 --- a/src/main/java/com/dispose/setup/SystemInitial.java +++ b/src/main/java/com/dispose/setup/SystemInitial.java @@ -75,6 +75,12 @@ public class SystemInitial implements CommandLineRunner { } catch (Exception ex) { log.error("load CHECK_PROTO_REQUEST_TIMEOUT configure error: {}", ex.getMessage()); } + + try { + DisposeConfigValue.USED_PRIVACY_PROTECT = Boolean.parseBoolean(disposeConfigure.getUsedPrivacyProtect()); + } catch (Exception ex) { + log.error("load USED_PRIVACY_PROTECT configure error: {}", ex.getMessage()); + } } /** diff --git a/src/test/java/com/dispose/test/debug/demo.java b/src/test/java/com/dispose/test/debug/demo.java index 761a59c3..ffe43a06 100644 --- a/src/test/java/com/dispose/test/debug/demo.java +++ b/src/test/java/com/dispose/test/debug/demo.java @@ -3,6 +3,7 @@ package com.dispose.test.debug; import com.dispose.common.DisposeDeviceType; import com.dispose.common.HttpType; import com.dispose.common.ObjectStatus; +import com.dispose.common.PrivacyHelper; import com.dispose.pojo.entity.DisposeDevice; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -129,4 +130,11 @@ public class demo { .writerWithDefaultPrettyPrinter() .writeValueAsString(upgradeDisposeDeviceProperties(dev, dev2))); } + + @Test + public void privacyHelper() { + log.info(PrivacyHelper.ipAddressPrivacy("192.168.0.123")); + log.info(PrivacyHelper.ipAddressPrivacy("2001:0000:4136:e378:8000:63bf:3fff:fdd2")); + log.info(PrivacyHelper.ipAddressPrivacy("3fde::fde2")); + } }