简介:大家好,我是程序员枫哥,🌟一线互联网的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