Browse Source

新增:跨域curd

dev_shibei_match
wxz 4 years ago
parent
commit
b1ee66d66f
  1. 37
      epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/form/CorsConfigFormDTO.java
  2. 7
      epmet-admin/epmet-admin-client/src/main/java/com/epmet/dto/result/CorsConfigResultDTO.java
  3. 44
      epmet-admin/epmet-admin-server/src/main/java/com/epmet/controller/CorsConfigController.java
  4. 5
      epmet-admin/epmet-admin-server/src/main/java/com/epmet/entity/CorsConfigEntity.java
  5. 6
      epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/CorsConfigService.java
  6. 74
      epmet-admin/epmet-admin-server/src/main/java/com/epmet/service/impl/CorsConfigServiceImpl.java
  7. 1
      epmet-admin/epmet-admin-server/src/main/resources/mapper/CorsConfigDao.xml
  8. 1
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java

37
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;
}

7
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;
}

44
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<List<CorsConfigResultDTO>>().ok(list);
}
/**
* @Description 添加跨域配置
* @return
* @author wxz
* @date 2021.06.25 13:51
*/
@PostMapping("/add")
public Result<CorsConfigResultDTO> addConfig(@RequestBody CorsConfigFormDTO input) {
ValidatorUtils.validateEntity(input, CorsConfigFormDTO.Add.class);
CorsConfigResultDTO data = corsConfigService.addConfig(input.getHeaderType(), input.getHeaderValue(), input.getComment());
return new Result<CorsConfigResultDTO>().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<>();
}
}

5
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;
}

6
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<CorsConfigResultDTO> list();
CorsConfigResultDTO addConfig(String headerType, String headerValue, String comment);
void deleteById(String configId);
void updateById(String configId, String headerType, String headerValue, String comment);
}

74
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<CorsConfigEntity> conditions = new LambdaQueryWrapper<>();
return ConvertUtils.sourceToTarget(corsConfigDao.selectList(conditions), CorsConfigResultDTO.class);
}
@Override
public CorsConfigResultDTO addConfig(String headerType, String headerValue, String comment) {
LambdaQueryWrapper<CorsConfigEntity> 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));
}
}
}

1
epmet-admin/epmet-admin-server/src/main/resources/mapper/CorsConfigDao.xml

@ -7,6 +7,7 @@
<result property="id" column="ID"/>
<result property="headerType" column="HEADER_TYPE"/>
<result property="headerValue" column="HEADER_VALUE"/>
<result property="comment" column="COMMENT"/>
<result property="delFlag" column="DEL_FLAG"/>
<result property="revision" column="REVISION"/>
<result property="createdBy" column="CREATED_BY"/>

1
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, "至少需要添加一个段落"),

Loading…
Cancel
Save