22
33import com .zaxxer .hikari .HikariConfig ;
44import com .zaxxer .hikari .HikariDataSource ;
5+ import org .jasypt .util .text .StrongTextEncryptor ;
56import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .beans .factory .annotation .Value ;
68import org .springframework .boot .jdbc .DataSourceBuilder ;
79import org .springframework .context .annotation .Bean ;
810import org .springframework .context .annotation .Configuration ;
1416/**
1517 * {@link HikariDataSourceInit} use for build connection-pool base on HikariCp with customize properties,
1618 * and then added to {@link DataSourceConfiguration} class as datasource for use in app.
17- * also if you want use for production un comment line 30 and comment line 31 and 32
18- * and if you want test with testContainer uncomment line 31 and 32 and comment line 30
19+ * also if you want use for production uncomment line 32 and comment line 33 and 34
20+ * and if you want test with testContainer uncomment line 33 and 34 and comment line 32
21+ *
22+ * DataBase password encryption with JASYPT:
23+ * first of all use jasypt to encrypted db-password and added into application.properties file,
24+ * then uncomment line 34 (encryptorPassword)
25+ * at the end add --jasypt.encryptor.password={secret-key} in program environment
1926 */
2027
2128//todo i must be find better solution for use hikariDataSource between production and test with testContainer
@@ -26,6 +33,9 @@ public class HikariDataSourceInit{
2633 @ Autowired
2734 private Environment env ;
2835
36+ @ Value ("${jasypt.encryptor.password}" )
37+ String encryptorPassword ;
38+
2939 @ Bean (name = "HikariDataSourceInit" )
3040 public DataSource dataSource (){
3141 HikariConfig hikariConfig = new HikariConfig (hikariProperties ()); // use for production
@@ -40,21 +50,42 @@ public DataSource dataSource(){
4050 }
4151
4252 protected DataSource InitDataSource (){
53+ String passwordProperty = env .getProperty ("demo.datasource.password" );
54+ String plainPassword = "" ;
55+ if (passwordProperty .contains ("ENC(" )){
56+ plainPassword = decryptPassword (passwordProperty );
57+ }else {
58+ plainPassword = passwordProperty ;
59+ }
4360 return DataSourceBuilder .create ()
4461 .driverClassName (env .getProperty ("demo.datasource.driver-class-name" ))
4562 .url (env .getProperty ("demo.datasource.url" ))
4663 .username (env .getProperty ("demo.datasource.username" ))
47- .password (env . getProperty ( "demo.datasource.password" ) )
64+ .password (plainPassword )
4865 .build ();
4966 }
5067
5168 protected Properties hikariProperties (){
69+ String passwordProperty = env .getProperty ("hikari.dataSource.password" );
70+ String plainPassword = "" ;
71+ if (passwordProperty .contains ("ENC(" )){
72+ plainPassword = decryptPassword (passwordProperty );
73+ }else {
74+ plainPassword = passwordProperty ;
75+ }
5276 Properties hikariProps = new Properties ();
5377 hikariProps .setProperty ("dataSourceClassName" , env .getProperty ("hikari.dataSourceClassName" ));
5478 hikariProps .setProperty ("dataSource.user" , env .getProperty ("hikari.dataSource.user" ));
55- hikariProps .setProperty ("dataSource.password" , env . getProperty ( "hikari.dataSource.password" ) );
79+ hikariProps .setProperty ("dataSource.password" , plainPassword );
5680 hikariProps .setProperty ("dataSource.databaseName" , env .getProperty ("hikari.dataSource.databaseName" ));
5781 return hikariProps ;
5882 }
5983
84+ @ Bean
85+ protected String decryptPassword (String value ) {
86+ var textEncryptor = new StrongTextEncryptor ();
87+ textEncryptor .setPassword (encryptorPassword );
88+ return textEncryptor .decrypt (value .substring (4 , value .length () - 1 ));
89+ }
90+
6091}
0 commit comments