【Mybatis Plus-10】数据安全保护

数据安全保护的作用:防止删库跑路
一、生成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打包插件

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

(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查询一条记录。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis-Plus提供了数据安全保护功能,其中包括字段加密和解密。在使用加密功能时,数据库存储的字段内容会以十六进制格式的密文形式保存。对于条件查询,如果不对密文进行处理,将无法匹配出想要的结果。为了实现条件查询,可以使用SQL的AES_DECRYPT函数对密文进行解密,并进行匹配。需要注意的是,SQL的解密函数只有AES_DECRYPT,因此只适用于AES加密算法。\[3\] 在具体的代码实现中,可以使用MyBatis-Plus的QueryWrapper来构建查询条件。在查询方法中,可以通过调用AES_DECRYPT函数对加密字段进行解密,并与目标值进行匹配。例如,在getAll方法中,可以使用QueryWrapper的like方法来进行模糊查询,同时使用AES_DECRYPT函数对加密字段进行解密,并与目标值进行匹配。最后,返回匹配的结果列表。\[1\] 需要注意的是,加密后的字段内容需要与数据库中存储的密文进行匹配,而不是与明文进行匹配。因此,在进行条件查询时,需要对密文进行解密后再进行匹配。\[2\] 总结起来,MyBatis-Plus的加密功能可以保护数据安全性,通过使用AES_DECRYPT函数对密文进行解密,可以实现条件查询。\[3\] #### 引用[.reference_title] - *1* *3* [MyBatis-Plus加密字段查询(密文检索)](https://blog.csdn.net/tongxin_tongmeng/article/details/128733039)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [MyBatis-Plus数据安全保护(加密解密)](https://blog.csdn.net/tongxin_tongmeng/article/details/128685399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小火柴127

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值