赞
踩
方式一: 1、spring.datasource配置 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/paypalt?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root type: com.alibaba.druid.pool.DruidDataSource druid: # 连接池的配置信息 # 初始化时建立物理连接的个数 initial-size: 3 # 连接池最小连接数 min-idle: 3 # 连接池最大连接数 max-active: 20 # 获取连接时最大等待时间,单位毫秒 max-wait: 60000 # 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 test-while-idle: true # 既作为检测的间隔时间又作为testWhileIdel执行的依据 time-between-connect-error-millis: 60000 # 销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接 min-evictable-idle-time-millis: 30000 # 用来检测连接是否有效的sql 必须是一个查询语句 # mysql中为 select 'x' # oracle中为 select 1 from dual validation-query: select 'x' # 申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true test-on-borrow: false # 归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true test-on-return: false # 是否缓存preparedStatement,mysql5.5+建议开启 pool-prepared-statements: true # 当值大于0时poolPreparedStatements会自动修改为true max-pool-prepared-statement-per-connection-size: 20 # 合并多个DruidDataSource的监控数据 use-global-data-source-stat: false # 配置扩展插件 filters: stat,wall,slf4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 定时输出统计信息到日志中,并每次输出日志会导致清零(reset)连接池相关的计数器。 time-between-log-stats-millis: 300000 #配置DruidStatFilter web-stat-filter: enabled: true url-pattern: '/*' exclusions: '*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*' # 配置DruidStatViewServlet stat-view-servlet: # 是否启用StatViewServlet(监控页面)默认值为false(考虑到安全问题默认并未启动,如需启用建议设置密码或白名单以保障安全) enabled: true url-pattern: '/druid/*' #IP白名单(没有配置或者为空,则允许所有访问) allow: 127.0.0.1,192.168.0.1 # IP黑名单 (存在共同时,deny优先于allow) deny: 192.168.0.128 # 禁用HTML页面上的“Reset All”功能 reset-enable: false # 登录名 login-username: admin # 登录密码 login-password: 123456
2、进行数据库明文加密
java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password
如下加密:
privateKey:MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAh12hnaZuMe76Yb4pi7ogSAEMOcavmz7Blo8DYxeipxeZQhnrXngxc0gAQ6ORlofLWtDm6S7bI7wfDT2EFy/2DwIDAQABAkABMRjYK3vy4pi/vY3eFhBssd2qsI4hPsczjSTJfY7IC9Dc1f7g0axTM6Cx68tRUwv0rSnUiJ5EcDEhuD0JusSZAiEAwX1HpCTq8QgBV1WriHQC7Cd/9Qqp1V4yJeA/jdvXhbsCIQCzGS6wdTQCXDZKLvjRLeSUyTmmIqV/wckqdnpMUZ2BvQIgBIamr1tBt6OlTGKvoYB9NQLzhkrakCgk6ifltK7IytMCIBIbf67zipiafhqt+RYdD7lDRwLXCeiKzS3v4JmKvuP5AiEAr+zqD6sdXv7rWjqu50n+LXbWtNP/M4JzzO1mJOHEhoE= publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIddoZ2mbjHu+mG+KYu6IEgBDDnGr5s+wZaPA2MXoqcXmUIZ6154MXNIAEOjkZaHy1rQ5uku2yO8Hw09hBcv9g8CAwEAAQ== password:Y464AerH8tabxQg5DlkUej6gQ64KY73ahgiPyaB0vguLBLjUEEkVu6VBueiXxcnMfVjh1Nbd+lJNUTnS1a3/xg==
配置密码:
privateKey:MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAh12hnaZuMe76Yb4pi7ogSAEMOcavmz7Blo8DYxeipxeZQhnrXngxc0gAQ6ORlofLWtDm6S7bI7wfDT2EFy/2DwIDAQABAkABMRjYK3vy4pi/vY3eFhBssd2qsI4hPsczjSTJfY7IC9Dc1f7g0axTM6Cx68tRUwv0rSnUiJ5EcDEhuD0JusSZAiEAwX1HpCTq8QgBV1WriHQC7Cd/9Qqp1V4yJeA/jdvXhbsCIQCzGS6wdTQCXDZKLvjRLeSUyTmmIqV/wckqdnpMUZ2BvQIgBIamr1tBt6OlTGKvoYB9NQLzhkrakCgk6ifltK7IytMCIBIbf67zipiafhqt+RYdD7lDRwLXCeiKzS3v4JmKvuP5AiEAr+zqD6sdXv7rWjqu50n+LXbWtNP/M4JzzO1mJOHEhoE= publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIddoZ2mbjHu+mG+KYu6IEgBDDnGr5s+wZaPA2MXoqcXmUIZ6154MXNIAEOjkZaHy1rQ5uku2yO8Hw09hBcv9g8CAwEAAQ== password:Y464AerH8tabxQg5DlkUej6gQ64KY73ahgiPyaB0vguLBLjUEEkVu6VBueiXxcnMfVjh1Nbd+lJNUTnS1a3/xg==
访问本地Druid http://localhost:8080/druid/
方式二:
1、pom.xml引入druid包
2、com.alibaba.druid.filter.config.ConfigTools生产秘钥
public final class DruidEncryptorUtils {
private static String privateKey;
private static String publicKey;
static {
try {
String[] keyPair = ConfigTools.genKeyPair(512);
privateKey = keyPair[0];
System.out.println(String.format("privateKey-->%s",privateKey));
publicKey = keyPair[1];
System.out.println(String.format("publicKey-->%s",publicKey));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
@SneakyThrows
public static String encode(String plaintext){
System.out.println("明文字符串:" + plaintext);
String ciphertext = ConfigTools.encrypt(privateKey,plaintext);
System.out.println("加密后字符串:" + ciphertext);
return ciphertext;
}
@SneakyThrows
public static String decode(String ciphertext){
System.out.println("加密字符串:" + ciphertext);
String plaintext = ConfigTools.decrypt(publicKey,ciphertext);
System.out.println("解密后的字符串:" + plaintext);
return plaintext;
}
3、修改数据链接datasourec配置文件
3.1、filter开启config 、配置connectionProperties
filter:
config: enabled: true
connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}(公钥秘钥)
3.2、数据库配置模版
pring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: ${DATASOURCE_URL:jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai}
username: ${DATASOURCE_USERNAME:root}
password: ${DATASOURCE_PWD:MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDgip8B3yVQxTyt==}
publickey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4IqfAd8lUMU8rfNsvCCG+ejqAjRWdQSCrWdBlB7D1phrgnMv1QdG4MsCFAR7yf4gCKjSN4Ql9BRfY5zDJgl7
druid:
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
filter:
config:
enabled: true
connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}
方式三:jasypt加密
1、pom.xml引入jasypt包
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
</dependency>
2、jasypt加密工具类
public final class JasyptEncryptorUtils {
private static final String salt = "platfrom";
private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
static {
basicTextEncryptor.setPassword(salt);
}
private JasyptEncryptorUtils(){}
public static String encode(String plaintext){
System.out.println("明文字符串:" + plaintext);
String ciphertext = basicTextEncryptor.encrypt(plaintext);
System.out.println("加密后字符串:" + ciphertext);
return ciphertext;
}
public static String decode(String ciphertext){
System.out.println("加密字符串:" + ciphertext);
ciphertext = "ENC(" + ciphertext + ")";
if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)){
String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext,basicTextEncryptor);
System.out.println("解密后的字符串:" + plaintext);
return plaintext;
}
System.out.println("解密失败");
return "";
}
}
3、数据库配置
jasypt:
encryptor:
password: root
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
4、数据库配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: ENC(TiGjk8ijFXoyI+LTqnckIfe6ogrlWwSd4xPp5MRyOvv7W+JeFWax9qT50hmNotK/qb6Z7+ip3+c6TXXt/YeyckM/7hDHXv+L9mXxgC+NEWE3OUnSeWYhJ2yMyy4eN4d1D4U+dAr7lLFCCGf8nRLEujV/Ii8ayPCi4RMgLlp8+yw=)
username: ENC(GzUxN13/FoeU9uwRI4iylA==)
password: ENC(p32urgxaD7/ztSfJhF4JAQ==)
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 连接池的配置信息
# 初始化时建立物理连接的个数
initial-size: 3
# 连接池最小连接数
min-idle: 3
# 连接池最大连接数
max-active: 20
# 获取连接时最大等待时间,单位毫秒
max-wait: 60000
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
test-while-idle: true
# 既作为检测的间隔时间又作为testWhileIdel执行的依据
time-between-connect-error-millis: 60000
# 销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接
min-evictable-idle-time-millis: 30000
jasypt:
encryptor:
password: platfrom
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
5、pom.xml jar包设置
5.1、idea/clipse 默认从maven下载 无法下载阿里maven仓库jasypt-spring-boot-starter包
5.2、方法一:打开 maven 的配置文件( windows 机器一般在 maven 安装目录的 conf/settings.xml ),在<mirrors></mirrors>标签中添加 mirror 子节点:
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
如果想使用其它代理仓库,可在<repositories></repositories>节点中加入对应的仓库使用地址。以使用 spring 代理仓为例:
<repository>
<id>spring</id>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
在你的 pom.xml 文件<denpendencies></denpendencies>节点中加入你要引用的文件信息:
<dependency>
<groupId>[GROUP_ID]</groupId>
<artifactId>[ARTIFACT_ID]</artifactId>
<version>[VERSION]</version>
</dependency>
执行拉取命令:
mvn install
5.2、方法二:直接阿里云开发者社区-云计算社区-阿里云下载jar到本地maven仓库Jasypt相关包都需下载
手动加入到idea项目里面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。