1
0
Переглянути джерело

fix: 邮件配置密码加密

chenjianhua 7 місяців тому
батько
коміт
4f7bfe4c9e

+ 17 - 4
service-manage/src/main/java/com/simuwang/manage/init/QuartzConfig.java

@@ -1,5 +1,7 @@
 package com.simuwang.manage.init;
 
+import cn.hutool.crypto.asymmetric.KeyType;
+import cn.hutool.crypto.asymmetric.RSA;
 import com.alibaba.fastjson2.JSON;
 import com.simuwang.base.common.enums.OpenStatusType;
 import com.simuwang.base.common.util.QuartzUtils;
@@ -38,6 +40,9 @@ public class QuartzConfig implements ApplicationRunner {
 
     @Value("${spring.task.groupName}")
     private String groupName;
+
+    @Autowired
+    private DaqProperties properties;
     private String JOB_CLASS="com.simuwang.manage.task.ParseSchedulerTask";
 
     private final Boolean enableQuartz;
@@ -49,9 +54,9 @@ public class QuartzConfig implements ApplicationRunner {
     @Override
     public void run(ApplicationArguments args){
         // 没有开启定时任务功能时直接退出
-        if (!enableQuartz) {
-            return;
-        }
+//        if (!enableQuartz) {
+//            return;
+//        }
         List<MailboxInfoDO> mailboxInfoDOS = emailConfigService.getAll();
         for(MailboxInfoDO mailboxInfoDO : mailboxInfoDOS){
             try{
@@ -64,7 +69,15 @@ public class QuartzConfig implements ApplicationRunner {
                 //请求参数
                 MailboxInfoDTO paramDTO = new MailboxInfoDTO();
                 paramDTO.setAccount(mailboxInfoDO.getEmail());
-                paramDTO.setPassword(mailboxInfoDO.getPassword());
+                String password = mailboxInfoDO.getPassword();
+                try{
+                    String publicKey = this.properties.getSecurityRsa().getPublicKey();
+                    String privateKey = this.properties.getSecurityRsa().getPrivateKey();
+                    password = new RSA(privateKey, publicKey).decryptStr(password, KeyType.PrivateKey);
+                }catch (Exception e){
+                    logger.error(e.getMessage(),e);
+                }
+                paramDTO.setPassword(password);
                 paramDTO.setPort(mailboxInfoDO.getPort());
                 paramDTO.setHost(mailboxInfoDO.getServer());
                 paramDTO.setProtocol(mailboxInfoDO.getProtocol());

+ 13 - 8
service-manage/src/main/java/com/simuwang/manage/service/impl/EmailConfigServiceImpl.java

@@ -98,7 +98,7 @@ public class EmailConfigServiceImpl implements EmailConfigService {
             //请求参数
             MailboxInfoDTO paramDTO = new MailboxInfoDTO();
             paramDTO.setAccount(infoDO.getEmail());
-            paramDTO.setPassword(infoDO.getPassword());
+            paramDTO.setPassword(getPassword(infoDO.getPassword()));
             paramDTO.setPort(infoDO.getPort());
             paramDTO.setHost(infoDO.getServer());
             paramDTO.setProtocol(infoDO.getProtocol());
@@ -108,19 +108,24 @@ public class EmailConfigServiceImpl implements EmailConfigService {
             logger.error(e.getMessage(),e);
         }
     }
-    @Override
-    public ResultVo connectTest(MailboxInfoVO mailboxInfoVO) {
-        ResultVo vo = new ResultVo(ResultCode.CONNECT_SUCCESS);
-        MailboxInfoDTO mailboxInfoDTO = new MailboxInfoDTO();
-        mailboxInfoDTO.setAccount(mailboxInfoVO.getEmail());
+
+    private String getPassword(String password){
         try{
             String publicKey = this.properties.getSecurityRsa().getPublicKey();
             String privateKey = this.properties.getSecurityRsa().getPrivateKey();
-            String password = new RSA(privateKey, publicKey).decryptStr(mailboxInfoVO.getPassword(), KeyType.PrivateKey);
-            mailboxInfoDTO.setPassword(password);
+            password = new RSA(privateKey, publicKey).decryptStr(password, KeyType.PrivateKey);
         }catch (Exception e){
             logger.error(e.getMessage(),e);
         }
+        return password;
+    }
+
+    @Override
+    public ResultVo connectTest(MailboxInfoVO mailboxInfoVO) {
+        ResultVo vo = new ResultVo(ResultCode.CONNECT_SUCCESS);
+        MailboxInfoDTO mailboxInfoDTO = new MailboxInfoDTO();
+        mailboxInfoDTO.setAccount(mailboxInfoVO.getEmail());
+        mailboxInfoDTO.setPassword(getPassword(mailboxInfoVO.getPassword()));
         mailboxInfoDTO.setPort(mailboxInfoVO.getPort());
         mailboxInfoDTO.setHost(mailboxInfoVO.getServer());
         mailboxInfoDTO.setProtocol(mailboxInfoVO.getProtocol());

+ 0 - 9
service-manage/src/main/java/com/simuwang/manage/task/ParseSchedulerTask.java

@@ -37,8 +37,6 @@ public class ParseSchedulerTask  extends QuartzJobBean {
     private EmailParseApiService emailParseApiService;
     @Autowired
     private EmailConfigService emailConfigService;
-    @Autowired
-    private DaqProperties properties;
     private static final Logger log = LoggerFactory.getLogger(ParseSchedulerTask.class);
     @Override
     protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
@@ -51,13 +49,6 @@ public class ParseSchedulerTask  extends QuartzJobBean {
         String password = jobDataMap.get("password").toString();
         MailboxInfoDTO paramDTO = new MailboxInfoDTO();
         paramDTO.setAccount(account);
-        try{
-            String publicKey = this.properties.getSecurityRsa().getPublicKey();
-            String privateKey = this.properties.getSecurityRsa().getPrivateKey();
-            password = new RSA(privateKey, publicKey).decryptStr(password, KeyType.PrivateKey);
-        }catch (Exception e){
-            log.error(e.getMessage(),e);
-        }
         paramDTO.setPassword(password);
         paramDTO.setPort(port);
         paramDTO.setHost(host);