MybatisPlus数据安全,你掌握了吗

MybatisPlus数据安全概述

存在数据库中的数据对于普通用户而言是不可见的,好像是藏起来了一样,但对于开发者,只要知道数据库的连接地址、用户名、密码,则数据不再安全;这也意味着,一旦连接数据库的配置文件暴露出去,则数据不再安全。

应用场景

开发中的数据库配置文件或配置中心中的配置信息

API介绍

MybatisPlus中有个针对配置项加密处理的

MybatisPlus数据安全,你掌握了吗
MybatisPlus数据安全,你掌握了吗
代码实现1 创建mp工程

创建maven工程,结构如下:

MybatisPlus数据安全,你掌握了吗
2 代码编写pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies>application.ymlspring: datasource: url: jdbc:mysql://localhost:3306/springboot?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root mybatis-plus: type-aliases-package: com.itheima.pojo启动类package com.itheima; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @version 1.0 * @description 说明 * @package com.itheima */ @SpringBootApplication @MapperScan(basePackages = “com.itheima.mapper”) public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } } pojopackage com.itheima.pojo; import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; /** * @version 1.0 * @description 说明 * @package com.itheima.pojo */ @Data public class User { private Integer id; private String username; @TableField(select = false) private String password; private String salt; } mapperpackage com.itheima.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.itheima.pojo.User; import org.springframework.stereotype.Repository; /** * @version 1.0 * @description 说明 * @package com.itheima.mapper */ @Repository public interface UserMapper extends BaseMapper<User> { } service接口与实现类package com.itheima.service; import com.baomidou.mybatisplus.extension.service.IService; import com.itheima.pojo.User; /** * @version 1.0 * @description 说明 * @package com.itheima.service */ public interface UserService extends IService<User> { } package com.itheima.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.itheima.mapper.UserMapper; import com.itheima.pojo.User; import com.itheima.service.UserService; import org.springframework.stereotype.Service; /** * @version 1.0 * @description 说明 * @package com.itheima.service.impl */ @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } controllerpackage com.itheima.controller; import com.itheima.pojo.User; import com.itheima.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @version 1.0 * @description 说明 * @package com.itheima.controller */ @RestController @RequestMapping(“user”) public class UserController { @Autowired private UserService userService; @GetMapping public List<User> listAll(){ return userService.list(); } } 启动测试生成加密后的内容package com.itheima; import com.baomidou.mybatisplus.core.toolkit.AES; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @version 1.0 * @description 说明 * @package com.itheima */ @SpringBootApplication @MapperScan(basePackages = “com.itheima.mapper”) public class App { public static void main(String[] args) { String secretKey = AES.generateRandomKey(); System.out.println(“secretKey:” + secretKey); String url = AES.encrypt(“jdbc:mysql://localhost:3306/springboot?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai”, secretKey); String username = AES.encrypt(“username”, secretKey); String password = AES.encrypt(“password”, secretKey); System.out.println(“url=” +url ); System.out.println(“username=” +username ); System.out.println(“password=” +password ); SpringApplication.run(App.class,args); } }
MybatisPlus数据安全,你掌握了吗
替换配置文件spring: datasource: url: mpw:wT9PqZ9Hf4VWgXDuZ/Z1JKfdDyS0sSu3+O2qDkJ/Ulnabpq3z1lZbiThWseQ4DQSx3+SWpufsTysjdYhn6Scsa77AzIIaUgv8DZ17gPxAq88AISmxd9OjxidmY50uBVMkGhP9qAted45zuHBzVrw6Q== driver-class-name: com.mysql.cj.jdbc.Driver username: mpw:Pnh++mI45YrC4s6JveJYaA== password: mpw:Pnh++mI45YrC4s6JveJYaA== mybatis-plus: type-aliases-package: com.itheima.pojo添加启动参数
MybatisPlus数据安全,你掌握了吗
运行效果
MybatisPlus数据安全,你掌握了吗
3 优化目的

