This commit is contained in:
liulu 2024-12-18 11:00:39 +08:00
parent f19891c39d
commit 36a2066542
9 changed files with 80 additions and 41 deletions

View File

@ -16,7 +16,7 @@ public interface KeyInfoService {
KeyInfoDTO.KeyView selectById(Long id);
Long save(KeyInfoDTO.KeySave save);
List<Long> save(KeyInfoDTO.KeySave save);
void update(KeyInfoDTO.KeyUpdate update);

View File

@ -188,7 +188,7 @@ public class KeyInfoServiceImpl implements KeyInfoService {
}
@Override
public Long save(KeyInfoDTO.KeySave save) {
public List<Long> save(KeyInfoDTO.KeySave save) {
KeyTemplate keyTemplate = keyTemplateMapper.selectOne(
new LambdaQueryWrapper<KeyTemplate>()
@ -200,7 +200,7 @@ public class KeyInfoServiceImpl implements KeyInfoService {
Assert.isTrue(EnableStatus.ENABLED.getCode().equals(app.getStatus()), "应用不是启用状态");
LocalDateTime now = LocalDateTime.now();
List<Long> ids = new ArrayList<>();
for (int i = 0; i < save.getGenNumber(); i++) {
// 密钥信息
@ -229,9 +229,11 @@ public class KeyInfoServiceImpl implements KeyInfoService {
info.setCheckValue(record.getCheckValue());
keyInfoMapper.insert(info);
spKeyRecordMapper.insert(record);
ids.add(info.getId());
}
return 0L;
return ids;
}
@Override

View File

@ -1,6 +1,9 @@
package com.sunyard.chsm.param;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
@ -10,6 +13,9 @@ import javax.validation.constraints.NotNull;
* @since 2024/12/17
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class KeyCreateReq {
/**

View File

@ -57,8 +57,8 @@ public class KeyInfoController {
@PostMapping("/create")
@AuditControllerLog(description = "创建密钥", operateType = AuditLogConst.ADD)
public R<String> save(@Valid @RequestBody KeyInfoDTO.KeySave save) {
Long id = keyInfoService.save(save);
return R.data(String.valueOf(id));
List<Long> ids = keyInfoService.save(save);
return R.data(String.valueOf(ids.get(0)));
}
/**

View File

@ -168,8 +168,7 @@ public class SingleSdfApiService implements SdfApiService, InitializingBean {
public byte[] encryptByTMK(byte[] data) {
checkKey();
byte[] pad = PaddingUtil.PKCS7Padding(data);
sdfApiAdapter.symEncrypt(sessionHandle, tmkHandle, AlgId.SGD_SM4_CBC, new byte[8], pad);
return new byte[0];
return sdfApiAdapter.symEncrypt(sessionHandle, tmkHandle, AlgId.SGD_SM4_CBC, new byte[8], pad);
}
@Override

View File

@ -70,7 +70,7 @@ public class AuthHandler implements HandlerInterceptor, InitializingBean {
if (codeServiceMap == null || !codeServiceMap.containsKey(code)) {
log.warn("app: {}-{}, 无权访问: {} - {}", user.getAppId(), user.getName(), code, request.getRequestURI());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getOutputStream().write(JsonUtils.toJsonBytes(R.error("无权访问")));
response.getOutputStream().write(JsonUtils.toJsonBytes(R.error("无权访问: " + request.getRequestURI())));
return false;
}
request.setAttribute("used_service_ids", codeServiceMap.get(code));

View File

@ -77,7 +77,8 @@ public class KeyManageService {
save.setApplicationId(UserContext.getCurrentAppId());
save.setKeyTemplateCode(req.getKeyTemplateCode());
save.setGenNumber(req.getGenNumber());
return keyInfoService.save(save);
List<Long> ids = keyInfoService.save(save);
return ids.get(0);
}

View File

@ -1,10 +1,13 @@
package api;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.param.AppTokenReq;
import com.sunyard.chsm.param.AppTokenResp;
import com.sunyard.chsm.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
@ -12,13 +15,16 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.Optional;
/**
* @author liulu
* @since 2024/12/17
*/
@Slf4j
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";
@ -58,10 +64,21 @@ public abstract class BaseTest {
.body(JsonUtils.toJsonBytes(req));
byte[] res = restTemplate.postForObject(url, JsonUtils.toJsonBytes(req), byte[].class);
R<T> r = JsonUtils.objectMapper()
.readValue(res, new TypeReference<R<T>>() {
});
return r.getResult();
JsonNode jsonNode = JsonUtils.objectMapper()
.readTree(res);
boolean success = jsonNode.get("success").asBoolean();
String message = Optional.ofNullable(jsonNode.get("message")).map(JsonNode::asText).orElse("");
if (!success) {
log.warn(message);
return null;
}
JsonNode result = jsonNode.get("result");
if (result == null) {
return null;
}
JsonParser returnJsonParser = JsonUtils.objectMapper().treeAsTokens(result);
return JsonUtils.objectMapper().readValue(returnJsonParser, tClass);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -1,19 +1,16 @@
package api;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.enums.KeyAlg;
import com.sunyard.chsm.enums.KeyStatus;
import com.sunyard.chsm.model.entity.KeyInfo;
import com.sunyard.chsm.param.KeyInfoQuery;
import com.sunyard.chsm.param.KeyInfoResp;
import com.sunyard.chsm.utils.JsonUtils;
import com.sunyard.chsm.param.KeyCreateReq;
import com.sunyard.chsm.param.KeyManageReq;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.time.LocalDateTime;
import java.util.Collections;
/**
* @author liulu
@ -22,26 +19,43 @@ import java.util.List;
@Slf4j
public class KeyManageTest extends BaseTest {
private static Long keyId;
@BeforeAll
public static void before() throws Exception {
KeyInfoQuery query = new KeyInfoQuery();
byte[] res = restTemplate.postForObject("/key/pageList", JsonUtils.toJsonBytes(query), byte[].class);
R<Page<KeyInfoResp>> r = JsonUtils.objectMapper()
.readValue(res, new TypeReference<R<Page<KeyInfoResp>>>() {
});
Assertions.assertTrue(r.isSuccess());
List<KeyInfoResp> records = r.getResult().getRecords();
Assertions.assertFalse(CollectionUtils.isEmpty(records));
keyId = records.iterator().next().getKeyId();
}
@Test
public void keyInfoTest(){
KeyInfo res = execute("/key/info", keyId, KeyInfo.class);
log.info("keyInfoTest: {}", JsonUtils.toJsonString(res));
public void keyLifeTest() {
Long sm4KeyId = execute("/key/gen", KeyCreateReq.builder().keyTemplateCode(keyTemplate).genNumber(1).build(), Long.class);
Assertions.assertTrue(sm4KeyId > 0);
//
KeyInfo keyInfo = execute("/key/info", sm4KeyId, KeyInfo.class);
Assertions.assertEquals(KeyAlg.SM4.getCode(), keyInfo.getKeyAlg());
Assertions.assertEquals(KeyStatus.ENABLED.getCode(), keyInfo.getStatus());
LocalDateTime now = LocalDateTime.now();
Assertions.assertTrue(now.isAfter(keyInfo.getEffectiveTime()) && now.isBefore(keyInfo.getExpiredTime()));
//
KeyManageReq keyManageReq = new KeyManageReq();
keyManageReq.setIds(Collections.singletonList(sm4KeyId));
execute("/key/disable", keyManageReq, Void.class);
keyInfo = execute("/key/info", sm4KeyId, KeyInfo.class);
Assertions.assertEquals(KeyStatus.DISABLED.getCode(), keyInfo.getStatus());
//
execute("/key/enable", keyManageReq, Void.class);
keyInfo = execute("/key/info", sm4KeyId, KeyInfo.class);
Assertions.assertEquals(KeyStatus.ENABLED.getCode(), keyInfo.getStatus());
execute("/key/disable", keyManageReq, Void.class);
keyInfo = execute("/key/info", sm4KeyId, KeyInfo.class);
Assertions.assertEquals(KeyStatus.DISABLED.getCode(), keyInfo.getStatus());
execute("/key/archive", keyManageReq, Void.class);
keyInfo = execute("/key/info", sm4KeyId, KeyInfo.class);
Assertions.assertEquals(KeyStatus.ARCHIVED.getCode(), keyInfo.getStatus());
execute("/key/destroy", keyManageReq, Void.class);
keyInfo = execute("/key/info", sm4KeyId, KeyInfo.class);
Assertions.assertNull(keyInfo);
}