diff --git a/chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java b/chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java new file mode 100644 index 0000000..7604932 --- /dev/null +++ b/chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java @@ -0,0 +1,14 @@ +package com.sunyard.chsm.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.sunyard.chsm.model.entity.KeyInfo; +import com.sunyard.chsm.model.entity.KeyRecord; +import org.apache.ibatis.annotations.Mapper; + +/** + * @author liulu + * @since 2024/10/22 + */ +@Mapper +public interface KeyRecordMapper extends BaseMapper { +} diff --git a/chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java b/chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java new file mode 100644 index 0000000..bc1f146 --- /dev/null +++ b/chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java @@ -0,0 +1,15 @@ +package com.sunyard.chsm.service; + +import com.sunyard.chsm.model.entity.KeyRecord; + +public interface KeyRecordService { + KeyRecord selectById(Long id); + + + /** + * 根据 keyId 查找密钥 + * @param keyId keyId + * @return 可用值或 null + */ + KeyRecord selectByKeyId(Long keyId); +} diff --git a/chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java b/chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java new file mode 100644 index 0000000..7a32a8a --- /dev/null +++ b/chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java @@ -0,0 +1,45 @@ +package com.sunyard.chsm.service.impl; + + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.sunyard.chsm.mapper.KeyRecordMapper; +import com.sunyard.chsm.model.entity.KeyRecord; +import com.sunyard.chsm.service.KeyRecordService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.time.LocalDateTime; + +/** + * @author Cheney + */ +@Slf4j +@Service +@Transactional +public class KeyRecordServiceImpl implements KeyRecordService { + + @Resource + private KeyRecordMapper keyRecordMapper; + + @Override + public KeyRecord selectById(Long id) { + return keyRecordMapper.selectById(id); + } + + @Override + public KeyRecord selectByKeyId(Long keyId) { + LocalDateTime now = LocalDateTime.now(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + // 添加 keyId 的条件 + queryWrapper.eq(KeyRecord::getKeyId, keyId); + // 添加 effectiveTime 小于当前时间的条件 + queryWrapper.lt(KeyRecord::getEffectiveTime, now); + // 添加 expiredTime 大于当前时间的条件 + queryWrapper.gt(KeyRecord::getExpiredTime, now); + + return keyRecordMapper.selectOne(queryWrapper); + } +} diff --git a/chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java b/chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java new file mode 100644 index 0000000..ca5ca23 --- /dev/null +++ b/chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java @@ -0,0 +1,28 @@ +package com.sunyard.chsm.service; + +import com.sunyard.chsm.WebServerApp; +import com.sunyard.chsm.model.dto.KeyInfoDTO; +import com.sunyard.chsm.model.entity.KeyRecord; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@Slf4j +@SpringBootTest(classes = WebServerApp.class) +public class SYMEncryptControllerTest { + + private static final long TEST_APP_ID = 1852232967292882946L; + private static final long TEST_KEY_ID = 1852232967292882945L; + + @Autowired + private KeyRecordService keyRecordService; + + + @Test + void testSelectKey() { + KeyRecord key = keyRecordService.selectByKeyId(TEST_KEY_ID); + System.out.println(key); + } + +}