数据安全保护的作用:防止删库跑路
一、生成16位随机密钥
//@SpringBootTest:表示当前的类是一个测试类,不会随同项目一块打包
@SpringBootTest
//@RunWith:表示启动这个单元测试类(单元测试不能运行),需要传递一个参数,必须是SpringRunner的实例类型
@RunWith(SpringRunner.class)
public class UserMapperTests {
@Test
public void CreateRandomKey(){
//生成 16 位随机 AES 密钥
String randomKey = AES.generateRandomKey();
System.out.println(randomKey);//13fe43969b1bd013
}
}
二、根据密钥加密 数据库连接信息
@Test
public void randomKeyTest(){
// 随机密钥加密
String url = AES.encrypt("jdbc:p6spy:mysql://localhost:3306/mini-boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false", "13fe43969b1bd013");
String username = AES.encrypt("root", "13fe43969b1bd013");
String password = AES.encrypt("123456", "13fe43969b1bd013");
System.out.println(url);
//WXwpYo/DGiOAeS/lyndBPeP9tx9wJf4FG0PhtMzgALzg4he+V/2qsUgmINUDhvj0HmVrYwoK4To5XfKQruPv1kxWHCt9+/z+2LLflHuHQXYdZQvflwuy5CfCRvf5uLMGljsBr/iGS8BBIpSvuQEa46S6tax0Gh0mVGji1k2v3wQ=
System.out.println(username);
//9oQ0O5BY0Qj7n8fYnuPhzg==
System.out.println(password);
//YVKPGf44/FPZB8MiGW2w0A==
}
YML 配置:
server:
port: 8080
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver #com.mysql.cj.jdbc.Driver
password: mpw:YVKPGf44/FPZB8MiGW2w0A==
url: mpw:WXwpYo/DGiOAeS/lyndBPeP9tx9wJf4FG0PhtMzgALzg4he+V/2qsUgmINUDhvj0HmVrYwoK4To5XfKQruPv1kxWHCt9+/z+2LLflHuHQXYdZQvflwuy5CfCRvf5uLMGljsBr/iGS8BBIpSvuQEa46S6tax0Gh0mVGji1k2v3wQ=
username: mpw:9oQ0O5BY0Qj7n8fYnuPhzg==
测试连接数据库
@SpringBootTest
class SpringbootApplicationTests {
@Test
void getConnection() throws SQLException {
System.out.println(dataSource.getConnection());
}
}
本地无法连接
application.yml
spring:
profiles:
active: dev
application-dev.yml (开发环境)
server:
port: 8080
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver #com.mysql.cj.jdbc.Driver
password: 123456
url: jdbc:p6spy:mysql://localhost:3306/mini-boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
application-prod.yml(生产环境)
server:
port: 8080
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver #com.mysql.cj.jdbc.Driver
password: mpw:YVKPGf44/FPZB8MiGW2w0A==
url: mpw:WXwpYo/DGiOAeS/lyndBPeP9tx9wJf4FG0PhtMzgALzg4he+V/2qsUgmINUDhvj0HmVrYwoK4To5XfKQruPv1kxWHCt9+/z+2LLflHuHQXYdZQvflwuy5CfCRvf5uLMGljsBr/iGS8BBIpSvuQEa46S6tax0Gh0mVGji1k2v3wQ=
username: mpw:9oQ0O5BY0Qj7n8fYnuPhzg==
指定yml文件为开发环境可正常连接数据库
使用方法:
项目打包
(1)导入SpringBoot打包插件
org.springframework.boot
spring-boot-maven-plugin
true
(2)检查pom.xml位置标签的内容是否为jar
(3)指定application.yml为生产环境
spring:
profiles:
active: prod
application-prod.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver #com.mysql.cj.jdbc.Driver
password: mpw:YVKPGf44/FPZB8MiGW2w0A==
url: mpw:WXwpYo/DGiOAeS/lyndBPeP9tx9wJf4FG0PhtMzgALzg4he+V/2qsUgmINUDhvj0HmVrYwoK4To5XfKQruPv1kxWHCt9+/z+2LLflHuHQXYdZQvflwuy5CfCRvf5uLMGljsBr/iGS8BBIpSvuQEa46S6tax0Gh0mVGji1k2v3wQ=
username: mpw:9oQ0O5BY0Qj7n8fYnuPhzg==
(4)使用maven package指令打包即可
生成jar文件
通过cmd命令运行项目
java -jar springboot-0.0.1-SNAPSHOT.jar --mpw.key=13fe43969b1bd013 --spring.profiles.active=prod
连接数据库成功,根据id查询一条记录。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)