• 给SpringBoot连接信息上把锁


    代码千万行,安全第一行;密码明文存,同事两行泪 --摘自 技术最前线一遍文章标题 具体内容 点我快速进入

    最近又发生了勒索病毒。

    file

    中勒索病毒原因:

    file

    受攻击的账户密码都是以明文形式存储的。下面这位hacker是不是很帅。

    file

     最近刚弄完一个项目,结果我的数据库配置文件是明文的,被leader看到了 就被骂了一顿。说如果hacker直接拉去了你的配置文件 根本不用破解直接把你数据库数据就给拉走了。
     我翻了好几个项目貌似都没有加密 直接配置文件就是下面。
    
    一个博客系统的配置文件:
    

    file

    一个app项目的配置文件:
    

    file

     我的后背感觉一阵凉意。。。。搬了好几年的砖,感觉自己写的代码在裸奔。
    

    file

    翻阅了很多博客我总结了一下SpringBoot的yml文件的数据库密码的配置:

    pom文件添加一下依赖:pom文件github地址:https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter

    file

     代码如下:
    <!-- yml 文件加密 https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
            <dependency>
                <groupId>com.github.ulisesbocchio</groupId>
                <artifactId>jasypt-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    

    直接复制过来一个工具类: 加密: file 解密: file 第一次执行结果: file 第二次执行结果: file 。。。。

    有木有发现每一次执行的加密的串都不一样。 加密方式:PBEWithMD5AndDES 加密方式解释为:

    file

    反正上图我是没看dong。也翻阅了很多资料。工作模式填充模式就把我弄蒙了。不过我现在还在看,你们会的可以给我讲一下。

     代码如下:
     package cn.cnbuilder.utils;
    
    import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
    import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
    import org.junit.jupiter.api.Test;
    
    
    public class YmlEncryptionUtil {
       /**
        * 加密串
        *
        * @param key 秘钥
        * @param str 要加密的字符串
        * @throws Exception
        */
       public static String encryption(String key, String str) throws Exception {
           StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
           EnvironmentPBEConfig config = new EnvironmentPBEConfig();
           //加密算法
           config.setAlgorithm("PBEWithMD5AndDES");
           // 加密的密钥
           config.setPassword(key);
           standardPBEStringEncryptor.setConfig(config);
           //加密串
           String encryptedText = standardPBEStringEncryptor.encrypt(str);
           return encryptedText;
       }
    
       /**
        * 解密工具类
        * @param encryptedStr 要解密的字符串
        * @param key 秘钥
        * @throws Exception
        */
       public static  String Decrypt(String encryptedStr, String key) throws Exception {
           StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
           EnvironmentPBEConfig config = new EnvironmentPBEConfig();
           //加密模式
           config.setAlgorithm("PBEWithMD5AndDES");
           //加密秘钥
           config.setPassword(key);
           standardPBEStringEncryptor.setConfig(config);
           //密文
           String str = standardPBEStringEncryptor.decrypt(encryptedStr);
           return str;
       }
    
       public static void main(String[] args) throws Exception {
           String encryptedStr = encryption("kingYiFan", "blog:www.cnbuilder.cn");
           System.out.println(encryptedStr);
           String str = Decrypt(encryptedStr, "kingYiFan");
           System.out.println(str);
       }
    }
    
     把以上工具类直接copy到project中,然后直接拿着数据库的连接方式生成加密串:
     然后填充到yml文件配置文件中 ENC(密文)
    

    具体如下图:

    file

    然后在yml文件中加入key

    file

      代码如下:
      jasypt:
          encryptor:
                password: KingYiFan
    
            修改完配置文件,重新启动项目就好了。有没有人一种感觉被我搞蒙了。如果这样的话,加密key都有,hacker直接就拿着工具类解密了。
    我也有这种感觉。
    
            我有一个想法就是在启动jar包的时候用命令给赋key(秘钥)
    命令如下(自行测试哈): java -jar jar包名称 --jasypt.encryptor.password: kingYiFan
    

    五一假期第一个工作日:感觉一点精神都没有好累好累。好想睡觉。这个五一练了三天科二,每天早6晚9 感觉整个人都不一样了。迷迷糊糊把这篇文章给写完的。

    有什么问题可以联系一下我。

    鼓励作者写出更好的技术文档,就请我喝一瓶哇哈哈哈哈哈哈哈。。你们的赞助决定我更新的速度哦!

    下一篇文章我给大家分享一下科目二的技巧以及考试要素。

    微信:

    支付宝:


    感谢一路支持我的人。。。。。
    
    Love me and hold me
    QQ:69673804(16年老号)
    EMAIL:itw@tom.com
    友链交换
    如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。
    

    网站名称:KingYiFan’S Blog
    网站地址:http://blog.cnbuilder.cn
    网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
    网站Logo/头像: [头像地址](https://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg)
    
  • 相关阅读:
    jdbc配置Spring
    zend studio报错
    phpStudy 5.5n +zendstudio12.5+xDebugger的配置
    一个关于finally和return的面试题
    进制详解
    设计模式--桥接(Bridge)模式
    Struts2中文件上传下载实例
    java int and Integer
    java面试题
    Python __slots__的使用
  • 原文地址:https://www.cnblogs.com/kingyifan/p/11721440.html
Copyright © 2020-2023  润新知