diff --git a/src/main/java/au/com/royalpay/payment/manage/notice/core/impls/MailServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/notice/core/impls/MailServiceImp.java index 6e9e7e7d9..63a1ed1db 100644 --- a/src/main/java/au/com/royalpay/payment/manage/notice/core/impls/MailServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/notice/core/impls/MailServiceImp.java @@ -4,6 +4,8 @@ import au.com.royalpay.payment.manage.mappers.system.MailUnsubMapper; import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.notice.beans.NoticeBean; import au.com.royalpay.payment.manage.notice.core.MailService; +import au.com.royalpay.payment.tools.codec.AESCrypt; +import au.com.royalpay.payment.tools.env.SysConfigManager; import au.com.royalpay.payment.tools.exceptions.BadRequestException; import au.com.royalpay.payment.tools.exceptions.NotFoundException; import au.com.royalpay.payment.tools.exceptions.ServerErrorException; @@ -18,6 +20,8 @@ import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.net.util.Base64; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -26,10 +30,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMethod; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.security.Key; import java.util.ArrayList; import java.util.List; @@ -53,7 +60,11 @@ public class MailServiceImp implements MailService { @Resource private MailUnsubMapper mailUnsubMapper; @Resource + private SysConfigManager sysConfigManager; + @Resource private ClientManager clientManager; + @Value("${app.settle.aes-key}") + private String settleAESKey; /* @Override public void sendEmail(NoticeBean noticeBean) { @@ -79,8 +90,8 @@ public class MailServiceImp implements MailService { public String sendEmailNotice(String notice_id, String title, List mailTo, String content) throws URISyntaxException, IOException { List unsubAddress = mailUnsubMapper.getAllAddress(); List mailToWithoutUnsub = new ArrayList<>(); - mailTo.parallelStream().forEach(p->{ - if(!unsubAddress.contains(p.getString("mailto"))){ + mailTo.parallelStream().forEach(p -> { + if (!unsubAddress.contains(p.getString("mailto"))) { mailToWithoutUnsub.add(p); } }); @@ -91,7 +102,7 @@ public class MailServiceImp implements MailService { String linkHref = link.attr("href"); String linkText = link.text(); Element e = link.previousElementSibling(); - if (e!=null && "img".equalsIgnoreCase(e.tagName())) { + if (e != null && "img".equalsIgnoreCase(e.tagName())) { e.remove(); } @@ -120,17 +131,13 @@ public class MailServiceImp implements MailService { String postUrl = mailHost + "/mail?" + generateMailSignParam(); - - - - HttpRequestResult result = new HttpRequestGenerator(postUrl, RequestMethod.POST).setJSONEntity(noticeBean).execute(); if (result.isSuccess()) { String mail_id = result.getResponseContentJSONObj().getString("mail_id"); return mail_id; //System.out.println("send Mail=============="+mail_id); } else { - throw new ServerErrorException("Error Connection "+result.getStatusCode()); + throw new ServerErrorException("Error Connection " + result.getStatusCode()); } } @@ -201,25 +208,25 @@ public class MailServiceImp implements MailService { @Override public JSONObject queryUnsubPageable(JSONObject params, int limit, int page) { - return PageListUtils.buildPageListResult(mailUnsubMapper.queryPageable(params,new PageBounds(page, limit, Order.formString("create_time.desc")))); + return PageListUtils.buildPageListResult(mailUnsubMapper.queryPageable(params, new PageBounds(page, limit, Order.formString("create_time.desc")))); } @Override public void addUnsub(String ClientMoniker) { JSONObject client = clientManager.getClientInfoByMoniker(ClientMoniker); - if(client==null){ + if (client == null) { throw new NotFoundException("Merchant not Found"); } - JSONObject existRecord = mailUnsubMapper.getOne(null,client.getString("contact_email")); - if(existRecord!=null){ + JSONObject existRecord = mailUnsubMapper.getOne(null, client.getString("contact_email")); + if (existRecord != null) { throw new BadRequestException("Client has already existed"); } - JSONObject record= new JSONObject(); + JSONObject record = new JSONObject(); record.put("id", IdUtil.getId()); - record.put("address",client.getString("contact_email")); - record.put("client_id",client.getIntValue("client_id")); - record.put("client_moniker",client.getString("client_moniker")); + record.put("address", client.getString("contact_email")); + record.put("client_id", client.getIntValue("client_id")); + record.put("client_moniker", client.getString("client_moniker")); mailUnsubMapper.save(record); } @@ -234,8 +241,13 @@ public class MailServiceImp implements MailService { mailClients.add(mailClient); noticeBean.setMailClients(mailClients); noticeBean.setContent(content); - noticeBean.setSenderAddress("settlement@royalpay.com.au"); - noticeBean.setPassword("MMKdMn7dJj49jp"); + JSONObject sysConfig = sysConfigManager.getSysConfig(); + noticeBean.setSenderAddress(sysConfig.getString("settle.email.address")); + String mailPwdEncrypted = sysConfig.getString("settle.email.password"); + Assert.isTrue(StringUtils.isNotEmpty(mailPwdEncrypted), "Settlement mail pwd not configured"); + Key aesKey = AESCrypt.fromKeyString(Base64.decodeBase64(settleAESKey)); + String mailPwd = new String(AESCrypt.decrypt(Base64.decodeBase64(mailPwdEncrypted), aesKey), StandardCharsets.UTF_8); + noticeBean.setPassword(mailPwd); noticeBean.setAttachFiles(attachFiles); String postUrl = mailHost + "/mail/single?" + generateMailSignParam(); HttpRequestResult result = null; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4ad7dabda..4b9da4527 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -115,7 +115,7 @@ royalpay.sms.appkey=43390d81e20c5191c278fbf4cd275be2 im.openim.appkey=24986533 im.openim.secret=96e28e7be21a874177211c31834b5174 - +app.settle.aes-key=EPrfsM2JE69ZPR7BhXn34g== #清算银行配置 settle.abafile.default-bank=CBA settle.abafile.remains-to=ANZ diff --git a/src/test/java/au/com/royalpay/payment/manage/process/aes/AESTest.java b/src/test/java/au/com/royalpay/payment/manage/process/aes/AESTest.java new file mode 100644 index 000000000..d82620417 --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/process/aes/AESTest.java @@ -0,0 +1,29 @@ +package au.com.royalpay.payment.manage.process.aes; + +import au.com.royalpay.payment.tools.codec.AESCrypt; +import org.apache.commons.net.util.Base64; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; +import java.security.Key; + +/** + * Create by yixian at 2018-09-17 12:55 + */ +public class AESTest { + @Test + public void initAESKey() { + System.out.println(Base64.encodeBase64String(AESCrypt.randomKey().getEncoded())); + } + + @Test + public void testEncrypt() { + String keyStr = "EPrfsM2JE69ZPR7BhXn34g=="; + String source = "123456"; + Key key = AESCrypt.fromKeyString(Base64.decodeBase64(keyStr)); + byte[] encrypted = AESCrypt.encrypt(source.getBytes(StandardCharsets.UTF_8), key); + System.out.println("encrypted: " + Base64.encodeBase64String(encrypted)); + System.out.println("validate: " + new String(AESCrypt.decrypt(encrypted, key), StandardCharsets.UTF_8)); + } + +}