From b1ee66d66f67f272d25d290d8b1116c5f4c8f089 Mon Sep 17 00:00:00 2001 From: wxz Date: Fri, 25 Jun 2021 15:34:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=B7=A8=E5=9F=9Fcu?= =?UTF-8?q?rd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/epmet/dto/form/CorsConfigFormDTO.java | 37 ++++++++++ .../epmet/dto/result/CorsConfigResultDTO.java | 7 ++ .../controller/CorsConfigController.java | 44 ++++++++++- .../com/epmet/entity/CorsConfigEntity.java | 5 ++ .../com/epmet/service/CorsConfigService.java | 6 ++ .../service/impl/CorsConfigServiceImpl.java | 74 +++++++++++++++++++ .../main/resources/mapper/CorsConfigDao.xml | 1 + .../tools/exception/EpmetErrorCode.java | 1 + 8 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/form/CorsConfigFormDTO.java diff --git a/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/form/CorsConfigFormDTO.java b/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/form/CorsConfigFormDTO.java new file mode 100644 index 0000000000..18668f87fe --- /dev/null +++ b/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/form/CorsConfigFormDTO.java @@ -0,0 +1,37 @@ +package com.epmet.dto.form; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CorsConfigFormDTO { + + public interface Add {} + public interface Update {} + + @NotBlank(message = "ID不能为空", groups = { Update.class }) + private String id; + + /** + * 首部type + */ + @NotBlank(message = "首部类型不能为空", groups = { Add.class, Update.class }) + private String headerType; + + /** + * 首部value + */ + @NotBlank(message = "首部值不能为空", groups = { Add.class, Update.class }) + private String headerValue; + + /** + * 备注 + */ + private String comment; + +} diff --git a/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/result/CorsConfigResultDTO.java b/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/result/CorsConfigResultDTO.java index 3faa333d74..828e2998ae 100644 --- a/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/result/CorsConfigResultDTO.java +++ b/epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/result/CorsConfigResultDTO.java @@ -8,6 +8,8 @@ import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor public class CorsConfigResultDTO { + + private String id; /** * 首部类型 */ @@ -17,4 +19,9 @@ public class CorsConfigResultDTO { * 首部值 */ private String headerValue; + + /** + * 首部备注 + */ + private String comment; } diff --git a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/controller/CorsConfigController.java b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/controller/CorsConfigController.java index e2972f93d6..6ccf102354 100644 --- a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/controller/CorsConfigController.java +++ b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/controller/CorsConfigController.java @@ -1,12 +1,12 @@ package com.epmet.controller; import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.dto.form.CorsConfigFormDTO; import com.epmet.dto.result.CorsConfigResultDTO; import com.epmet.service.CorsConfigService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -29,4 +29,42 @@ public class CorsConfigController { return new Result>().ok(list); } + /** + * @Description 添加跨域配置 + * @return + * @author wxz + * @date 2021.06.25 13:51 + */ + @PostMapping("/add") + public Result addConfig(@RequestBody CorsConfigFormDTO input) { + ValidatorUtils.validateEntity(input, CorsConfigFormDTO.Add.class); + CorsConfigResultDTO data = corsConfigService.addConfig(input.getHeaderType(), input.getHeaderValue(), input.getComment()); + return new Result().ok(data); + } + + /** + * @Description 通过id删除配置 + * @return + * @author wxz + * @date 2021.06.25 14:18 + */ + @PostMapping("/delete/{id}") + public Result deleteConfigById(@PathVariable("id") String configId) { + corsConfigService.deleteById(configId); + return new Result<>(); + } + + /** + * @Description 根据id更新 + * @return + * @author wxz + * @date 2021.06.25 14:23 + */ + @PostMapping("/update") + public Result updateById(@RequestBody CorsConfigFormDTO input) { + ValidatorUtils.validateEntity(input, CorsConfigFormDTO.Update.class); + corsConfigService.updateById(input.getId(), input.getHeaderType(), input.getHeaderValue(), input.getComment()); + return new Result<>(); + } + } diff --git a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/entity/CorsConfigEntity.java b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/entity/CorsConfigEntity.java index 75f7258e76..0a717f1c50 100644 --- a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/entity/CorsConfigEntity.java +++ b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/entity/CorsConfigEntity.java @@ -48,4 +48,9 @@ public class CorsConfigEntity extends BaseEpmetEntity { */ private String headerValue; + /** + * 说明 + */ + private String comment; + } diff --git a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/CorsConfigService.java b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/CorsConfigService.java index 8bae0f9531..93a6b358a3 100644 --- a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/CorsConfigService.java +++ b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/CorsConfigService.java @@ -11,4 +11,10 @@ import java.util.List; */ public interface CorsConfigService { List list(); + + CorsConfigResultDTO addConfig(String headerType, String headerValue, String comment); + + void deleteById(String configId); + + void updateById(String configId, String headerType, String headerValue, String comment); } diff --git a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/impl/CorsConfigServiceImpl.java b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/impl/CorsConfigServiceImpl.java index aed116d421..2441882ea3 100644 --- a/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/impl/CorsConfigServiceImpl.java +++ b/epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/impl/CorsConfigServiceImpl.java @@ -1,12 +1,20 @@ package com.epmet.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.exception.ExceptionUtils; +import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.dao.CorsConfigDao; import com.epmet.dto.result.CorsConfigResultDTO; import com.epmet.entity.CorsConfigEntity; import com.epmet.service.CorsConfigService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.List; @@ -22,6 +30,11 @@ public class CorsConfigServiceImpl implements CorsConfigService { @Autowired private CorsConfigDao corsConfigDao; + @Autowired + private RedisTemplate redisTemplate; + + private Logger logger = LoggerFactory.getLogger(getClass()); + /** * @Description 查询配置列表 * @return @@ -33,4 +46,65 @@ public class CorsConfigServiceImpl implements CorsConfigService { LambdaQueryWrapper conditions = new LambdaQueryWrapper<>(); return ConvertUtils.sourceToTarget(corsConfigDao.selectList(conditions), CorsConfigResultDTO.class); } + + @Override + public CorsConfigResultDTO addConfig(String headerType, String headerValue, String comment) { + LambdaQueryWrapper w = new LambdaQueryWrapper<>(); + w.eq(CorsConfigEntity::getHeaderType, headerType.trim()); + w.eq(CorsConfigEntity::getHeaderValue, headerValue.trim()); + if (corsConfigDao.selectCount(w) > 0) { + throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), "已存在该跨域配置,请勿重复添加"); + } + + CorsConfigEntity insert = new CorsConfigEntity(); + insert.setHeaderType(headerType); + insert.setHeaderValue(headerValue); + insert.setComment(comment); + if (corsConfigDao.insert(insert) == 0) { + throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), "插入跨域配置出错"); + } + + deleteFromRedis(); + return new CorsConfigResultDTO(insert.getId(), headerType, headerValue, comment); + } + + @Override + public void deleteById(String configId) { + if (corsConfigDao.deleteById(configId) == 0) { + String msg = "删除出错,不存在该项数据"; + throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), msg, msg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL); + } + deleteFromRedis(); + } + + @Override + public void updateById(String configId, String headerType, String headerValue, String comment) { + CorsConfigEntity exists = corsConfigDao.selectById(configId); + if (exists == null) { + String msg = "该项数据不存在"; + throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), msg, msg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL); + } + exists.setHeaderType(headerType); + exists.setHeaderValue(headerValue); + exists.setComment(comment); + if (corsConfigDao.updateById(exists) == 0) { + throw new RenException(EpmetErrorCode.OPER_CORS_CONFIG_ERROR.getCode(), "更新出错,影响条数为0"); + } + deleteFromRedis(); + } + + /** + * @Description 从缓存中删除 + * @return + * @author wxz + * @date 2021.06.25 14:26 + */ + private void deleteFromRedis() { + try { + redisTemplate.delete(RedisKeys.getCorsConfigKey()); + } catch (Exception e) { + String errorMsg = ExceptionUtils.getErrorStackTrace(e); + logger.error(String.format("从Redis中删除Cors跨域信息失败,程序继续执行。错误信息:%s", errorMsg)); + } + } } diff --git a/epmet-admin/epmet-admin-server/src/main/resources/mapper/CorsConfigDao.xml b/epmet-admin/epmet-admin-server/src/main/resources/mapper/CorsConfigDao.xml index ce43effd0a..36201beb73 100644 --- a/epmet-admin/epmet-admin-server/src/main/resources/mapper/CorsConfigDao.xml +++ b/epmet-admin/epmet-admin-server/src/main/resources/mapper/CorsConfigDao.xml @@ -7,6 +7,7 @@ + diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java index 86edad6f0b..2538492bb8 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java @@ -128,6 +128,7 @@ public enum EpmetErrorCode { USER_LIST_ROLES_BY_KEY_FAIL(8715, "根据角色key查询角色列表失败"), OPER_UPLOAD_IMG_TYPE_ERROR(8716, "请上传PNG格式的图片"), OPER_UPLOAD_IMG_SIZE_ERROR(8717, "请上传200*200的图片"), + OPER_CORS_CONFIG_ERROR(8718, "跨域配置错误"), // 党建声音 前端提示 88段 DRAFT_CONTENT_IS_NULL(8801, "至少需要添加一个段落"),