想让别人看不到你的数据库密码该怎么办呢???
1.建立maven项目添加依赖。
<dependencies>
<!-- com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!-- artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.18</version>
</dependency>
</dependencies>
2、新建一个Java文件连接数据库。
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn.scl;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.SQLException;
/**
* <p>Project: jdbcfirst - Dome1</p>
* <p>Powered by scl On 2023-08-16 13:02:49</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class Dome1 {
public static void main(String[] args) throws SQLException {
DruidDataSource ds = new DruidDataSource();
ds.setInitialSize(10);
ds.setMaxActive(50);
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql:/book");
ds.setUsername("admin");
ds.setFilters("stat,config");
ds.setConnectionProperties("config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH5gqQc19zlXxwLQklRKj77VgeKvDX5XoGEX/ADnXtGo3KN+Ldpw/hVTTz4iYD2aeTyfkObOrOMcaokmYSB/wECAwEAAQ==");
ds.setPassword("i/yutkZ8G3M8h2/jKIvccSgRHUhg3nCVERCLO0BDkCB9kBaZps1gExQ6vnob6fUN0VM/7cLjsAUFtrrBG39Xxg==");
//ds.setPassword("admin");
var c = ds.getConnection();
//验证是否连接成功,成功就输出数据库的版本号
System.out.println(c.getMetaData().getDatabaseProductVersion());
}
}
3.找到maven\repo\com\alibaba\druid\1.2.18这个目录(加密)
如果你还没下载maven这里可以很详细的告诉你:maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客
命令:java -cp druid-1.2.18.jar com.alibaba.druid.filter.config.ConfigTools admin(这个是你的数据库密码)
注意这两个密码:
在Java文件中填写写两个密码:
有小伙伴该问了,那该怎么解密呢???? 别着急现在就告诉你
4.解密
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn.scl;
import com.alibaba.druid.filter.config.ConfigTools;
/**
* <p>Project: jdbcfirst - Test</p>
* <p>Powered by scl On 2023-08-16 11:26:56</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class Test {
public static void main(String[] args) throws Exception {
String pk="MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH5gqQc19zlXxwLQklRKj77VgeKvDX5XoGEX/ADnXtGo3KN+Ldpw/hVTTz4iYD2aeTyfkObOrOMcaokmYSB/wECAwEAAQ==";
String ps="i/yutkZ8G3M8h2/jKIvccSgRHUhg3nCVERCLO0BDkCB9kBaZps1gExQ6vnob6fUN0VM/7cLjsAUFtrrBG39Xxg==";
String password= ConfigTools.decrypt(pk,ps);
System.out.println(password);
}
}
注意千万别写错了!!!