启动时,需要指定密钥 才能使用,如果我们把它封装到一个jar里,让它启动时自动去加载密钥,且密钥可配置,那这样就更灵活了。

分析

通过查看MybatisPlus加载的源码,其做解密处理的类如下:

/* * Copyright (c) 2011-2020, baomidou (jobob@qq.com). * <p> * Licensed under the Apache License, Version 2.0 (the “License”); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * <p> * https://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an “AS IS” BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.autoconfigure; import com.baomidou.mybatisplus.core.toolkit.AES; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.OriginTrackedMapPropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource; import java.util.HashMap; /** * 安全加密处理器 * * @author hubin * @since 2020-05-23 */ public class SafetyEncryptProcessor implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { /** * 命令行中获取密钥 */ String mpwKey = null; for (PropertySource<?> ps : environment.getPropertySources()) { if (ps instanceof SimpleCommandLinePropertySource) { SimpleCommandLinePropertySource source = (SimpleCommandLinePropertySource) ps; mpwKey = source.getProperty(“mpw.key”); break; } } /** * 处理加密内容 */ if (StringUtils.isNotBlank(mpwKey)) { HashMap<String, Object> map = new HashMap<>(); for (PropertySource<?> ps : environment.getPropertySources()) { if (ps instanceof OriginTrackedMapPropertySource) { OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource) ps; for (String name : source.getPropertyNames()) { Object value = source.getProperty(name); if (value instanceof String) { String str = (String) value; if (str.startsWith(“mpw:”)) { map.put(name, AES.decrypt(str.substring(4), mpwKey)); } } } } } // 将解密的数据放入环境变量,并处于第一优先级上 if (CollectionUtils.isNotEmpty(map)) { environment.getPropertySources().addFirst(new MapPropertySource(“custom-encrypt”, map)); } } } }

其使用了SPI原理,在类所在的jar下的META-INF/spring.factories中配置了这个SafetyEncryptProcessor。那我们能否也来定义一个这样的配置处理器,判断环境配置中是否配置了–mpw.key,如果没有配置,则给它配置上,这样就不用在启动时添加参数来运行了。

MybatisPlus数据安全,你掌握了吗
实现创建配置工程mysafe
MybatisPlus数据安全,你掌握了吗
代码清单

pom.xml

<parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.3.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>

SafetyEncryptProcessor

package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.io.ClassPathResource; import org.springframework.util.StringUtils; import java.io.IOException; import java.util.Properties; /** * @version 1.0 * @description 说明 * @package com.itheima */ public class SafetyEncryptProcessor implements EnvironmentPostProcessor, Ordered { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { Properties pro = new Properties(); try { pro.load(new ClassPathResource(“ert.properties”).getInputStream()); } catch (IOException e) { e.printStackTrace(); } /** * 命令行中获取密钥 */ String mpwKey = null; for (PropertySource<?> ps : environment.getPropertySources()) { if (ps instanceof SimpleCommandLinePropertySource) { SimpleCommandLinePropertySource source = (SimpleCommandLinePropertySource) ps; mpwKey = source.getProperty(“mpw.key”); break; } } if(StringUtils.isEmpty(mpwKey)){ environment.getPropertySources().addFirst(new SimpleCommandLinePropertySource(“mySpringApplicationCommandLineArgs”, “–mpw.key=” + pro.getProperty(“ert.version”))); } } @Override public int getOrder() { return 0; } }

spring.factories

ert.version=b440fe7fd55dbe26 org.springframework.boot.env.EnvironmentPostProcessor= com.itheima.SafetyEncryptProcessor

ert.properties

ert.version=2ac6625cb3188f52安装到本地仓库
MybatisPlus数据安全,你掌握了吗
修改mp工程添加依赖

修改pom.xml,添加mysafe的依赖

<dependency> <groupId>com.itheima</groupId> <artifactId>mysafe</artifactId> <version>1.0-SNAPSHOT</version> </dependency>4 测试结果

去除启动时的参数设置。再启动后访问页面、效果如下:

MybatisPlus数据安全,你掌握了吗
总结MybatisPlus利用了springboot的配置信息增强器与SPI机制来实现对配置文件中敏感数据的解密处理。mysafe工程打成的jar包,将来就上传到企业的内部服务器上,当密钥变更时,重新打包即可。而我们将来布署项目到服务器上时,也肯定存在配置文件,一旦配置信息暴露,则数据库就危险了。通过加密手段能够让破解者增加破解阻碍。此次练习仅做抛砖引玉作用,关于加密与解密是没有做到那么严谨的,需要结合自己公司实际情况去调整。

免责声明:文章内容来自互联网,本站仅作为分享,不对其真实性负责,如有侵权等情况,请与本站联系删除。
转载请注明出处:MybatisPlus数据安全,你掌握了吗 https://www.dachanpin.com/a/cyfx/11431.html

(0)
上一篇 2023-05-12 03:18:32
下一篇 2023-05-12

相关推荐

  • 宁夏石嘴山市“平台+培训+政策”助推大学生创业就业

    近年来,石嘴山市人力资源和社会保障局以大学生创业引领计划为锲机,采取搭建平台、开展培训、落实政策、树立典型等措施,助推大学生创业就业,在全市形成大众创业、万众创新的热潮。 为了给大学生创业就业营造良好的创业氛围,石嘴山市邀请新闻媒体对全市全民创业情况进行专题报道,印制、发放大学生创业宣传彩页和折页5000份,开展高校毕业生创业就业政策专项宣传活动,让更多大学…

    创业分享 2023-05-28
    3300
  • 创业环境好 企业劲头足

      8月24日,第四届全球吉商大会吉商投资及经贸合作项目对接洽谈会在长春举行,吉林省各地区的项目负责同志和与会吉商展开沟通交流。   丁 研摄   “当初,回吉林创业是有阻力的。”科大讯飞吉林公司总经理徐艺华回忆2015年组建讯飞子公司时说。   科大讯飞,现已掌握众多拥有自主知识产权的核心技术。4年前,科大讯飞在吉林省布局了子公司,并成立了科大讯飞在全国的…

    2023-05-12
    10800
  • 大学生暑期创业赚“经历” 让梦想“初具雏形”

      为了实现自己的创业梦,小西一直悄悄努力着。一直不断提高自己的厨艺和销售水平。家人暑期让她去旅游,她却选择去工厂企业打工做销售。“去年暑假去了一家服装厂,做过包装的活儿,累,工资低,主要是学不到我想要的东西。”她的兼职主要是能够锻炼,提升经历,工资高低是次要的。当得知一家艺术学校招暑期工时,她的伶牙俐齿和亲和力很快帮助自己通过了相关面试。 (责任编辑:ad…

    创业分享 2023-05-12
    11400
  • 贝珞茵女装靠谱吗质量怎样?加入开店让创业之路更显顺畅

    贝珞茵女装靠谱吗质量怎样?加入开店让创业之路更显顺畅!就是因为贝珞茵女装总部的服务比较好,而且本身这一个品牌内部的女装在市面上的综合口碑和认可度又是比较高的,所以如果你能选择代理它,将品牌商标量出来的话,是不用担心后期服装卖不出去、会滞销问题的,而且哪怕有一部分衣服确实因为号码过小等原因卖不出去、它们是可以退货的,所以不用有滞销的顾虑,可以放心。 那么,所谓…

    创业分享 2023-05-12
    10400
  • 茶饮加盟怎么选,茶亭序品牌成为众多创业者的首选

    在云深不知处 ① 凡本网注明”稿件来源:济宁日报、济宁晚报”的所有文字和图片稿件,版权均属于 济宁日报社和东方圣城网所有,任何媒体、网站或个人未经本网协议授权不得转载、链接、转贴或以其他方式复制发表。已经本网协议授权的媒体、网站,在下载使用时必须注明”稿件来源: 东方圣城网”,违者本网将依法追究责任。 地址:广…

    创业分享 2023-05-12
    7700

发表回复

登录后才能评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信