diff --git a/pom.xml b/pom.xml index 23da1b2..99509f6 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,21 @@ org.projectlombok lombok + + org.apache.httpcomponents + httpclient + 4.4.1 + + + org.apache.httpcomponents + httpcore + 4.4.1 + + + org.apache.httpcomponents + httpmime + 4.4.1 + diff --git a/src/main/java/com/sentinel/license/LicenseApplication.java b/src/main/java/com/license/LicenseApplication.java similarity index 90% rename from src/main/java/com/sentinel/license/LicenseApplication.java rename to src/main/java/com/license/LicenseApplication.java index adf5b56..25c1c46 100644 --- a/src/main/java/com/sentinel/license/LicenseApplication.java +++ b/src/main/java/com/license/LicenseApplication.java @@ -1,4 +1,4 @@ -package com.sentinel.license; +package com.license; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/main/java/com/sentinel/license/bean/Code.java b/src/main/java/com/license/bean/Code.java similarity index 95% rename from src/main/java/com/sentinel/license/bean/Code.java rename to src/main/java/com/license/bean/Code.java index 5eb3c76..b127d7b 100644 --- a/src/main/java/com/sentinel/license/bean/Code.java +++ b/src/main/java/com/license/bean/Code.java @@ -1,4 +1,4 @@ -package com.sentinel.license.bean; +package com.license.bean; /** diff --git a/src/main/java/com/sentinel/license/bean/LicenseInfo.java b/src/main/java/com/license/bean/LicenseInfo.java similarity index 80% rename from src/main/java/com/sentinel/license/bean/LicenseInfo.java rename to src/main/java/com/license/bean/LicenseInfo.java index 929d4a4..988a03c 100644 --- a/src/main/java/com/sentinel/license/bean/LicenseInfo.java +++ b/src/main/java/com/license/bean/LicenseInfo.java @@ -1,4 +1,4 @@ -package com.sentinel.license.bean; +package com.license.bean; import lombok.Data; @@ -19,5 +19,7 @@ public class LicenseInfo { // 1 获取指纹 2 更新license 3 获取license信息 4 验证license信息 private Integer type; + //提供服务标识 + private String serverId; } diff --git a/src/main/java/com/sentinel/license/bean/Ret.java b/src/main/java/com/license/bean/Ret.java similarity index 97% rename from src/main/java/com/sentinel/license/bean/Ret.java rename to src/main/java/com/license/bean/Ret.java index 8018f0d..e464646 100644 --- a/src/main/java/com/sentinel/license/bean/Ret.java +++ b/src/main/java/com/license/bean/Ret.java @@ -1,4 +1,4 @@ -package com.sentinel.license.bean; +package com.license.bean; import java.util.Arrays; diff --git a/src/main/java/com/sentinel/license/bean/Rets.java b/src/main/java/com/license/bean/Rets.java similarity index 94% rename from src/main/java/com/sentinel/license/bean/Rets.java rename to src/main/java/com/license/bean/Rets.java index db00370..9844880 100644 --- a/src/main/java/com/sentinel/license/bean/Rets.java +++ b/src/main/java/com/license/bean/Rets.java @@ -1,4 +1,5 @@ -package com.sentinel.license.bean; +package com.license.bean; + public class Rets { diff --git a/src/main/java/com/license/cache/CacheManager.java b/src/main/java/com/license/cache/CacheManager.java new file mode 100644 index 0000000..8f4b56f --- /dev/null +++ b/src/main/java/com/license/cache/CacheManager.java @@ -0,0 +1,53 @@ +package com.license.cache; + +import org.springframework.stereotype.Component; + +import java.util.HashMap; + +/** + * @author fengjunfeng + * @date 2022/1/20 + * @time 10:24 + * @description + **/ +@Component +public class CacheManager { + /** + */ + private static HashMap map = new HashMap<>(); + +// /** +// * 保存时间 +// */ +// private static int liveTime = 60; + + /** + * 信息缓存 + * @param key + * @param v + * @return + */ + public static boolean addCache(String key,String v){ + map.put(key,v); + return true; + } + + /** + * 缓存信息 + * @param key + * @return + */ + public static boolean delCache(String key){ + map.remove(key); + return true; + } + + /** + * @param key + * @return + */ + public static String getCache(String key){ + return map.get(key); + } + +} diff --git a/src/main/java/com/license/config/RestTemplateConfig.java b/src/main/java/com/license/config/RestTemplateConfig.java new file mode 100644 index 0000000..9a16fef --- /dev/null +++ b/src/main/java/com/license/config/RestTemplateConfig.java @@ -0,0 +1,121 @@ +package com.license.config; + +import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.client.ResponseErrorHandler; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +/** +* +* @author fengjunfeng +* @date 2022/1/20 +* @time 11:12 +* @description +* +**/ +@Configuration +public class RestTemplateConfig { + @Value("${http.maxTotal}") + private String maxTotal; + @Value("${http.defaultMaxPerRoute}") + private String defaultMaxPerRoute; + @Value("${http.connectTimeout}") + private String connectTimeout; + @Value("${http.connectionRequestTimeout}") + private String connectionRequestTimeout; + @Value("${http.socketTimeout}") + private String socketTimeout; + + + + @Bean + public RestTemplate restTemplate() { + RestTemplate restTemplate = new RestTemplate(httpRequestFactory()); + List> converterList = restTemplate.getMessageConverters(); + + //重新设置StringHttpMessageConverter字符集为UTF-8,解决中文乱码问题 + HttpMessageConverter converterTarget = null; + for (HttpMessageConverter item : converterList) { + if (StringHttpMessageConverter.class == item.getClass()) { + converterTarget = item; + break; + } + } + if (null != converterTarget) { + converterList.remove(converterTarget); + } + + + converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); + + /** + * restTemplate 异常处理类 , 解决返回不了除了200之外的状态码 + */ + ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() { + @Override + public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException { + return true; + } + + @Override + public void handleError(ClientHttpResponse clientHttpResponse) throws IOException { + + } + }; + restTemplate.setErrorHandler(responseErrorHandler); + + return restTemplate; + } + + @Bean + public ClientHttpRequestFactory httpRequestFactory() { + return new HttpComponentsClientHttpRequestFactory(httpClient()); + } + + @Bean + public HttpClient httpClient() { + Registry registry = RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.getSocketFactory()) + .register("https", SSLConnectionSocketFactory.getSocketFactory()) + .build(); + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); + connectionManager.setMaxTotal(Integer.valueOf(this.maxTotal)); + connectionManager.setDefaultMaxPerRoute(Integer.valueOf(this.defaultMaxPerRoute)); + RequestConfig requestConfig = RequestConfig.custom() + //服务器返回数据(response)的时间,超过抛出read timeout + .setSocketTimeout(Integer.valueOf(this.socketTimeout)) + //连接上服务器(握手成功)的时间,超出抛出connect timeout + .setConnectTimeout(Integer.valueOf(this.connectTimeout)) + //从连接池中获取连接的超时时间,超时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool + .setConnectionRequestTimeout(Integer.valueOf(this.connectionRequestTimeout)) + .build(); + return HttpClientBuilder.create() + .setDefaultRequestConfig(requestConfig) + .setConnectionManager(connectionManager) + .setConnectionManagerShared(true) + .build(); + } + + + + +} diff --git a/src/main/java/com/license/controller/LicenseController.java b/src/main/java/com/license/controller/LicenseController.java new file mode 100644 index 0000000..01b3f71 --- /dev/null +++ b/src/main/java/com/license/controller/LicenseController.java @@ -0,0 +1,105 @@ +package com.license.controller; + +import cn.hutool.core.map.MapUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import cn.hutool.log.Log; + +import com.license.bean.LicenseInfo; +import com.license.cache.CacheManager; +import com.license.utils.Constant; +import com.license.utils.GetIpProtUtil; +import com.license.utils.HaspUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.*; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; + +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +/** + * @author fengjunfeng + * @date 2021/12/28 + * @time 16:00 + * @description + **/ +@RestController +public class LicenseController { + private static final Log log = Log.get(); + @Autowired + public RestTemplate restTemplate; + @Value("${bifang-api.server.port}") + private String port; + /** + * license锁信息交互 + * + * @return + */ + @PostMapping("/licenseOperation") + public Object licenseOperation(@RequestBody LicenseInfo licenseInfo) { +// if (!verify(licenseInfo.getServerId())){ +// return null; +// } + if (licenseInfo.getType().equals(Constant.LICENSE_C2V)){ + //获取C2V信息 + return HaspUtil.readC2V(licenseInfo.getVendorCode(),licenseInfo.getFeatureId()); + }else if (licenseInfo.getType().equals(Constant.LICENSE_UPDATE)){ + //上传license信息 + return HaspUtil.updateKeyWithLicense(licenseInfo.getV2C()); + }else if (licenseInfo.getType().equals(Constant.LICENSE_QUERY)){ + //获取license信息 + return HaspUtil.getLicenseInfo(licenseInfo.getVendorCode(),licenseInfo.getFeatureId()); + }else if (licenseInfo.getType().equals(Constant.LICENSE_VERIFY)){ + //验证license信息 + return HaspUtil.verify(licenseInfo.getVendorCode(),licenseInfo.getFeatureId()); + }else { + log.info("type error"); + return null; + } + } + + /** + * + * @author fengjunfeng + * @date 2022/1/20 + * @time 11:14 + * @description + * + **/ + public boolean verify(String serverId){ + try { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + HttpEntity httpEntity = new HttpEntity<>(null, headers); + String url = "http://" + GetIpProtUtil.getLocalIP() +":" + port + "/getServerId"; + ResponseEntity entity = restTemplate.exchange(url, HttpMethod.GET,httpEntity,Map.class); + log.info("license status: {}", JSONUtil.toJsonStr(entity)); + String str = MapUtil.get(entity.getBody(), "data", String.class); + if (str.equals(serverId)){ + return true; + } + }catch (SocketException | UnknownHostException e){ + log.error("获取ip出错"); + } + return false; + } + + + @PostMapping("/test") + public Object test() { + verify("1111"); + + if (CacheManager.getCache("test") == null){ + CacheManager.addCache("test","11111"); + }else { + System.out.println(CacheManager.getCache("test")); + } + return null; + } + +} diff --git a/src/main/java/com/sentinel/license/utils/Constant.java b/src/main/java/com/license/utils/Constant.java similarity index 92% rename from src/main/java/com/sentinel/license/utils/Constant.java rename to src/main/java/com/license/utils/Constant.java index 19948a7..a2fb297 100644 --- a/src/main/java/com/sentinel/license/utils/Constant.java +++ b/src/main/java/com/license/utils/Constant.java @@ -1,4 +1,4 @@ -package com.sentinel.license.utils; +package com.license.utils; /** * @author fengjunfeng diff --git a/src/main/java/com/license/utils/GetIpProtUtil.java b/src/main/java/com/license/utils/GetIpProtUtil.java new file mode 100644 index 0000000..7bfc472 --- /dev/null +++ b/src/main/java/com/license/utils/GetIpProtUtil.java @@ -0,0 +1,86 @@ +package com.license.utils; + +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Enumeration; + +public class GetIpProtUtil { + + /** + * 获取Linux下的IP地址 + * + * @return IP地址 + * @throws SocketException + */ + private static String getLinuxLocalIp() throws SocketException { + String ip = ""; + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { + NetworkInterface intf = en.nextElement(); + String name = intf.getName(); + if (!name.contains("docker") && !name.contains("lo")) { + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + String ipaddress = inetAddress.getHostAddress().toString(); + if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") + && !ipaddress.contains("fe80")) { + ip = ipaddress; + } + } + } + } + } + } catch (SocketException ex) { + ip = "127.0.0.1"; + ex.printStackTrace(); + } + return ip; + } + /** + * 判断操作系统是否是Windows + * + * @return + */ + public static boolean isWindowsOS() { + boolean isWindowsOS = false; + // 注:这里的system,系统指的是 JRE (runtime)system,不是指 OS + String osName = System.getProperty("os.name"); + if (osName.toLowerCase().indexOf("windows") > -1) { + isWindowsOS = true; + } + return isWindowsOS; + } + /** + * 获取本地IP地址 + * + * @throws SocketException + */ + public static String getLocalIP() throws UnknownHostException, SocketException { + if (isWindowsOS()) { + return InetAddress.getLocalHost().getHostAddress(); + } else { + return getLinuxLocalIp(); + } + } + + public static String getLocalPort() { + return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServerPort() + ""; + } + + public static String getIp() { + String ip = "127.0.0.1"; + return ip; + } + + +// public static void main(String[] args) throws MalformedObjectNameException, SocketException, UnknownHostException { +// System.out.println(getLocalIP()); +// } + +} diff --git a/src/main/java/com/sentinel/license/utils/HaspUtil.java b/src/main/java/com/license/utils/HaspUtil.java similarity index 90% rename from src/main/java/com/sentinel/license/utils/HaspUtil.java rename to src/main/java/com/license/utils/HaspUtil.java index f48c788..0cf8a38 100644 --- a/src/main/java/com/sentinel/license/utils/HaspUtil.java +++ b/src/main/java/com/license/utils/HaspUtil.java @@ -1,4 +1,4 @@ -package com.sentinel.license.utils; +package com.license.utils; import Aladdin.Hasp; import Aladdin.HaspStatus; import cn.hutool.core.util.XmlUtil; @@ -163,17 +163,19 @@ public class HaspUtil { // log.info("license status: {}", status); if(status == HaspStatus.HASP_STATUS_OK) { //如果已经安装过license ,读取指纹和 hasp id - String info = hasp.getSessionInfo(KEY_ID_FORMAT); - Document document = XmlUtil.parseXml(info); - Element rootEle = document.getDocumentElement(); - Element haspElement=XmlUtil.getElement(rootEle,"hasp"); - String id = haspElement.getAttribute("id"); - String licenseInfo = hasp.getInfo("\n" + - "\n" + - " \n" + - "",KEY_C2V_FORMAT,vendorCode); + String licenseInfo = hasp.getSessionInfo(KEY_C2V_FORMAT); + +// String info = hasp.getSessionInfo(KEY_ID_FORMAT); +// Document document = XmlUtil.parseXml(info); +// Element rootEle = document.getDocumentElement(); +// Element haspElement=XmlUtil.getElement(rootEle,"hasp"); +// String id = haspElement.getAttribute("id"); +// String licenseInfo = hasp.getInfo("\n" + +// "\n" + +// " \n" + +// "",KEY_C2V_FORMAT,vendorCode); status = hasp.getLastError(); log.info("getLicense c2v license status: {}", status); if (status != HaspStatus.HASP_STATUS_OK) { diff --git a/src/main/java/com/sentinel/license/controller/LicenseController.java b/src/main/java/com/sentinel/license/controller/LicenseController.java deleted file mode 100644 index 67638c6..0000000 --- a/src/main/java/com/sentinel/license/controller/LicenseController.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.sentinel.license.controller; - -import cn.hutool.log.Log; -import com.sentinel.license.bean.LicenseInfo; -import com.sentinel.license.utils.Constant; -import com.sentinel.license.utils.HaspUtil; -import org.springframework.web.bind.annotation.*; - -/** - * @author fengjunfeng - * @date 2021/12/28 - * @time 16:00 - * @description - **/ -@RestController -public class LicenseController { - private static final Log log = Log.get(); - - - /** - * license锁信息交互 - * - * @return - */ - @PostMapping("/licenseOperation") - public Object licenseOperation(@RequestBody LicenseInfo licenseInfo) { - if (licenseInfo.getType().equals(Constant.LICENSE_C2V)){ - //获取C2V信息 - return HaspUtil.readC2V(licenseInfo.getVendorCode(),licenseInfo.getFeatureId()); - }else if (licenseInfo.getType().equals(Constant.LICENSE_UPDATE)){ - //上传license信息 - return HaspUtil.updateKeyWithLicense(licenseInfo.getV2C()); - }else if (licenseInfo.getType().equals(Constant.LICENSE_QUERY)){ - //获取license信息 - return HaspUtil.getLicenseInfo(licenseInfo.getVendorCode(),licenseInfo.getFeatureId()); - }else if (licenseInfo.getType().equals(Constant.LICENSE_VERIFY)){ - //验证license信息 - return HaspUtil.verify(licenseInfo.getVendorCode(),licenseInfo.getFeatureId()); - }else { - log.info("type error"); - return null; - } - } - -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e68636a..11359b6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,3 +8,27 @@ server.tomcat.accept-count=1000 server.tomcat.min-spare-threads=100 #server.address=127.0.0.1 + +bifang-api.server.port=8080 +#设置连接超时时间,单位毫秒 +http.connectTimeout=120000 +#http clilent中从connetcion pool中获得一个connection的超时时间,单位毫秒 +http.connectionRequestTimeout=120000 +#请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用 +http.socketTimeout=120000 +http.maxTotal=20 +http.defaultMaxPerRoute=2 + + + +# 开启https,配置跟证书对应 +#server.ssl.enabled=true +#server.ssl.key-store=D:/tomcat.keystore +## server.ssl.key-store-type=JKS +#server.ssl.key-store-type=JKS +## 密码 +#server.ssl.key-store-password=tomcat +## springboot2.x不需要配置 +#server.ssl.key-password=tomcat +## 别名 +#server.ssl.key-alias=tomcat