SpringBoot + MyBatisPlus实现Yaml配置参数AES算法加密

SpringBoot + MyBatisPlus实现Yaml配置参数AES算法加密

1概述

  • 该功能为了保护数据库配置及数据安全,在一定的程度上控制开发人员流动导致敏感信息泄露。
  • MybatisPlus版本要求: 3.3.2+

2 密钥加密

// 生成 16 位随机 AES 密钥
String randomKey = AES.generateRandomKey();

// 随机密钥加密
String result = AES.encrypt(data, randomKey);

3 Yaml参数配置

  • 注意事项: 加密配置必须以 mpw: 字符串开头!!!
// 加密配置 mpw: 开头紧接加密内容(所有配置参数均可使用)
spring:
  datasource:
    url: mpw:SgzR0RIVp7tcrVAgf+KQC6XRa62hnW102m2Xd6l2asvI3TB8ZXT1YsuC79omFCvi/C1tfCQyDUDKngrIrhuQtg==
    password: mpw:npBEYOUyNaSPGiXp9/hVhQ==
    username: mpw:dwDIspd5OkIv9yIyqttPEw==

在这里插入图片描述

4 使用方式

// Jar 启动参数(idea 设置 Program arguments,服务器可以设置为启动环境变量),参数值为步骤2生成的AES 密钥
--mpw.key=e231113965db81b4

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在前端页面上添加一个输入框用于输入搜索关键字,并且在点击搜索按钮时将关键字传递到后端。 然后,在后端的控制器中,接收前端传递的关键字参数,并且调用MyBatisPlus提供的模糊查询API进行查询。 具体实现步骤如下: 1.前端页面实现 在前端页面上添加一个输入框和搜索按钮,当用户在输入框输入了关键字并点击搜索按钮时,将关键字传递到后端。 ```vue <template> <div> <input v-model="keyword" /> <button @click="search">搜索</button> </div> </template> <script> export default { data() { return { keyword: '', }; }, methods: { search() { this.$axios.get('/api/search', { params: { keyword: this.keyword } }).then((res) => { console.log(res.data); }); }, }, }; </script> ``` 2.后端控制器实现后端控制器中,接收前端传递的关键字参数,并且调用MyBatisPlus提供的模糊查询API进行查询。这里我们使用MyBatisPlus提供的QueryWrapper对象来构建查询条件,并且使用Lambda表达式来简化代码。 ```java @RestController @RequestMapping("/api") public class SearchController { @Autowired private UserService userService; @GetMapping("/search") public List<User> search(String keyword) { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like("name", keyword).or().like("email", keyword); return userService.list(wrapper); } } ``` 在上面的代码中,我们构建了一个QueryWrapper对象,使用like方法来查询name或者email字段中包含关键字的数据。最终调用userService的list方法来执行查询操作。 需要注意的是,在这里我们假设UserService是一个MyBatisPlus提供的服务类,并且已经配置好了MapperScannerConfigurer来扫描Mapper接口。如果还没有配置,可以参考MyBatisPlus的文档进行配置。 3.配置MyBatisPlus 在使用MyBatisPlus的模糊查询功能前,需要先配置MyBatisPlus。在SpringBoot中,只需要在配置文件中添加如下配置即可: ```yaml mybatis-plus: mapper-locations: classpath:/mapper/**/*.xml global-config: db-config: logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true ``` 在上面的配置中,我们指定了Mapper接口XML文件的位置,配置了逻辑删除的值和驼峰命名规则等。 4.编写Mapper接口和XML文件 最后,我们需要编写Mapper接口和XML文件来实现具体的查询操作。这里我们以User表为例,编写如下代码: ```java public interface UserMapper extends BaseMapper<User> { } ``` ```xml <mapper namespace="com.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="email" property="email" /> </resultMap> <select id="selectByKeyword" resultMap="BaseResultMap"> select * from user where name like concat('%', #{keyword}, '%') or email like concat('%', #{keyword}, '%') </select> </mapper> ``` 在上面的代码中,我们使用了MyBatisPlus提供的BaseMapper接口,并且编写了一个selectByKeyword方法来执行模糊查询操作。需要注意的是,我们使用了concat函数来将%和关键字拼接起来,以实现模糊查询的效果。 至此,我们已经完成了springboot+mybatisplus+vue实现模糊查询的全部操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值