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; } }