数据库配置的通用做法是配置在配置文件中,譬如application.yml,application.properties中;但在有些情况下,开发人员并不想将数据库的配置信息放置在配置文件中,若未做脱敏处理,数据库的地址,用户名,密码等信息就会有暴露的风险
数据库配置在配置文件中
spring:
datasource:
url: jdbc:postgresql://localhost:5432/postgres?useSSL=false&stringtype=unspecified
username: postgres
password: ******
driver-class-name: org.postgresql.Driver
数据库配置在Configuration中
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.postgresql.Driver");
dataSourceBuilder.url("jdbc:postgresql://localhost:5432/postgres?useSSL=false&stringtype=unspecified");
dataSourceBuilder.username("postgres");
dataSourceBuilder.password("******");
return dataSourceBuilder.build();
}
}