This commit is contained in:
liulu
2024-12-27 14:57:18 +08:00
parent b6fc524b3f
commit 265d81a7f0
8 changed files with 143 additions and 63 deletions
@@ -11,6 +11,7 @@ import org.apache.commons.pool2.impl.GenericObjectPool;
@Data
public class DeviceContext {
private Long id;
private String ip;
private Integer port;
private String manufacturer;
@@ -27,12 +27,8 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -49,7 +45,9 @@ public class DeviceManager implements InitializingBean {
private static final Map<Long, AtomicInteger> ROUND_MAP = new HashMap<>();
private Map<Long, List<DeviceContext>> deviceMap = new HashMap<>();
// private Map<Long, List<DeviceContext>> deviceMap = new HashMap<>();
private Map<Long, List<Long>> serviceDeviceMap;
private final Map<Long, DeviceContext> usedDeviceMap = new ConcurrentHashMap<>();
private boolean enableSoftDevice = false;
private TMKContext softContext;
@@ -79,14 +77,20 @@ public class DeviceManager implements InitializingBean {
if (atomicInteger.get() > Integer.MAX_VALUE - 10000) {
atomicInteger.set(1);
}
List<DeviceContext> contexts = new ArrayList<>();
for (Long serviceId : serviceIds) {
Optional.ofNullable(deviceMap.get(serviceId))
.ifPresent(contexts::addAll);
}
List<DeviceContext> contexts = serviceDeviceMap.entrySet().stream()
.filter(it -> serviceIds.contains(it.getKey()))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.map(usedDeviceMap::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
DeviceContext device = getNextDevice(contexts, atomicInteger.getAndIncrement());
TMKContext tmkContext;
if (device == null) {
if (log.isDebugEnabled()) {
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
log.debug("request: {}, use soft device 1", request.getRequestURI());
}
tmkContext = getSoftContext();
} else {
if (log.isDebugEnabled()) {
@@ -98,6 +102,11 @@ public class DeviceManager implements InitializingBean {
tmkContext = pool.borrowObject(2000);
tmkContext.setPool(pool);
} catch (Exception e) {
usedDeviceMap.remove(device.getId());
if (log.isDebugEnabled()) {
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
log.debug("request: {}, use soft device 2", request.getRequestURI());
}
tmkContext = getSoftContext();
}
}
@@ -146,7 +155,6 @@ public class DeviceManager implements InitializingBean {
return null; // 理论上不会到这里
}
private void syncDevice() {
log.debug(">>>>>>>>>>>>>>> start sync device <<<<<<<<<<<<<<<");
enableSoftDevice = tmkService.isEnableSoftDevice();
@@ -158,60 +166,52 @@ public class DeviceManager implements InitializingBean {
);
if (CollectionUtils.isEmpty(devices)) {
log.info("no device for sync ...");
deviceMap.clear();
usedDeviceMap.clear();
return;
}
Map<Long, List<Device>> groupDeviceMap = devices.stream().collect(Collectors.groupingBy(Device::getGroupId));
Map<Long, List<Long>> groupDeviceMap = devices.stream()
.collect(Collectors.groupingBy(Device::getGroupId, Collectors.mapping(Device::getId, Collectors.toList())));
List<CryptoServiceDeviceGroup> serviceDeviceGroups = cryptoServiceDeviceGroupMapper
.selectList(new LambdaQueryWrapper<CryptoServiceDeviceGroup>()
.in(CryptoServiceDeviceGroup::getDeviceGroupId, groupDeviceMap.keySet()));
if (CollectionUtils.isEmpty(serviceDeviceGroups)) {
deviceMap.clear();
usedDeviceMap.clear();
return;
}
Map<Long, List<Device>> waitSyncMap = serviceDeviceGroups.stream()
serviceDeviceMap = serviceDeviceGroups.stream()
.collect(Collectors.toMap(CryptoServiceDeviceGroup::getServiceId,
it -> groupDeviceMap.get(it.getDeviceGroupId())));
for (Map.Entry<Long, List<Device>> entry : waitSyncMap.entrySet()) {
deviceMap.compute(entry.getKey(), (k, old) -> {
if (CollectionUtils.isEmpty(old)) {
return entry.getValue().stream().map(this::mapToContext).filter(Objects::nonNull).collect(Collectors.toList());
for (Device device : devices) {
DeviceCheckRes checkRes = tmkService.checkDevice(device);
if (checkRes.isHasError()) {
log.warn("设备:{}, 检测异常:{}", device.getServiceIp(), checkRes.getMessage());
usedDeviceMap.remove(device.getId());
continue;
}
DeviceContext exist = usedDeviceMap.get(device.getId());
if (Objects.isNull(exist)) {
usedDeviceMap.remove(device.getId());
DeviceContext context = getDeviceContext(device, checkRes);
if (context != null) {
usedDeviceMap.put(device.getId(), context);
}
List<String> newSerials = entry.getValue().stream()
.map(Device::getDeviceSerial)
.collect(Collectors.toList());
List<String> oldSerials = old.stream()
.map(DeviceContext::getDeviceSerial)
.collect(Collectors.toList());
List<DeviceContext> nc = old.stream()
.filter(it -> newSerials.contains(it.getDeviceSerial()))
.collect(Collectors.toList());
List<Device> waitSync = entry.getValue().stream()
.filter(it -> !oldSerials.contains(it.getDeviceSerial()))
.collect(Collectors.toList());
nc.addAll(waitSync.stream().map(this::mapToContext).filter(Objects::nonNull).collect(Collectors.toList()));
return nc;
});
} else {
if (!Objects.equals(checkRes.getDeviceSerial(), exist.getDeviceSerial())
|| !Objects.equals(checkRes.getPubKey(), exist.getPubKey())) {
DeviceContext context = getDeviceContext(device, checkRes);
if (context != null) {
usedDeviceMap.put(device.getId(), context);
}
}
}
}
}
private DeviceContext mapToContext(Device device) {
private static DeviceContext getDeviceContext(Device device, DeviceCheckRes checkRes) {
try {
Assert.hasText(device.getEncTmk(), "TMK 状态异常");
DeviceCheckRes checkRes = tmkService.checkDevice(device);
if (!Objects.equals(checkRes.getDeviceSerial(), device.getDeviceSerial())
|| !Objects.equals(checkRes.getPubKey(), device.getPubKey())) {
return null;
}
DeviceContext context = new DeviceContext();
context.setId(device.getId());
context.setIp(device.getServiceIp());
context.setPort(device.getServicePort());
context.setModel(device.getManufacturerModel());
@@ -234,7 +234,6 @@ public class DeviceManager implements InitializingBean {
TMKContextFactory tenantTMKContextFactory = new TMKContextFactory(checkRes.getSdfApiAdapter(), context);
GenericObjectPool<TMKContext> pool = new GenericObjectPool<>(tenantTMKContextFactory, config);
context.setPool(pool);
return context;
} catch (Exception ex) {
log.warn("device conn error: {}", device, ex);
@@ -435,7 +435,8 @@ public class AsymKeyService {
signedAndEnvelopedData.getSignerInfos()
);
ContentInfo contentInfo = new ContentInfo(Signed_Data, sd);
p7Verify(contentInfo.getEncoded(), null);
boolean verify = p7Verify(contentInfo.getEncoded(), null);
Assert.isTrue(verify, "签名验证失败");
AsymEnvelopeUnsealResp resp = new AsymEnvelopeUnsealResp();
resp.setPlainData(CodecUtils.encodeBase64(plainData));
return resp;
@@ -29,7 +29,7 @@ public abstract class BaseTest {
protected static final String keyTemplate = "sym-sm4-001";
protected static final String ak = "216205d408130d83d13c5072305b8b65";
protected static final String sk = "ae64515d1d5adec2cc6ae8726d0c1bbc";
protected static final String server = "http://127.0.0.1:8900";
protected static final String server = "http://172.16.18.46:8900";
protected static final RestTemplate restTemplate;
protected static final String token;