Skip to content

Commit

Permalink
ras 加密
Browse files Browse the repository at this point in the history
  • Loading branch information
lslvxy committed May 24, 2022
1 parent 96a76e7 commit d638bab
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 30 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/laysan/autojob/config/DruidConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/laysan/autojob/core/utils/AESUtil.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}

/**
Expand All @@ -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;
}
}


}
4 changes: 2 additions & 2 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 15 additions & 13 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/laysan/autojub/test/SyncTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit d638bab

Please sign in to comment.