SpringBoot整合jasypt加密配置文件敏感信息

简介:大家好,我是程序员枫哥,🌟一线互联网的IT民工、📝资深面试官、🌹Java跳槽网创始人。拥有多年一线研发经验,曾就职过科大讯飞、美团网、平安等公司。在上海有自己小伙伴组建的副业团队,目前业余时间专注Java技术分享,春招/秋招/社招/跳槽,一对一学习辅助,项目接活开发。
🌈更多学习内容, 欢迎👏关注👀【文末】微信公众号:IT枫斗者
🌟🌟程序员找工作,就上Java跳槽网:www.javatiaocao.com

SpringBoot整合jasypt加密配置文件敏感信息

在项目中我们需要对配置文件的一些敏感信息进行加密处理,比如数据库账户密码,避免直接暴露出来,这种场景常常用于生产环境,我们不想让开发人员知道生产库的密码,有运维人员统一管理。

引入依赖

  • <dependency>
                <groupId>com.github.ulisesbocchio</groupId>
                <artifactId>jasypt-spring-boot-starter</artifactId>
                <version>3.0.4</version>
            </dependency>
    
    

加密工具类

  • public class JasyptUtils {
        public static void main(String[] args) {
            //这里假设我们的数据库密码是root
            String info = encrypt("root");
            //加密 TCFVL/wsN9AxelTDQyP/3g==
            System.out.println(info);
            //解密 root
            System.out.println(decrypt(info));
        }
    
        /**
         * 加密
         *
         * @param plaintext 明文
         * @return
         */
        public static String encrypt(String plaintext) {
            StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
            EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
            // 指定算法
            config.setAlgorithm("PBEWithMD5AndDES");
            // 指定秘钥,和yml配置文件中保持一致
            config.setPassword("llp");
            encryptor.setConfig(config);
            // 生成加密数据
            return encryptor.encrypt(plaintext);
        }
    
        /**
         * 解密
         *
         * @param data 加密后数据
         * @return
         */
        public static String decrypt(String data) {
            StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
            EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
            config.setAlgorithm("PBEWithMD5AndDES");
            config.setPassword("llp");
            encryptor.setConfig(config);
            // 解密数据
            return encryptor.decrypt(data);
        }
    }
    

启动类

  • @SpringBootApplication
    @MapperScan(basePackages = "com.llp.jasypt.mapper")
    public class JasyptApplication {
        public static void main(String[] args) {
            SpringApplication.run(JasyptApplication.class, args);
        }
    }
    

application.yml配置文件

  • 这里我们可以对需要加密的字段进行加密处理,比如url、username、password 或者说redis的一些账户信息等等。

  • jasypt:
      encryptor:
        # 加密的秘钥
    #    password: llp
        # 加密算法
        algorithm: PBEWithMD5AndDES
        iv-generator-classname: org.jasypt.iv.NoIvGenerator
        property:
          # 算法识别的前后缀,默认ENC(),数据库密文示例:password: "ENC(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)"
          prefix: Enc(
          suffix: )
    
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/llp?autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
        username: Enc(TCFVL/wsN9AxelTDQyP/3g==)
        password: Enc(TCFVL/wsN9AxelTDQyP/3g==)
    
    #mybatis配置
    mybatis:
      #指定要扫描的mapper.xml位置; classpath:mapper/*.xml 扫描在类路径下的mapper目录下的。xml文件
      mapper-locations: classpath:mapper/*.xml
      #配置类型别名,通常指定实体类所在包,这样我们在xml中resultType="com.llp.springboot.mybatis.bean.Monster"就可以简写成Monster
      type-aliases-package: com.llp.springboot.jasypt.entity
      #配置mybatis sql打印日志
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
        #启用自动驼峰配置
        map-underscore-to-camel-case: true
    
      #通过config-location可以指定mybatis-config.xml,这样就可以用传统的方式来配置mybatis
      #config-location: classpath:mybatis-config.xml
    

启动配置

  • 通常我们不会将password写在配置文件中,而是由运维人员进行统一管理,这样开发人员只能看到密文而不知道解密的秘钥

  • jasypt:
      encryptor:
        # 加密的秘钥
    #    password: llp
        # 加密算法
        algorithm: PBEWithMD5AndDES
        iv-generator-classname: org.jasypt.iv.NoIvGenerator
        property:
          # 算法识别的前后缀,默认ENC(),数据库密文示例:password: "ENC(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)"
          prefix: Enc(
          suffix: )
    
  • 如果打包后部署项目,可以使用如下命令在启动项目时指定秘钥:

  • #方式1:
    java -jar xxx.jar -Djasypt.encryptor.password=加密数据的秘钥
    
    #方式2:
    java -jar xxx.jar --jasypt.encryptor.password=加密数据的秘钥
    
  • idea中如何配置启动参数

  • -Djasypt.encryptor.password=llp
    
  • img

JasyptJava Simplified Encryption)是一个Java加密库,它可以用来加密/解密文本、哈希密码等。Spring Boot提供了对Jasypt的支持,可以在应用程序中轻松使用Jasypt加密敏感信息。下面是使用Jasypt对MySQL配置文件进行加密的步骤: 1. 引入Jasypt依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置加密 在application.properties或者application.yml中添加以下配置: ```properties jasypt.encryptor.password=your_password ``` 其中,your_password是用来加密敏感信息的密码。 3. 加密MySQL配置信息 在application.properties或者application.yml中加入MySQL配置信息,如下: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root ``` 将密码部分使用Jasypt进行加密,如下: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=ENC(加密后的密码) ``` 其中,加密后的密码可以使用Jasypt提供的加密工具进行加密。例如,我们可以使用以下命令生成加密后的密码: ``` java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password="your_password" algorithm=PBEWithMD5AndDES ``` 其中,jasypt-1.9.2.jar是Jasypt的jar包,your_password是用来加密敏感信息的密码。执行以上命令后,会输出加密后的密码。 4. 配置解密 在启动应用程序时,Spring Boot会自动解密加密敏感信息。因此,我们不需要任何额外的配置来解密MySQL密码。只需将加密后的密码放入配置文件即可。 至此,我们已经成功地使用Jasypt对MySQL配置文件进行了加密
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT枫斗者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值