From 0d7e48c3b145478e503207a610c84dc14de2f20f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?=
<8569561+fuqiangma@user.noreply.gitee.com>
Date: Thu, 30 Mar 2023 22:17:24 +0800
Subject: [PATCH] shardingsphere jiajiemi
---
shardingspherejamijiemi/.gitignore | 27 +++
shardingspherejamijiemi/fanfu-web.iml | 92 ++++++++++
shardingspherejamijiemi/pom.xml | 140 ++++++++++++++
.../main/java/com/woniu/WoNiuApplication.java | 23 +++
.../service/config/MyShardingEncryptor.java | 122 ++++++++++++
.../service/controller/OrderController.java | 47 +++++
.../java/com/woniu/service/entity/Order.java | 134 ++++++++++++++
.../woniu/service/enums/ResponseCodeEnum.java | 39 ++++
.../service/generator/CodeGenerator.java | 45 +++++
.../com/woniu/service/mapper/OrderMapper.java | 15 ++
.../woniu/service/service/IOrderService.java | 16 ++
.../service/impl/OrderServiceImpl.java | 20 ++
.../com/woniu/service/util/ResultEntity.java | 89 +++++++++
...rdingsphere.encrypt.strategy.spi.Encryptor | 1 +
.../src/main/resources/application.properties | 14 ++
.../src/main/resources/application.yml | 58 ++++++
.../src/main/resources/logback-spring.xml | 9 +
.../src/main/resources/sql.sql | 22 +++
.../src/test/java/com/woniu/TestA.java | 24 +++
shardingspherejamijiemi/woniu-web.iml | 173 ++++++++++++++++++
20 files changed, 1110 insertions(+)
create mode 100644 shardingspherejamijiemi/.gitignore
create mode 100644 shardingspherejamijiemi/fanfu-web.iml
create mode 100644 shardingspherejamijiemi/pom.xml
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/WoNiuApplication.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/config/MyShardingEncryptor.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/controller/OrderController.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/entity/Order.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/enums/ResponseCodeEnum.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/generator/CodeGenerator.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/mapper/OrderMapper.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/service/IOrderService.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/service/impl/OrderServiceImpl.java
create mode 100644 shardingspherejamijiemi/src/main/java/com/woniu/service/util/ResultEntity.java
create mode 100644 shardingspherejamijiemi/src/main/resources/META-INF/services/org.apache.shardingsphere.encrypt.strategy.spi.Encryptor
create mode 100644 shardingspherejamijiemi/src/main/resources/application.properties
create mode 100644 shardingspherejamijiemi/src/main/resources/application.yml
create mode 100644 shardingspherejamijiemi/src/main/resources/logback-spring.xml
create mode 100644 shardingspherejamijiemi/src/main/resources/sql.sql
create mode 100644 shardingspherejamijiemi/src/test/java/com/woniu/TestA.java
create mode 100644 shardingspherejamijiemi/woniu-web.iml
diff --git a/shardingspherejamijiemi/.gitignore b/shardingspherejamijiemi/.gitignore
new file mode 100644
index 0000000..682d4ed
--- /dev/null
+++ b/shardingspherejamijiemi/.gitignore
@@ -0,0 +1,27 @@
+# Created by .ignore support plugin (hsz.mobi)
+### Java template
+# Compiled class file
+*.class
+
+# Log file
+*.log
+
+# BlueJ files
+*.ctxt
+
+# Mobile Tools for Java (J2ME)
+.mtj.tmp/
+
+# Package Files #
+*.jar
+*.war
+*.nar
+*.ear
+*.zip
+*.tar.gz
+*.rar
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*
+/.idea/
+/target/
diff --git a/shardingspherejamijiemi/fanfu-web.iml b/shardingspherejamijiemi/fanfu-web.iml
new file mode 100644
index 0000000..4fb2f38
--- /dev/null
+++ b/shardingspherejamijiemi/fanfu-web.iml
@@ -0,0 +1,92 @@
+
+
+ * + *
+ * + * @author woniu,公众号:【程序员蜗牛g】 + * @since 2023-03-30 + */ +@Configuration +public class MyShardingEncryptor implements Encryptor { + + private final Logger log = LoggerFactory.getLogger(MyShardingEncryptor.class); + + // AES KEY + private static final String AES_KEY = "aes.key.value"; + + private Properties properties = new Properties(); + + public MyShardingEncryptor(){ + + } + + @Override + public void init() { + + } + + @Override + public String encrypt(Object plaintext) { + try { + byte[] result = this.getCipher(1).doFinal(StringUtils.getBytesUtf8(String.valueOf(plaintext))); + log.debug("[MyShardingEncryptor]>>>> 加密: {}", Base64.encodeBase64String(result)); + return Base64.encodeBase64String(result); + } catch (Exception ex) { + log.error("[MyShardingEncryptor]>>>> 加密异常:", ex); + } + return null; + + } + + @Override + public Object decrypt(String ciphertext) { + try { + if (null == ciphertext) { + return null; + } else { + byte[] result = this.getCipher(2).doFinal(Base64.decodeBase64(ciphertext)); + log.debug("[MyShardingEncryptor]>>>> 解密: {}", new String(result)); + return new String(result); + } + } catch (Exception ex) { + log.error("[MyShardingEncryptor]>>>> 解密异常:", ex); + } + return null; + } + + @Override + public String getType() { + return "mySharding"; // 和yml配置一致 + } + + @Override + public Properties getProperties() { + return this.properties; + } + + @Override + public void setProperties(Properties properties) { + this.properties = properties; + } + + /** + * 加解密算法 + * @param decryptMode 1-加密,2-解密,还有其他类型可以点进去看源码。 + */ + private Cipher getCipher(int decryptMode) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { + Preconditions.checkArgument(this.properties.containsKey("aes.key.value"), "No available secret key for `%s`.", + AESEncryptor.class.getName()); + Cipher result = Cipher.getInstance("AES"); + result.init(decryptMode, new SecretKeySpec(this.createSecretKey(), "AES")); + return result; + } + + /** + * 创建密钥,规则根据自己需要定义。 + * -- PS: 生产环境规范要求不能打印出密钥相关日志,以免发生意外泄露情况。 + */ + private byte[] createSecretKey() { + // yml中配置的原始密钥 + String oldKey = this.properties.get("aes.key.value").toString(); + Preconditions.checkArgument(null != oldKey, String.format("%s can not be null.", "aes.key.value")); + /* + * 将原始密钥和自定义的盐一起再次加密生成新的密钥返回. + * 注意,因为我们用的AES加解密方式最终密钥必须16位,否则AES会报错, + * 而application.yml中配置的aes.key.value是10位字符组合,所以这里才substring(0,5),否则最终没有返回16位会抛AES异常,可以自己试验下。 + */ + String secretKey = DigestUtils.sha1Hex(oldKey + AES_KEY).toUpperCase().substring(0, 5) + "!" + oldKey; + // 密钥打印在上线前一定要删掉,避免泄露引起安全事故。 + log.debug("[MyShardingEncryptor]>>>> 密钥: {}", secretKey); + return secretKey.getBytes(); + } + +} diff --git a/shardingspherejamijiemi/src/main/java/com/woniu/service/controller/OrderController.java b/shardingspherejamijiemi/src/main/java/com/woniu/service/controller/OrderController.java new file mode 100644 index 0000000..76751cd --- /dev/null +++ b/shardingspherejamijiemi/src/main/java/com/woniu/service/controller/OrderController.java @@ -0,0 +1,47 @@ +package com.woniu.service.controller; + +import com.alibaba.fastjson.JSONObject; +import com.woniu.service.entity.Order; +import com.woniu.service.service.IOrderService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * SpringBoot+ShardingSphere实现数据库字段加密存储和解密返回 + */ +@RestController +@RequestMapping("/order") +@Slf4j +public class OrderController { + + + @Autowired + private IOrderService orderService; + + + /** + * 查询订单 + */ + @GetMapping("/list") + public ResponseEntity+ * + *
+ * + * @author woniu,公众号:【程序员蜗牛g】 + * @since 2023-03-30 + */ +@TableName("tb_order") +@ApiModel(value = "Order对象", description = "") +public class Order implements Serializable { + + private static final long serialVersionUID = 2132123l; + + @ApiModelProperty("主键id") + @TableId(value = "id", type = IdType.AUTO) + private Long id; + + @ApiModelProperty("订单号") + private String orderId; + + @ApiModelProperty("用户姓名") + private String name; + + @ApiModelProperty("身份证号") + private String idCard; + + @ApiModelProperty("订单类型,1-挂号,2-门诊,3-住院。") + private String orderType; + + @ApiModelProperty("订单状态,0-待支付,1-支付成功,2-支付失败。") + private String orderStatus; + + @ApiModelProperty("订单金额") + private String amount; + + @ApiModelProperty("创建时间") + private Date createdAt; + + @ApiModelProperty("更新时间") + private Date updatedAt; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + public String getOrderId() { + return orderId; + } + + public void setOrderId(String orderId) { + this.orderId = orderId; + } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public String getIdCard() { + return idCard; + } + + public void setIdCard(String idCard) { + this.idCard = idCard; + } + public String getOrderType() { + return orderType; + } + + public void setOrderType(String orderType) { + this.orderType = orderType; + } + public String getOrderStatus() { + return orderStatus; + } + + public void setOrderStatus(String orderStatus) { + this.orderStatus = orderStatus; + } + public String getAmount() { + return amount; + } + + public void setAmount(String amount) { + this.amount = amount; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public Date getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Date updatedAt) { + this.updatedAt = updatedAt; + } + + @Override + public String toString() { + return "Order{" + + "id=" + id + + ", orderId=" + orderId + + ", name=" + name + + ", idCard=" + idCard + + ", orderType=" + orderType + + ", orderStatus=" + orderStatus + + ", amount=" + amount + + ", createdAt=" + createdAt + + ", updatedAt=" + updatedAt + + "}"; + } +} diff --git a/shardingspherejamijiemi/src/main/java/com/woniu/service/enums/ResponseCodeEnum.java b/shardingspherejamijiemi/src/main/java/com/woniu/service/enums/ResponseCodeEnum.java new file mode 100644 index 0000000..ff1ddce --- /dev/null +++ b/shardingspherejamijiemi/src/main/java/com/woniu/service/enums/ResponseCodeEnum.java @@ -0,0 +1,39 @@ +package com.woniu.service.enums; + +/** + *+ * 状态枚举类 + *
+ * + * @author 公众号:【程序员蜗牛g】 + */ +public enum ResponseCodeEnum { + + SUCCESS(0, "0", "成功"), + FAIL (-1, "-1", "失败"), + PARAMETER_ERROR(1,"1","参数错误"), + ERROR(500, "500", "错误"); + + private final int id; + private final String code; + private final String label; + + ResponseCodeEnum(final int id, final String code, final String label) { + this.id = id; + this.code = code; + this.label = label; + } + + public int getId() { + return id; + } + + public String getCode() { + return code; + } + + public String getLabel() { + return label; + } + +} diff --git a/shardingspherejamijiemi/src/main/java/com/woniu/service/generator/CodeGenerator.java b/shardingspherejamijiemi/src/main/java/com/woniu/service/generator/CodeGenerator.java new file mode 100644 index 0000000..5d7ccd2 --- /dev/null +++ b/shardingspherejamijiemi/src/main/java/com/woniu/service/generator/CodeGenerator.java @@ -0,0 +1,45 @@ +package com.woniu.service.generator; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; + +import java.util.Collections; + +/** + *+ * 状态枚举类 + *
+ * + * @author 公众号:【程序员蜗牛g】 + */ +public class CodeGenerator { + + private static final String url = "jdbc:mysql://localhost:3306/encrypt_demo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false"; + private static final String username = "root"; + private static final String password = "123456"; + private static final String outputDir = "D:\\workspace\\workspace_blog\\encrypt-demo\\src\\main\\java"; // entity、mapper、service、controller生成的目录地址,换成自己项目的。 + private static final String xmlOutputDir = "D:\\workspace\\workspace_blog\\encrypt-demo\\src\\main\\resources\\mapper"; // xxMapper.xml生成的目录地址,换成自己项目的。 + + public static void main(String[] args) { + FastAutoGenerator.create(url, username, password) + .globalConfig(builder -> { + builder.author("福隆苑居士,公众号:【Java分享客栈】") // 设置作者 + .enableSwagger() // 开启 swagger 模式 + .fileOverride() // 覆盖已生成文件 + .outputDir(outputDir); // 指定输出目录 + }) + .packageConfig(builder -> { + builder.parent("com.example.encrypt") // 设置父包名,和自己项目的父包名一致即可。 + .moduleName("") // 设置父包模块名,为空就会直接生成在父包名目录下。 + .pathInfo(Collections.singletonMap(OutputFile.mapperXml, xmlOutputDir)); // 设置mapperXml生成路径 + }) + .strategyConfig(builder -> { + builder.addInclude("tb_order") // 设置需要生成的表名,多个用逗号隔开。 + .addTablePrefix("t_", "tb_", "c_"); // 设置过滤表前缀,多个用逗号隔开。 + }) + .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 + .execute(); + } + +} diff --git a/shardingspherejamijiemi/src/main/java/com/woniu/service/mapper/OrderMapper.java b/shardingspherejamijiemi/src/main/java/com/woniu/service/mapper/OrderMapper.java new file mode 100644 index 0000000..3fa7a59 --- /dev/null +++ b/shardingspherejamijiemi/src/main/java/com/woniu/service/mapper/OrderMapper.java @@ -0,0 +1,15 @@ +package com.woniu.service.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.woniu.service.entity.Order; + +/** + *+ * 状态枚举类 + *
+ * + * @author 公众号:【程序员蜗牛g】 + */ +public interface OrderMapper extends BaseMapper+ * 状态枚举类 + *
+ * + * @author 公众号:【程序员蜗牛g】 + */ +public interface IOrderService extends IService+ * 状态枚举类 + *
+ * + * @author 公众号:【程序员蜗牛g】 + */ +@Service +public class OrderServiceImpl extends ServiceImpl+ * 状态枚举类 + *
+ * + * @author 公众号:【程序员蜗牛g】 + */ +public class ResultEntity