Selaa lähdekoodia

feat:store_user的password字段进行加解密

李亚非 2 kuukautta sitten
vanhempi
commit
e40f98ba3e

+ 94 - 88
alien-config/pom.xml

@@ -1,88 +1,94 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <modelVersion>4.0.0</modelVersion>
-    <parent>
-        <groupId>shop.alien</groupId>
-        <artifactId>alien-cloud</artifactId>
-        <version>1.0.0</version>
-    </parent>
-
-    <artifactId>alien-config</artifactId>
-    <version>1.0.0</version>
-    <name>alien-config</name>
-    <description>alien-config</description>
-
-    <properties>
-        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-    </properties>
-
-    <dependencies>
-
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>org.springframework.cloud</groupId>
-            <artifactId>spring-cloud-starter-openfeign</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>io.github.openfeign</groupId>
-            <artifactId>feign-okhttp</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.alibaba</groupId>
-            <artifactId>druid-spring-boot-starter</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>org.projectlombok</groupId>
-            <artifactId>lombok</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.baomidou</groupId>
-            <artifactId>mybatis-plus-boot-starter</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-redis</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.baomidou</groupId>
-            <artifactId>mybatis-plus-core</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.baomidou</groupId>
-            <artifactId>mybatis-plus-extension</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>shop.alien</groupId>
-            <artifactId>alien-util</artifactId>
-            <version>1.0.0</version>
-        </dependency>
-
-    </dependencies>
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-compiler-plugin</artifactId>
-                <configuration>
-                    <source>8</source>
-                    <target>8</target>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
-
-</project>
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>shop.alien</groupId>
+        <artifactId>alien-cloud</artifactId>
+        <version>1.0.0</version>
+    </parent>
+
+    <artifactId>alien-config</artifactId>
+    <version>1.0.0</version>
+    <name>alien-config</name>
+    <description>alien-config</description>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>io.github.openfeign</groupId>
+            <artifactId>feign-okhttp</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-extension</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>shop.alien</groupId>
+            <artifactId>alien-entity</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>shop.alien</groupId>
+            <artifactId>alien-util</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>8</source>
+                    <target>8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 5 - 0
alien-entity/pom.xml

@@ -87,6 +87,11 @@
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>
+        <dependency>
+            <groupId>shop.alien</groupId>
+            <artifactId>alien-util</artifactId>
+            <version>1.0.0</version>
+        </dependency>
 
     </dependencies>
 

+ 3 - 2
alien-entity/src/main/java/shop/alien/entity/store/StoreUser.java

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
+import shop.alien.util.encryption.EncryptTypeHandler;
 
 import java.util.Date;
 
@@ -18,7 +19,7 @@ import java.util.Date;
  */
 @Data
 @JsonInclude
