From d638babc370113d913167eedc517ba95ca8bfe7a Mon Sep 17 00:00:00 2001 From: lishan Date: Tue, 24 May 2022 18:37:02 +0800 Subject: [PATCH] =?UTF-8?q?ras=20=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +++ .../laysan/autojob/config/DruidConfig.java | 19 ++++++++++ .../autojob/core/helper/ServiceTemplate.java | 2 +- .../laysan/autojob/core/utils/AESUtil.java | 38 ++++++++++++++----- src/main/resources/application-dev.yml | 4 +- src/main/resources/application-local.yml | 7 ++++ src/main/resources/application-prod.yml | 6 +-- src/main/resources/application.yml | 28 +++++++------- .../com/laysan/autojub/test/SyncTests.java | 4 +- 9 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/laysan/autojob/config/DruidConfig.java create mode 100755 src/main/resources/application-local.yml diff --git a/pom.xml b/pom.xml index 9490dd7..cb98e3d 100755 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,11 @@ mysql mysql-connector-java + + com.alibaba + druid-spring-boot-starter + 1.2.9 + org.projectlombok lombok diff --git a/src/main/java/com/laysan/autojob/config/DruidConfig.java b/src/main/java/com/laysan/autojob/config/DruidConfig.java new file mode 100644 index 0000000..700dbde --- /dev/null +++ b/src/main/java/com/laysan/autojob/config/DruidConfig.java @@ -0,0 +1,19 @@ +package com.laysan.autojob.config; + +import com.alibaba.druid.pool.DruidDataSource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; + +@Configuration +@Slf4j +public class DruidConfig { + @Bean + @ConfigurationProperties(prefix = "spring.datasource") + public DataSource druidDataSource() { + return new DruidDataSource(); + } +} \ No newline at end of file diff --git a/src/main/java/com/laysan/autojob/core/helper/ServiceTemplate.java b/src/main/java/com/laysan/autojob/core/helper/ServiceTemplate.java index 36c2c87..7e1f2c4 100644 --- a/src/main/java/com/laysan/autojob/core/helper/ServiceTemplate.java +++ b/src/main/java/com/laysan/autojob/core/helper/ServiceTemplate.java @@ -41,7 +41,7 @@ public static void execute(AccountType accountType, Account account, ServiceTemp taskLog.setSucceed(0); } catch (Exception e) { AutojobContextHolder.get().setDetailMessage("定时任务执行失败"); - LogUtils.error(log, accountType, account.getAccount(), "定时任务执行失败"); + LogUtils.error(log, accountType, account.getAccount(), "定时任务执行失败,{}", e); account.setTodayExecuted(0); taskLog.setSucceed(0); } finally { diff --git a/src/main/java/com/laysan/autojob/core/utils/AESUtil.java b/src/main/java/com/laysan/autojob/core/utils/AESUtil.java index 011ab88..ade9e5f 100755 --- a/src/main/java/com/laysan/autojob/core/utils/AESUtil.java +++ b/src/main/java/com/laysan/autojob/core/utils/AESUtil.java @@ -1,19 +1,34 @@ - package com.laysan.autojob.core.utils; import cn.hutool.core.codec.Base64; import cn.hutool.crypto.SecureUtil; +import cn.hutool.crypto.asymmetric.KeyType; +import cn.hutool.crypto.asymmetric.RSA; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; + /** * @author lise * @version AESUtil.java, v 0.1 2020年11月27日 17:41 lise */ @Component +@Slf4j public class AESUtil { - @Value("${autojob.aes_key}") - public String key; + @Value("${autojob.rsa.public_key}") + private String publicKeyBase64 = ""; + @Value("${autojob.rsa.private_key}") + private String privateKeyBase64 = ""; + private RSA rsa; + + @PostConstruct + public void init() { + byte[] publicKey = Base64.decode(publicKeyBase64); + byte[] privateKey = Base64.decode(privateKeyBase64); + rsa = SecureUtil.rsa(privateKey, publicKey); + } /** * AES @@ -22,8 +37,11 @@ public class AESUtil { * @return */ public String encrypt(String message) { - byte[] keyByte = Base64.decode(this.key); - return SecureUtil.aes(keyByte).encryptBase64(message); + try { + return rsa.encryptBase64(message, KeyType.PublicKey); + } catch (Exception e) { + return null; + } } /** @@ -32,10 +50,10 @@ public String encrypt(String message) { * @return */ public String decrypt(String encrypted) { - byte[] keyByte = Base64.decode(this.key); - byte[] decode = Base64.decode(encrypted); - return SecureUtil.aes(keyByte).decryptStr(decode); + try { + return rsa.decryptStr(encrypted, KeyType.PrivateKey); + } catch (Exception e) { + return null; + } } - - } \ No newline at end of file diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index ba5c97a..bb962ab 100755 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://47.100.3.180:51306/autojob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true - username: autojob + url: jdbc:mysql://127.0.0.1:51306/autojob?characterEncoding=utf-8&allowMultiQueries=true&&useAffectedRows=true&useSSL=false + username: root password: ${AUTOJOB_MYSQL_PASSWORD} jpa: show-sql: true \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml new file mode 100755 index 0000000..dfde2ca --- /dev/null +++ b/src/main/resources/application-local.yml @@ -0,0 +1,7 @@ +spring: + datasource: + url: jdbc:mysql://127.0.0.1:3306/autojob?characterEncoding=utf-8&allowMultiQueries=true&&useAffectedRows=true&useSSL=false + username: root + password: 123456 + jpa: + show-sql: true \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 2a6f8c9..ccc9baa 100755 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://rm-uf6f7nf8y9vih1oqk125010.mysql.rds.aliyuncs.com:3306/autojob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true - username: lise - password: lise@1234 + url: jdbc:mysql://127.0.0.1:30236/autojob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true + username: root + password: 123456 jpa: show-sql: true \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a9f197a..957ce86 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,18 +7,18 @@ spring: active: dev datasource: driver-class-name: com.mysql.cj.jdbc.Driver - hikari: - connection-timeout: 10000 - validation-timeout: 3000 - idle-timeout: 60000 - login-timeout: 5 - max-lifetime: 60000 - maximum-pool-size: 10 - minimum-idle: 5 - read-only: false - auto-commit: true - pool-name: HikariCP - connection-test-query: select 1 + type: com.alibaba.druid.pool.DruidDataSource + druid: + maxActive: 20 + initialSize: 1 + minIdle: 3 + maxWait: 60000 + timeBetweenEvictionRunsMillis: 60000 + minEvictableIdleTimeMillis: 300000 + testWhileIdle: true + testOnBorrow: false + testOnReturn: false + validationQuery: SELECT 1 jpa: hibernate: ddl-auto: update @@ -71,7 +71,9 @@ server: port: 8080 autojob: - aes_key: ${AUTOJOB_ENCRYPT_KEY} + rsa: + public_key: ${AUTOJOB_RSA_PUBLIC_KEY} + private_key: ${AUTOJOB_RSA_PRIVATE_KEY} ok: http: diff --git a/src/test/java/com/laysan/autojub/test/SyncTests.java b/src/test/java/com/laysan/autojub/test/SyncTests.java index f9143fd..59b4aac 100644 --- a/src/test/java/com/laysan/autojub/test/SyncTests.java +++ b/src/test/java/com/laysan/autojub/test/SyncTests.java @@ -9,8 +9,10 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; @SpringBootTest(classes = AutojobApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("local") class SyncTests { @Autowired @@ -27,7 +29,7 @@ class SyncTests { @Test void comm() throws Exception { //messageService.sendMessage("111111", "xxx", "ooo"); - System.out.println(aesUtil.decrypt("XBbmxoCDlA5sxMXhPmw1Fg==")); + System.out.println(aesUtil.decrypt("nOtu8RwWB8ECpBeWlCtT3geEBIfNkKtVeQoQxFVcOfj6ywM2vutgH8SZB431xapxipGv4g2ERuxSrUeLwmNvswH3mn9t/aARJPwPUrSLxQNeaUS0g1eAktnu351SjcRZspNc6jSF2GVMY1navDjmXyzOKm/rfDghbOLxL5/nN38=")); // Account account = accountService.findById(1L); // cloud189RunService.run(account, false);