feat():增加数据库连接密码加密
parent
1856fcee91
commit
85e4b669fe
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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();
|
||||
}
|
||||
}*/
|
||||
}
|
|
@ -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 配置
|
||||
|
|
Loading…
Reference in New Issue