-@TableName("store_user")
+@TableName(value = "store_user", autoResultMap = true)
 @ApiModel(value = "StoreUser对象", description = "门店用户")
 public class StoreUser extends Model<StoreUser> {
 
@@ -51,7 +52,7 @@ public class StoreUser extends Model<StoreUser> {
     private String idCard;
 
     @ApiModelProperty(value = "密码")
-    @TableField("password")
+    @TableField(value = "password", typeHandler = EncryptTypeHandler.class)
     private String password;
 
     @ApiModelProperty(value = "支付密码")

+ 1 - 1
alien-gateway/src/main/java/shop/alien/gateway/AlienGatewayApplication.java

@@ -17,7 +17,7 @@ import shop.alien.util.encryption.JasyptAutoConfiguration;
  * @date 2025/1/21 16:58
  */
 @Import({JacksonAutoConfiguration.class, JasyptAutoConfiguration.class})
-@ComponentScan({"shop.alien.gateway.*"}) // 停止扫描 shop.alien.util.* 以避免 RestTemplate 依赖冲突
+@ComponentScan({"shop.alien.gateway.*", "shop.alien.util.common", "shop.alien.util.encryption.properties"}) 
 @MapperScan(basePackages = {"shop.alien.gateway.mapper", "shop.alien.mapper"})
 @EnableFeignClients(basePackages = "shop.alien.gateway.feign")
 @SpringBootApplication

+ 19 - 3
alien-store/src/main/java/shop/alien/store/controller/StorePriceController.java

@@ -16,6 +16,8 @@ import shop.alien.store.service.StorePriceService;
 import shop.alien.util.encryption.Decrypt;
 import shop.alien.util.encryption.Encrypt;
 import shop.alien.util.encryption.JasyptEncryptorUtil;
+import shop.alien.util.encryption.StandardAesUtil;
+import shop.alien.util.encryption.properties.EncryptProperties;
 
 import java.util.HashMap;
 import java.util.List;
@@ -40,15 +42,17 @@ public class StorePriceController {
 
     private final StoreInfoMapper storeInfoMapper;
 
+    private final EncryptProperties encryptProperties;
+
     @Value("${test1}")
     private String dbPassword;
 
-    @ApiOperation("测试读取配置与手动加解密")
+    @ApiOperation("读取配置自动解密与手动加解密-测试接口")
     @GetMapping("/testConfig")
     public R<Map<String, Object>> testConfig(@RequestParam String salt, @RequestParam String text) {
         Map<String, Object> result = new HashMap<>();
         
-        // 1. 测试自动解密(Jasypt 注入的结果)
+        // 1. 测试配置文件的value自动解密(Jasypt 注入的结果)
         result.put("configPassword", dbPassword);
 
         // 2. 调用 utils 里的封装进行手动加解密逻辑
@@ -67,7 +71,19 @@ public class StorePriceController {
         return R.data(result, "调试成功");
     }
 
-    @ApiOperation("加解密测试接口")
+    @ApiOperation("AES加密-测试接口")
+    @PostMapping("/aes/encrypt")
+    public R<String> aesEncrypt(@RequestParam String text) {
+        return R.data(StandardAesUtil.encrypt(text, encryptProperties.getKey(), encryptProperties.getIv()));
+    }
+
+    @ApiOperation("AES解密-测试接口")
+    @PostMapping("/aes/decrypt")
+    public R<String> aesDecrypt(@RequestParam String text) {
+        return R.data(StandardAesUtil.decrypt(text, encryptProperties.getKey(), encryptProperties.getIv()));
+    }
+
+    @ApiOperation("AES加密请求体和解密响应体-测试接口")
     @ApiOperationSupport(order = 10)
     @PostMapping("/testEncryption")
     @Decrypt

+ 20 - 7
alien-util/pom.xml

@@ -46,6 +46,7 @@
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
+            <version>4.5.11</version>
         </dependency>
 
         <dependency>
@@ -103,8 +104,26 @@
 
         <dependency>
             <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-core</artifactId>
+            <version>3.2.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-extension</artifactId>
+            <version>3.2.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis</artifactId>
+            <version>3.5.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-generator</artifactId>
-            <version>3.3.1.tmp</version>
+            <version>3.2.0</version>
         </dependency>
 
         <dependency>
@@ -295,12 +314,6 @@
         </dependency>
 
         <dependency>
-            <groupId>shop.alien</groupId>
-            <artifactId>alien-entity</artifactId>
-            <version>1.0.0</version>
-        </dependency>
-
-        <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-java</artifactId>
             <version>3.8.1</version>

+ 39 - 0
alien-util/src/main/java/shop/alien/util/common/SpringContextUtil.java

@@ -0,0 +1,39 @@
+package shop.alien.util.common;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+/**
+ * Spring 上下文工具类
+ */
+@Component
+public class SpringContextUtil implements ApplicationContextAware {
+
+    private static ApplicationContext applicationContext;
+
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        SpringContextUtil.applicationContext = applicationContext;
+    }
+
+    public static ApplicationContext getApplicationContext() {
+        return applicationContext;
+    }
+
+    public static Object getBean(String name) {
+        return applicationContext.getBean(name);
+    }
+
+    public static <T> T getBean(Class<T> clazz) {
+        if (applicationContext == null) {
+            return null;
+        }
+        return applicationContext.getBean(clazz);
+    }
+
+    public static <T> T getBean(String name, Class<T> clazz) {
+        return applicationContext.getBean(name, clazz);
+    }
+}

+ 70 - 0
alien-util/src/main/java/shop/alien/util/encryption/EncryptTypeHandler.java

@@ -0,0 +1,70 @@
+package shop.alien.util.encryption;
+
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+import org.apache.ibatis.type.MappedJdbcTypes;
+import org.apache.ibatis.type.MappedTypes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import shop.alien.util.common.SpringContextUtil;
+import shop.alien.util.encryption.properties.EncryptProperties;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * MyBatis 数据库字段加解密类型转换器
+ * 基于 AES/CBC/PKCS5Padding
+ * 使用方式:在实体类字段上添加 @TableField(typeHandler = EncryptTypeHandler.class)
+ */
+@MappedJdbcTypes(JdbcType.VARCHAR)
+@MappedTypes(String.class)
+public class EncryptTypeHandler extends BaseTypeHandler<String> {
+
+    private static final Logger log = LoggerFactory.getLogger(EncryptTypeHandler.class);
+
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
+        EncryptProperties properties = SpringContextUtil.getBean(EncryptProperties.class);
+        if (properties != null && properties.isEnabled() && parameter != null) {
+            String encrypted = StandardAesUtil.encrypt(parameter, properties.getKey(), properties.getIv());
+            ps.setString(i, encrypted);
+        } else {
+            ps.setString(i, parameter);
+        }
+    }
+
+    @Override
+    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        return decrypt(rs.getString(columnName));
+    }
+
+    @Override
+    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        return decrypt(rs.getString(columnIndex));
+    }
+
+    @Override
+    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+        return decrypt(cs.getString(columnIndex));
+    }
+
+    private String decrypt(String value) {
+        if (value == null || value.isEmpty()) {
+            return value;
+        }
+        EncryptProperties properties = SpringContextUtil.getBean(EncryptProperties.class);
+        if (properties != null && properties.isEnabled()) {
+            try {
+                String decrypted = StandardAesUtil.decrypt(value, properties.getKey(), properties.getIv());
+                return decrypted != null ? decrypted : value;
+            } catch (Exception e) {
+                log.error("数据库字段解密失败,返回原值: {}", e.getMessage());
+                return value;
+            }
+        }
+        return value;
+    }
+}