1.该功能为了保护数据库配置及数据安全,在一定的程度上控制开发人员流动导致敏感信息泄露2.加密配置 mpw: 开头紧接加密内容( 非数据库配置专用 YML 中其它配置也是可以使用的 )3.随机密钥请负责人妥善保管,当然越少人知道越好
spring:datasource:url: jdbc:mysql://localhost:3306/mybatis-plususername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
@SpringBootTest
class MybatisPlusApplicationTests {@Testvoid contextLoads() {// 生成 16 位随机 AES 密钥String randomKey = AES.generateRandomKey();// 随机密钥加密String mysql_db = AES.encrypt("jdbc:mysql://localhost:3306/mybatis-plus", randomKey);String mysql_usr = AES.encrypt("root", randomKey);String mysql_pwd = AES.encrypt("123456", randomKey);System.out.println("==============randomKey:"+randomKey);System.out.println("==============mysql_url:"+mysql_db);System.out.println("==============mysql_usr:"+mysql_usr);System.out.println("==============mysql_pwd:"+mysql_pwd);}}
YML加密后
spring:datasource:url: mpw:IlcV2VrLIr+z3ruf0oHP1sV3JuEvntw9QZDEYhQWDNHJ9Xkm7qZokxkEeTCPNqmausername: mpw:aoVz0lDJNymnmrhw6LkQow==password: mpw:StRVtLG7vB6iKVt83du7fw==driver-class-name: com.mysql.cj.jdbc.Driver注意:秘钥b79c41284abe13b5由负责人妥善保管,秘钥可解密-->AES.decrypt("xxxx", "秘钥")
1.IDEA 添加程序实参:--mpw.key=b79c41284abe13b52.WAR包添加程序实参:java -jar plus-0.0.1-SNAPSHOT.war --mpw.key=b79c41284abe13b5