一、Druid加密演算法
在使用Druid連接池時,需要將資料庫連接密碼配置到Druid數據源中。為了保證密碼安全,Druid提供了加密功能。Druid和JdbcUrl連接字元串中都可以使用相同的加密方式。Druid使用的是基於口令的對稱加密演算法(AES演算法)。
下面是Druid加密演算法的示例代碼:
// 加密 public static String encrypt(String password, String key) throws Exception { byte[] keyBytes = key.getBytes(); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] bytes = password.getBytes(); byte[] encrypted = cipher.doFinal(bytes); return new String(Base64.getEncoder().encode(encrypted)); } // 解密 public static String decrypt(String password, String key) throws Exception { byte[] keyBytes = key.getBytes(); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] encrypted = Base64.getDecoder().decode(password); byte[] decrypted = cipher.doFinal(encrypted); return new String(decrypted); }
二、Druid加密資料庫密碼
Druid可以通過配置文件方式加密資料庫密碼,在配置文件中將密碼加密後配置即可。
下面是使用Druid加密資料庫密碼的yml配置示例:
spring: datasource: druid: username: root password: '${password}' url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai driverClassName: com.mysql.jdbc.Driver filter: - stat - wall connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 connectionInitSqls: - set names utf8mb4; encrypt: type: druid key: 123456 password: aWVmMjI4MjNhODBiMDdiMg==
三、Druid加密密碼
Druid也可以在代碼中使用加密方式,在獲取數據源時將加密後的密碼傳入即可。下面是使用Druid加密密碼的示例代碼:
DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); String password = "123456"; password = ConfigTools.encrypt(password); dataSource.setPassword(password);
四、Druid加密命令
Druid提供了encrypt和decrypt命令,可以用於加密、解密密碼。在使用命令時需要指定加密方式、密碼和加密密碼。
下面是Druid加密解密命令的示例:
java -cp druid-1.1.7.jar com.alibaba.druid.filter.config.ConfigTools password 123456 password = 2679fe72e4f8b16526d115be594af9d1 java -cp druid-1.1.7.jar com.alibaba.druid.filter.config.ConfigTools decrypt 2679fe72e4f8b16526d115be594af9d1 password = 123456
五、Druid加密配置
Druid可以通過配置文件來設置加密方式。在配置文件中需要指定加密方式和密鑰。
下面是使用Druid加密配置的yml配置示例:
spring: datasource: druid: username: root password: '${password}' url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai driverClassName: com.mysql.jdbc.Driver filter: - stat - wall connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 connectionInitSqls: - set names utf8mb4; encrypt: type: druid key: 123456
六、Druid加密yml配置
在Druid的配置文件中也可以使用加密方式,將加密後的密碼配置到yml文件中即可。
下面是使用Druid加密yml配置的示例:
spring: datasource: druid: username: root password: ENC(aWVmMjI4MjNhODBiMDdiMg==) url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai driverClassName: com.mysql.jdbc.Driver filter: - stat - wall connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 connectionInitSqls: - set names utf8mb4;
七、Druid加密解密
Druid提供了ConfigTools工具類可以用於加密、解密密碼。
下面是使用Druid加密解密的示例:
// 加密 String password = "123456"; String encryptPassword = ConfigTools.encrypt(password); // 解密 String decryptPassword = ConfigTools.decrypt(encryptPassword);
以上就是Druid加密的詳細闡述,包括Druid加密演算法、Druid加密資料庫密碼、Druid加密密碼、Druid加密命令、Druid加密配置、Druid加密yml配置、Druid加密解密等方面。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190752.html