如何清理DruidDataSource datasource中的明文信息
时间: 2024-01-14 13:44:26 浏览: 44
在DruidDataSource中,可以通过调用setPasswordCallback方法来设置一个回调函数,用于在每次获取数据库连接时动态获得密码信息。这样可以避免在代码中明文存储密码信息,提高安全性。
示例代码如下:
```java
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(username);
dataSource.setPasswordCallback(new PasswordCallback() {
@Override
public void setPassword(String password) {
// 在此处动态获取密码信息,并设置到dataSource中
dataSource.setPassword(password);
}
});
```
在实际使用中,可以根据具体情况实现PasswordCallback接口中的setPassword方法,例如从配置文件中读取密码信息,或者从安全存储中获取密码信息。这样就可以在不暴露明文密码的情况下使用DruidDataSource。
相关问题
DataSource 转 DruidDataSource
Druid 是一个 JDBC 连接池和监控平台,相比于 DataSource,它提供了更多的功能和配置选项。如果你需要使用 Druid,可以通过以下步骤将 DataSource 转换为 DruidDataSource。
1. 添加 Druid 依赖
在项目的 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
```
其中,`${druid.version}` 是你希望使用的 Druid 版本号。
2. 配置 DruidDataSource
在配置文件中添加以下配置:
```properties
# 数据库连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
# Druid 配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.filters=stat,wall,log4j
```
在上面的配置中,我们设置了一个名为 `spring.datasource` 的数据源,并将其类型设置为 `com.alibaba.druid.pool.DruidDataSource`,这样就可以使用 Druid 连接池了。同时,我们也配置了一些 Druid 的属性,例如连接池初始化大小、最小空闲连接数、最大活动连接数等。
3. 注入 DruidDataSource
在代码中,你可以通过 `@Autowired` 注解将 DruidDataSource 注入到需要使用数据源的地方,例如:
```java
@Autowired
private DruidDataSource dataSource;
```
然后,你就可以使用 DruidDataSource 提供的各种功能了,例如连接池监控、SQL 监控等。
需要注意的是,在使用 DruidDataSource 时,你需要在配置文件中添加 Druid 的一些配置选项,例如监控页面的访问路径、登录用户名和密码等。另外,DruidDataSource 也提供了更多的配置选项,你可以根据实际需要进行配置。
DruidDataSource 使用
DruidDataSource 是阿里巴巴开源的一个数据库连接池组件,用于管理和提供数据库连接。在使用 DruidDataSource 之前,需要先引入 Druid 的依赖包。
首先,在项目的 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>{druid_version}</version>
</dependency>
```
其中,`{druid_version}` 是 Druid 的版本号,可以根据需要进行替换。
接下来,在 Spring Boot 的配置文件(application.properties 或 application.yml)中配置 DruidDataSource 的相关属性,例如:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=db_username
spring.datasource.password=db_password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# DruidDataSource 相关配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,log4j
```
以上是一些常见的配置项,你可以根据实际需求进行调整。
最后,在需要使用数据库连接的地方,注入 DruidDataSource 对象即可进行数据库操作。例如,在 Spring Boot 的 Service 类中使用:
```java
@Service
public class UserService {
@Autowired
private DruidDataSource dataSource;
public void doSomething() {
// 使用 dataSource 进行数据库操作
}
}
```
这样就完成了 DruidDataSource 的使用。通过配置文件中的属性,可以调整连接池的大小、最大等待时间、连接验证等设置,从而满足具体项目的需求。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)