当前位置: 代码网 > it编程>编程语言>Java > SpringBoot整合jasypt加密配置文件敏感信息

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

2024年06月12日 Java 我要评论
springboot整合jasypt加密配置文件敏感信息在项目中我们需要对配置文件的一些敏感信息进行加密处理,比如数据库账户密码,避免直接暴露出来,这种场景常常用于生产环境,我们不想让开发人员知道生产

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

到此这篇关于springboot整合jasypt加密配置文件敏感信息的文章就介绍到这了,更多相关springboot jasypt加密敏感信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com