踩过的坑
jdk的版本也会影响jasypt的使用 使用jdk-8u152-linux-x64.tar.gz的时候会出现下面的问题 1.8.0_292这个版本实测没有问题 jdk-8u381-linux-x64.tar.gz这个版本实测也没有什么问题 这也就解释了为什么idea上面运行的时候没有什么问题,但是到服务器上面部署之后就会出现问题的原因
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.redis.password' to java.lang.String
at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:363) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:323) at org.springframework.boot.context.properties.bind.Binder.lambda$bindDataObject$4(Binder.java:447) at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:92) at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:80) at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:56) at org.springframework.boot.context.properties.bind.Binder.lambda$bindDataObject$5(Binder.java:451) at org.springframework.boot.context.properties.bind.Binder$Context.withIncreasedDepth(Binder.java:571) at org.springframework.boot.context.properties.bind.Binder$Context.withDataObject(Binder.java:557) at org.springframework.boot.context.properties.bind.Binder$Context.access$300(Binder.java:512) at org.springframework.boot.context.properties.bind.Binder.bindDataObject(Binder.java:449) at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:390) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:319) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:308) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:238) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:225) at org.springframework.boot.context.properties.ConfigurationPropertiesBinder.bind(ConfigurationPropertiesBinder.java:90) at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:89) ... 134 common frames omitted
通过yml读取系统环境变量的方式
解决在项目开源中可能暴露敏感信息的问题 这种方式不光可以避免在项目中暴露我们的用户名和密码,甚至可以避免暴露我们的服务器的ip地址
windows
右键此电脑
-> 属性
-> 高级系统设置
-> 环境变量
进入到环境变量配置界面
配置完系统的环境变量之后一定要重启idea或者是系统,让环境变量生效
远程版本
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${REMOTE_HOST:localhost}:3306/autumn_cloud?characterEncoding=utf-8&useSSL=false
username: ${REMOTE_MYSQL_USERNAME:root}
password: ${REMOTE_MYSQL_PASSWORD:root}
redis:
host: ${REMOTE_HOST:localhost}
port: 6379
password: ${REMOTE_REDIS_PASSWORD:root}
本地版本
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${LOCAL_HOST:localhost}:3306/autumn_cloud?characterEncoding=utf-8&useSSL=false
username: ${LOCAL_MYSQL_USERNAME:root}
password: ${LOCAL_MYSQL_PASSWORD:root}
redis:
host: ${LOCAL_HOST:localhost}
port: 6379
password: ${LOCAL_REDIS_PASSWORD:root}
linux里面设置对应的环境变量
同样设置完成后需要刷新,让设置的环境变量生效
source ~/etc/profile
source ~/.bashrc
source ~/.zashrc
REMOTE_HOST=xxx
REMOTE_MYSQL_USERNAME=xxx
REMOTE_MYSQL_PASSWORD=xxx
REMOTE_REDIS_PASSWORD=xxx
LOCAL_MYSQL_USERNAME=xxx
LOCAL_MYSQL_PASSWORD=xxx
LOCAL_HOST=xxx
LOCAL_REDIS_PASSWORD=xxx
方法通过jasypt加密工具来对用户名和密码进行加密
springboot配置文件中的配置项加密&jasypt的使用 这种方式只能加密我们的用户名和密码,不能对ip地址加密再读取,会报错
系统环境变量里面设置加密使用的密钥
设置完记得要重启idea或者是系统
JASYPT_PASS
export JASYPT_PASS=xxx
引入依赖
我测试使用的springboot的版本是2.3.6
<!-- jasypt-spring-boot-starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
获取加密后的密文
@Autowired
private StringEncryptor encryptor;
@Test
void test4(){
List<String> properties = new ArrayList<>();
properties.add("需要加密的用户名1");
properties.add("需要加密的密码1");
properties.add("需要加密的用户名2");
properties.add("需要解密的密码2");
properties.forEach(property -> {
System.out.println(property + " = " + encryptor.encrypt(property));
});
}
// 输出后的格式类似与这个样子
//xxxx = XC2ObTM80vSAsUNA5uTAjMp4OEg09wYpXsVXJS1xm62fZN3dcdocryBJpa6qbMTm
修改配置文件
# 读取系统的环境变量,加载jasypt的加解密密钥
jasypt:
encryptor:
password: ${JASYPT_PASS}
spring:
# 配置MySQL的datasource链接的信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://124.222.220.45:32768/account_book?serverTimezone=Asia/Shanghai
username: ENC(+UqVvXNR//wSI0Qp1xAh/PLrJKYjgnhAoyL5BAvngMvInTuxcijGEkaqtjjfUN8d)
password: ENC(vqUawoWuP0ai73FSET+uU4hQN8gELwUediXJ41S86h2zc6G09JV8VqkwdE/89tn0Ntt3uu8kjao8dfuFf4qtHg==)
# type: com.zaxxer.hikari.HikariDataSource
# 配置Redis的链接的信息
redis:
host: 124.222.220.45
port: 6379
password: ENC(XC2ObTM80vSAsUNA5uTAjMp4OEg09wYpXsVXJS1xm62fZN3dcdocryBJpa6qbMTm)