当前位置:   article > 正文

数据库配置加密(自定义加密方式)

数据库配置加密(自定义加密方式)

1. 首先,自己编写加密工具类,我这里使用的是国密(免得其他地方有要求),并使用hutool工具,需要应入pom

  1. package com.banyoyo.epdb.utils;
  2. import cn.hutool.core.util.CharsetUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.crypto.symmetric.SymmetricCrypto;
  5. /**
  6. * @ClassName SM4Utils
  7. * @Description TODO
  8. * @Author Banyoyo
  9. * @Date 2024/4/18 9:50
  10. */
  11. public class SM4Utils {
  12. //key必须是16字节,即128位
  13. final static String key = "abcdefghabcdefgh";
  14. //指明加密算法和秘钥
  15. static SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
  16. /**
  17. * 加密为16进制,也可以加密成base64/字节数组
  18. *
  19. * @param plaintext
  20. * @return
  21. */
  22. public static String encryptSm4(String plaintext) {
  23. if (StrUtil.isBlank(plaintext)) {
  24. return "";
  25. }
  26. return sm4.encryptHex(plaintext, CharsetUtil.CHARSET_UTF_8);
  27. }
  28. /**
  29. * 解密
  30. *
  31. * @param ciphertext
  32. * @return
  33. */
  34. public static String decryptSm4(String ciphertext) {
  35. if (StrUtil.isBlank(ciphertext)) {
  36. return "";
  37. }
  38. return sm4.decryptStr(ciphertext,CharsetUtil.CHARSET_UTF_8);
  39. }
  40. public static void main(String[] args) {
  41. String url="jdbc:mysql://localhost:3306/ep?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true";
  42. String username="root";
  43. String password="123456";
  44. String urlstr = SM4Utils.encryptSm4(url);
  45. String usernamestr = SM4Utils.encryptSm4(username);
  46. String passwordstr = SM4Utils.encryptSm4(password);
  47. System.out.println("加密后的urlstr字符串为"+urlstr);
  48. System.out.println("加密后的usernamestr字符串为"+usernamestr);
  49. System.out.println("加密后的passwordstr字符串为"+passwordstr);
  50. System.out.println("解密后的urlstr字符串为"+SM4Utils.decryptSm4(urlstr));
  51. System.out.println("解密后的usernamestr字符串为"+SM4Utils.decryptSm4(usernamestr));
  52. System.out.println("解密后的passwordstr字符串为"+SM4Utils.decryptSm4(passwordstr));
  53. }
  54. }

打印结果: 

加密后的urlstr字符串为55cefb31b2a805c71443be1a7e0c5b01aa899256121fdd1af5f098f1c1d8ed4bd0aa7fabb91bb9e204596e2522b1aa87486cc60fd45dc64f2d8b01adb3df80e459807cbce9b50eb68ddccc8af042dde5aa39ca7398e97efb662498c641a788809d7efd6414314db6dab13609eb473384f3e15a53efeff4e0c7d1707a0ac2a967497f93f37dc229ebce4bf8963cd91a631ed8218197be5654fce709d7b6fe25cb
加密后的usernamestr字符串为c9fd389f247df4eacaff9693735ac1c5
加密后的passwordstr字符串为951fd163e9d0894fec3d1dac43a2deb8
解密后的urlstr字符串为jdbc:mysql://localhost:3306/ep?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
解密后的usernamestr字符串为root
解密后的passwordstr字符串为123456

2.读取配置,并且解密返回

        数据库配置在spring.datasource下还是spring.datasource.druid下都能读取得到

  1. package com.banyoyo.epdb.config.dataSource;
  2. import cn.hutool.db.ds.DataSourceWrapper;
  3. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper;
  4. import com.ambition.epdb.utils.SM4Utils;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. /**
  9. * @ClassName DataSourceConfig
  10. * @Description TODO
  11. * @Author banyoyo
  12. * @Date 2024/4/16 16:21 DataSourceProperties
  13. */
  14. @Configuration
  15. public class DruidDataSourceConfig extends DruidDataSourceWrapper {
  16. @Bean(value = "druidDataSource")
  17. public void druidDataSource() {
  18. String url = getUrl();
  19. System.out.println("Druid: "+url);
  20. setUrl(SM4Utils.decryptSm4(url));
  21. String username = getUsername();
  22. System.out.println("Druid: "+username);
  23. setUsername(SM4Utils.decryptSm4(username));
  24. String password = getPassword();
  25. System.out.println("Druid: "+password);
  26. setPassword(SM4Utils.decryptSm4(password));
  27. }
  28. }

3.dynamic-datasource多数据源(主要还是继承DynamicDataSourceProperties )

  1. @Configuration
  2. @Qualifier("myDataSourceConfig")
  3. @Primary
  4. public class DataSourceConfig extends DynamicDataSourceProperties {
  5. @Autowired
  6. private Environment env;
  7. @Bean
  8. public DynamicDataSourceProvider handleData() throws Exception{
  9. Map<String, DataSourceProperty> datasource = getDatasource();
  10. DataSourceProperty dbone= datasource.get("dbone");
  11. String username = env.getProperty("spring.datasource.dynamic.datasource.dbone.username");
  12. String password = env.getProperty("spring.datasource.dynamic.datasource.dbone.password");
  13. String key= env.getProperty("key");
  14. dbone.setUsername(SymmetricEncoderUtils.decrypt(username, key));
  15. dbone.setPassword(SymmetricEncoderUtils.decrypt(password, key));
  16. datasource.put("dbone",dbone);
  17. return new YmlDynamicDataSourceProvider(datasource);
  18. }
  19. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/451509
推荐阅读
相关标签
  

闽ICP备14008679号