This commit is contained in:
六如
2024-10-11 09:59:52 +08:00
parent cff2ee120b
commit 35b3e6489d
13 changed files with 307 additions and 55 deletions

View File

@@ -0,0 +1,41 @@
package com.gitee.sop.adminbackend.common.jackson;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Locale;
import java.util.TimeZone;
/**
* 日期序列化配置类
*/
@Data
@ConfigurationProperties(prefix = "jackson")
public class DateConverterProperties {
/**
* Date/LocalDateTime 格式化
*/
private String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
/**
* localDate 格式化
*/
private String localDateFormat = "yyyy-MM-dd";
/**
* localTime 格式化
*/
private String localTimeFormat = "HH:mm:ss";
/**
* 时区, 默认 GMT+8
*/
private TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
/**
* 地区, 默认 zh_CN
*/
private Locale locale = Locale.CHINA;
}

View File

@@ -0,0 +1,147 @@
package com.gitee.sop.adminbackend.common.jackson;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* 基于Jackson的日期格式配置
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(DateConverterProperties.class)
public class JacksonDateConverterConfig {
@Autowired
private DateConverterProperties props;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
log.info("@================= 初始化jackson日期序列化 ==================@");
log.info("Locale : 【{}】", props.getLocale());
log.info("TimeZone : 【{}】", props.getTimeZone().getDisplayName());
log.info("Date : 【{}】", props.getDateTimeFormat());
log.info("LocalDateTime : 【{}】", props.getDateTimeFormat());
log.info("LocalDate : 【{}】", props.getLocalDateFormat());
log.info("LocalTime : 【{}】", props.getLocalTimeFormat());
final DateTimeFormatter dateTimeFormatter = this.ofPattern(props.getDateTimeFormat());
final DateTimeFormatter localDateFormatter = this.ofPattern(props.getLocalDateFormat());
final DateTimeFormatter localTimeFormatter = this.ofPattern(props.getLocalTimeFormat());
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(props.getDateTimeFormat());
return builder -> {
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
// 地区
.locale(props.getLocale())
// 时区
.timeZone(props.getTimeZone());
// 序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter))
.serializerByType(LocalDate.class, new LocalDateSerializer(localDateFormatter))
.serializerByType(LocalTime.class, new LocalTimeSerializer(localTimeFormatter))
.serializerByType(Date.class, new DateSerializer(false, simpleDateFormat));
// 反序列化
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(localDateFormatter))
.deserializerByType(LocalTime.class, new LocalTimeDeserializer(localTimeFormatter))
.deserializerByType(Date.class,
new DateDeserializers.DateDeserializer(DateDeserializers.DateDeserializer.instance,
simpleDateFormat, props.getDateTimeFormat()));
};
}
/**
* 根据字符串 获取 DateTimeFormatter
*
* @param pattern 日期格式字符串
* @return DateTimeFormatter
*/
private DateTimeFormatter ofPattern(String pattern) {
return DateTimeFormatter.ofPattern(pattern);
}
/**
* LocalDate转换器用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern(props.getLocalDateFormat()));
}
};
}
/**
* LocalDateTime转换器用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(props.getDateTimeFormat()));
}
};
}
/**
* LocalTime转换器用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
return LocalTime.parse(source, DateTimeFormatter.ofPattern(props.getLocalTimeFormat()));
}
};
}
/**
* Date转换器用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(props.getDateTimeFormat());
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
log.error(">>>> init Converter String to Date error!", e);
throw new RuntimeException(e);
}
}
};
}
}

View File

@@ -66,12 +66,12 @@ public class IsvInfoController {
/**
* 获取秘钥信息
*
* @param isvInfoId
* @param isvId
* @return
*/
@GetMapping("/getKeys")
public Result<IsvKeysDTO> getKeys(Long isvInfoId) {
IsvKeysDTO isvKeysDTO = isvInfoService.getKeys(isvInfoId);
public Result<IsvKeysDTO> getKeys(Long isvId) {
IsvKeysDTO isvKeysDTO = isvInfoService.getKeys(isvId);
return Result.ok(isvKeysDTO);
}

View File

@@ -3,6 +3,8 @@ package com.gitee.sop.adminbackend.controller.isv.req;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@@ -12,6 +14,13 @@ import javax.validation.constraints.NotNull;
@Data
public class IsvInfoAddParam {
/**
* 秘钥格式1PKCS8(JAVA适用)2PKCS1(非JAVA适用)
*/
@Min(value = 1, message = "秘钥格式错误")
@Max(value = 2, message = "秘钥格式错误")
private Integer keyFormat;
/**
* 1启用2禁用
*/

View File

@@ -23,7 +23,7 @@ public class IsvKeys {
/**
* isv_info.id
*/
private Long isvInfoId;
private Long isvId;
/**
* 秘钥格式1PKCS8(JAVA适用)2PKCS1(非JAVA适用)

View File

@@ -24,7 +24,7 @@ public class PermIsvRole {
/**
* isv_info表id
*/
private Long isvInfoId;
private Long isvId;
/**
* 角色code

View File

@@ -8,8 +8,8 @@ import com.gitee.sop.adminbackend.dao.entity.IsvKeys;
*/
public interface IsvKeysMapper extends BaseMapper<IsvKeys> {
default IsvKeys getByAppId(String appId) {
return this.get(IsvKeys::getAppId, appId);
default IsvKeys getByIsvInfoId(Long isvId) {
return this.get(IsvKeys::getIsvId, isvId);
}
}

View File

@@ -43,6 +43,12 @@ public class IsvInfoService implements LambdaService<IsvInfo, IsvInfoMapper> {
return rsaTool.createKeys();
}
/**
* 添加ISV
* @param isvInfoAddDTO
* @return
* @throws Exception
*/
@Transactional(rollbackFor = Exception.class)
public long add(IsvInfoAddDTO isvInfoAddDTO) throws Exception {
IsvInfo rec = CopyUtil.copyBean(isvInfoAddDTO, IsvInfo::new);
@@ -53,25 +59,27 @@ public class IsvInfoService implements LambdaService<IsvInfo, IsvInfoMapper> {
if (CollectionUtils.isNotEmpty(isvInfoAddDTO.getRoleCodes())) {
this.saveIsvRole(rec, isvInfoAddDTO.getRoleCodes());
}
IsvKeysGenDTO isvKeysGenVO = this.createIsvKeys();
// 生成秘钥
RSATool.KeyFormat keyFormat = RSATool.KeyFormat.of(isvInfoAddDTO.getKeyFormat());
IsvKeysGenDTO isvKeysGenVO = this.createIsvKeys(keyFormat);
IsvKeys isvKeys = new IsvKeys();
isvKeys.setIsvInfoId(rec.getId());
isvKeys.setIsvId(rec.getId());
CopyUtil.copyPropertiesIgnoreNull(isvKeysGenVO, isvKeys);
isvKeysService.save(isvKeys);
return rec.getId();
}
private void saveIsvRole(IsvInfo isvInfo, List<String> roleCodeList) {
long isvInfoId = isvInfo.getId();
Long isvId = isvInfo.getId();
permIsvRoleService.query()
.eq(PermIsvRole::getIsvInfoId, isvInfoId)
.eq(PermIsvRole::getIsvId, isvId)
.delete();
List<PermIsvRole> tobeSaveList = roleCodeList.stream()
.map(roleCode -> {
PermIsvRole rec = new PermIsvRole();
rec.setIsvInfoId(isvInfoId);
rec.setIsvId(isvId);
rec.setRoleCode(roleCode);
return rec;
})
@@ -88,10 +96,10 @@ public class IsvInfoService implements LambdaService<IsvInfo, IsvInfoMapper> {
// }
}
private IsvKeysGenDTO createIsvKeys() throws Exception {
private IsvKeysGenDTO createIsvKeys(RSATool.KeyFormat keyFormat) throws Exception {
IsvKeysGenDTO isvKeysGenDTO = new IsvKeysGenDTO();
RSATool.KeyStore keyStoreIsv = this.createKeys(RSATool.KeyFormat.PKCS8);
RSATool.KeyStore keyStoreIsv = this.createKeys(keyFormat);
isvKeysGenDTO.setPublicKeyIsv(keyStoreIsv.getPublicKey());
isvKeysGenDTO.setPrivateKeyIsv(keyStoreIsv.getPrivateKey());
@@ -100,8 +108,8 @@ public class IsvInfoService implements LambdaService<IsvInfo, IsvInfoMapper> {
return isvKeysGenDTO;
}
public IsvKeysDTO getKeys(Long isvInfoId) {
IsvKeys isvKeys = isvKeysService.get(IsvKeys::getIsvInfoId, isvInfoId);
public IsvKeysDTO getKeys(Long isvId) {
IsvKeys isvKeys = isvKeysService.get(IsvKeys::getIsvId, isvId);
return CopyUtil.copyBean(isvKeys, IsvKeysDTO::new);
}

View File

@@ -3,6 +3,8 @@ package com.gitee.sop.adminbackend.service.isv.dto;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
@@ -13,6 +15,13 @@ import java.util.List;
@Data
public class IsvInfoAddDTO {
/**
* 秘钥格式1PKCS8(JAVA适用)2PKCS1(非JAVA适用)
*/
@Min(value = 1, message = "秘钥格式错误")
@Max(value = 2, message = "秘钥格式错误")
private Integer keyFormat;
/**
* 1启用2禁用
*/