feat():增加数据库连接密码加密

master
DongZeLiang 2024-09-11 17:59:50 +08:00
parent 1856fcee91
commit 85e4b669fe
8 changed files with 102 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package com.muyu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* @author DongZeLiang
@ -10,7 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @description
* @date 2023/11/9
*/
@SpringBootApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan(value = "com.muyu.**.mapper")
public class VehicleSimulationApplication {

View File

@ -0,0 +1,39 @@
package com.muyu.config;
import com.muyu.utils.AesUtil;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.sql.Connection;
/**
* @Author: DongZeLiang
* @date: 2024/9/11
* @Description:
* @Version: 1.0
*/
@Log4j2
@Configuration
public class DataSourceConfig {
/**
* Bean
* @param dataSourceProperties
* @return hikari
*/
@Bean
public HikariDataSource initDataSource(DataSourceProperties dataSourceProperties) throws Exception {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(dataSourceProperties.getUrl());
hikariDataSource.setUsername(dataSourceProperties.getUsername());
hikariDataSource.setPassword(AesUtil.decrypt(dataSourceProperties.getPassword()));
hikariDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
Connection connection = hikariDataSource.getConnection();
log.info("数据源初始化成功");
hikariDataSource.evictConnection(connection);
return hikariDataSource;
}
}

View File

@ -1,4 +1,4 @@
package com.muyu.web.config;
package com.muyu.config;
import com.muyu.web.common.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;

View File

@ -1,4 +1,4 @@
package com.muyu.web.config;
package com.muyu.config;
import com.dtflys.forest.annotation.BindingVar;
import com.muyu.system.handle.SystemHandler;

View File

@ -1,4 +1,4 @@
package com.muyu.web.config;
package com.muyu.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;

View File

@ -1,4 +1,4 @@
package com.muyu.web.config;
package com.muyu.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;

View File

@ -0,0 +1,54 @@
package com.muyu.utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AesUtil {
private static final String key = "oLXZmx1h1nAo2G832yEceA==";
private static final String ALGORITHM = "AES";
private static final int KEY_SIZE = 128;
public static String generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(KEY_SIZE);
SecretKey secretKey = keyGenerator.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
public static String encrypt(String data) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
/*public static void main(String[] args) {
try {
String data = "Vehicle@666";
String encryptedData = encrypt(data);
String decryptedData = decrypt(encryptedData);
System.out.println("Original Key: " + key);
System.out.println("Original Data: " + data);
System.out.println("Encrypted Data: " + encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
} catch (Exception e) {
e.printStackTrace();
}
}*/
}

View File

@ -5,12 +5,13 @@ spring:
allow-circular-references: true
mvc:
static-path-pattern: /static/**
# 数据源配置
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://47.92.86.60:23658/muyu-vehicle?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: Vehicle@666
password: YFmFzrZt8zJhOqgNX3VDgA==
# mybatis-plus 配置