diff --git a/pom.xml b/pom.xml index 1aec8c7..54d614a 100644 --- a/pom.xml +++ b/pom.xml @@ -336,25 +336,25 @@ true - local + local development - development + development staging - staging + staging production - production + production diff --git a/src/main/java/com/loafle/overflow/central/commons/utils/EmailSender.java b/src/main/java/com/loafle/overflow/central/commons/utils/EmailSender.java index f3cc162..916c04a 100644 --- a/src/main/java/com/loafle/overflow/central/commons/utils/EmailSender.java +++ b/src/main/java/com/loafle/overflow/central/commons/utils/EmailSender.java @@ -1,6 +1,7 @@ package com.loafle.overflow.central.commons.utils; import org.apache.commons.codec.binary.Base64; +import org.apache.velocity.Template; import org.apache.velocity.app.VelocityEngine; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; @@ -41,7 +42,7 @@ public class EmailSender { SimpleMailMessage message1 = new SimpleMailMessage(); message1.setTo(mail.getMailTo()); message1.setSubject(mail.getMailSubject()); - message1.setText(getContentFromTemplate(mail.getModel())); + message1.setText(getContentFromTemplate(mail.getModel(), mail)); message1.setFrom("geek@loafle.com"); mailSender.send(message1); @@ -101,10 +102,14 @@ public class EmailSender { return null; } - private String getContentFromTemplate(Map model) { + private String getContentFromTemplate(Map model, Mail mail) { StringBuffer content = new StringBuffer(); + +// Template template = this.velocityEngine.getTemplate("./local/vmtemplates/signup-email.vm"); try { - content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,"/templates/email-template.vm", "UTF-8", model)); + content.append(VelocityEngineUtils.mergeTemplateIntoString + (velocityEngine, + mail.getTemplateLoacation(), "UTF-8", model)); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/loafle/overflow/central/module/email/service/EmailAuthService.java b/src/main/java/com/loafle/overflow/central/module/email/service/EmailAuthService.java index 1cf49d2..06d33f5 100644 --- a/src/main/java/com/loafle/overflow/central/module/email/service/EmailAuthService.java +++ b/src/main/java/com/loafle/overflow/central/module/email/service/EmailAuthService.java @@ -13,6 +13,7 @@ import com.loafle.overflow.model.domain.Domain; import com.loafle.overflow.model.domain.DomainMember; import com.loafle.overflow.model.email.EmailAuth; import com.loafle.overflow.model.member.Member; +import com.loafle.overflow.model.meta.MetaEmailStatus; import com.loafle.overflow.model.meta.MetaMemberStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; @@ -28,6 +29,7 @@ import java.util.*; */ @Service("EmailAuthService") public class EmailAuthService { + private static final String WEB_GO_MEMBER_ADDR = "http://127.0.0.1:19080/account/"; @Autowired private EmailAuthDAO emailAuthDAO; @@ -45,61 +47,24 @@ public class EmailAuthService { private EmailSender emailSender; - public EmailAuth sendEmailByMember(long memberId, String memberEmail) throws UnsupportedEncodingException, MailException { - - EmailAuth auth = new EmailAuth(); - auth.setMember(new Member(memberId)); - // Todo AuthKey Generation - - String en = emailSender.encrypt(memberEmail); - auth.setEmailAuthKey(en); - - String encode = URLEncoder.encode(en, "UTF-8"); - -// System.out.println("encode = [" + encode + "]"); - Mail mail = new Mail(); - mail.setMailTo(memberEmail); - mail.setMailSubject("Confirm Email"); - mail.setMailContent("http://127.0.0.1:19080/account/check_email?key="+ encode +"\r\nConfirm Email"); - Map model = new HashMap<>(); - model.put("firstName", auth.getMember().getName()); - model.put("lastName", auth.getMember().getCompanyName()); - model.put("location", "Seoul"); - model.put("signature", "www.loafle.com"); - - mail.setModel(model); - emailSender.sendSimpleEmail(mail); - - this.emailAuthDAO.save(auth); - - return auth; - } +// private static final String WEB_NG_MEMBER_ADDR = "http://127.0.0.1:4200/auth/"; public EmailAuth read(long id) { return this.emailAuthDAO.findOne(id); } - public EmailAuth readByAuthKey(String authKey) throws OverflowException, UnsupportedEncodingException { - System.out.println("authKey = [" + authKey + "]"); - String deStr = URLDecoder.decode(authKey, "UTF-8"); + public EmailAuth readBySignupAuthKey(String token) throws OverflowException, UnsupportedEncodingException { + System.out.println("authKey = [" + token + "]"); + String deStr = URLDecoder.decode(token, "UTF-8"); System.out.println("deStr = [" + deStr + "]"); EmailAuth auth = this.emailAuthDAO.findByEmailAuthKey(deStr); - // Todo Compare email date and current date - if (auth != null) { - // Over 12 hours of validity of e-mail authentication. - Calendar cal = Calendar.getInstance(); + boolean res = this.isValidateTime(auth); - cal.setTime(auth.getCreateDate()); - cal.add(Calendar.HOUR, 12); - Date futureDate = cal.getTime(); - - Date nowDate = new Date(); - - if (!nowDate.before(futureDate)) { + if (!res) { throw new OverflowException("The authentication expiration time has passed.", new Throwable()); } @@ -121,8 +86,26 @@ public class EmailAuthService { return auth; } + public EmailAuth readByPwAuthKey(String token) throws OverflowException, UnsupportedEncodingException { + String deStr = URLDecoder.decode(token, "UTF-8"); + EmailAuth auth = this.emailAuthDAO.findByEmailAuthKey(deStr); + + if (auth != null && (auth.getMember() != null && auth.getMember().getId() > 0)) { + boolean res = this.isValidateTime(auth); + + if (!res) { + throw new OverflowException("The authentication expiration time has passed.", new Throwable()); + } + + auth.setAuthConfirmDate(new Date()); + this.emailAuthDAO.save(auth); + } + + return auth; + + } + // dZQgXM1o/Cx48X8DM+6ec/oPfqA2l/LdWtijOZ2EnWk= - // dZQgXM1o/Cx48X8DM 6ec/oPfqA2l/LdWtijOZ2EnWk= public List readByMember(long memberId) { return this.emailAuthDAO.findByMember(new Member(memberId)); @@ -132,28 +115,76 @@ public class EmailAuthService { return this.emailAuthDAO.save(emailAuth); } + public EmailAuth sendEmailByMember(Member member) throws OverflowException { + return this.sendEMail(member, 1); + } + // Todo Send Email Refactoring - public EmailAuth sendEmailResetPassword(Member member) throws UnsupportedEncodingException, MailException { + public EmailAuth sendEmailResetPassword(Member member) throws OverflowException { + return this.sendEMail(member, 2); + } + + private boolean isValidateTime(EmailAuth auth) { + + Calendar cal = Calendar.getInstance(); + + cal.setTime(auth.getCreateDate()); + cal.add(Calendar.HOUR, 12); + Date futureDate = cal.getTime(); + + Date nowDate = new Date(); + + if (!nowDate.before(futureDate)) { + return false; + } + + return true; + } + + private EmailAuth sendEMail(Member member, int status) throws OverflowException { + String enMail = emailSender.encrypt(member.getEmail()); + + String mailSubject = null; + String entry = null; + String templateName = null; + EmailAuth auth = new EmailAuth(); auth.setMember(member); - // Todo AuthKey Generation - - String en = emailSender.encrypt(member.getEmail()); - auth.setEmailAuthKey(en); - - String encode = URLEncoder.encode(en, "UTF-8"); + auth.setEmailAuthKey(enMail); + auth.setEmailStatus(new MetaEmailStatus(status)); // System.out.println("encode = [" + encode + "]"); + String encode = ""; + + try { + encode = URLEncoder.encode(enMail, "UTF-8"); + }catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + if (status == 1) { + mailSubject = "Signup Confirm Mail"; + entry = "confirm_signup"; + templateName = "./vmtemplates/signup.vm"; + } else if (status == 2) { + mailSubject = "Reset Password Confirm Mail"; + entry = "confirm_reset_pw"; + templateName = "./vmtemplates/password_reset.vm"; + } + Mail mail = new Mail(); mail.setMailTo(member.getEmail()); - mail.setMailSubject("Reset Password Email"); - mail.setMailContent("http://127.0.0.1:9091/#/account/reset_password?key="+ encode +"\r\nConfirm Email"); + mail.setMailSubject(mailSubject); + mail.setTemplateLoacation(templateName); + + String uri = WEB_GO_MEMBER_ADDR + entry + "?key=" + encode; + Map model = new HashMap<>(); model.put("firstName", auth.getMember().getName()); model.put("lastName", auth.getMember().getCompanyName()); model.put("location", "Seoul"); model.put("signature", "www.loafle.com"); - + model.put("content", uri); mail.setModel(model); emailSender.sendSimpleEmail(mail); @@ -161,6 +192,34 @@ public class EmailAuthService { return auth; } - - } + + + +// EmailAuth auth = new EmailAuth(); +// auth.setMember(member); +// String en = emailSender.encrypt(member.getEmail()); +// auth.setEmailAuthKey(en); +// auth.setEmailStatus(new MetaEmailStatus(1)); +// String encode = URLEncoder.encode(en, "UTF-8"); +// +//// System.out.println("encode = [" + encode + "]"); +// Mail mail = new Mail(); +// mail.setMailTo(member.getEmail()); +// mail.setMailSubject("Confirm Email"); +//// mail.setMailContent("http://127.0.0.1:19080/account/confirm_signup?key="+ encode +"\r\nConfirm Email"); +// +// String uri = WEB_GO_MEMBER_ADDR + "confirm_signup?key=" + encode + "\r\nConfirm Email"; +// Map model = new HashMap<>(); +// model.put("firstName", auth.getMember().getName()); +// model.put("lastName", auth.getMember().getCompanyName()); +// model.put("location", "Seoul"); +// model.put("signature", "www.loafle.com"); +// model.put("content", uri); +// +// mail.setModel(model); +// emailSender.sendSimpleEmail(mail); +// +// this.emailAuthDAO.save(auth); +// +// return auth; \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java b/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java index 39c8128..d570fa3 100644 --- a/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java +++ b/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java @@ -109,7 +109,7 @@ public class CentralMemberService implements MemberService { Member resMember = this.memberDAO.save(member); try { - this.emailAuthService.sendEmailByMember(member.getId(), member.getEmail()); + this.emailAuthService.sendEmailByMember(resMember); } catch (Exception e) { // Todo ReSend Mail e.printStackTrace(); @@ -135,11 +135,11 @@ public class CentralMemberService implements MemberService { return member; } - public Member resetPassword(String encodeEmail, String pw) throws OverflowException { + public Member resetPassword(String signinID, String newPw) throws OverflowException { String deStr = null; try { - deStr = URLDecoder.decode(encodeEmail, "UTF-8"); + deStr = URLDecoder.decode(signinID, "UTF-8"); }catch (Exception e) { } @@ -151,7 +151,7 @@ public class CentralMemberService implements MemberService { throw new OverflowException("", null); } - boolean checkPass = this.isPasswordStrong(pw); + boolean checkPass = this.isPasswordStrong(newPw); if (!checkPass) { throw new OverflowException("PasswordNotStrongException()", new Throwable()); @@ -159,7 +159,7 @@ public class CentralMemberService implements MemberService { // "special character, lowercase letter, and number, " + // "and must be at least 6 characters long."); } - member.setPw(passwordEncoder.encode(pw)); + member.setPw(passwordEncoder.encode(newPw)); return this.modify(member); } diff --git a/src/main/java/com/loafle/overflow/central/module/probe/service/CentralProbeService.java b/src/main/java/com/loafle/overflow/central/module/probe/service/CentralProbeService.java index d3bbde2..7b3018c 100644 --- a/src/main/java/com/loafle/overflow/central/module/probe/service/CentralProbeService.java +++ b/src/main/java/com/loafle/overflow/central/module/probe/service/CentralProbeService.java @@ -1,6 +1,7 @@ package com.loafle.overflow.central.module.probe.service; import com.loafle.overflow.central.module.probe.dao.ProbeDAO; +import com.loafle.overflow.core.annotation.WebappAPI; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.domain.Domain; import com.loafle.overflow.model.probe.Probe; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..95fa5da --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=@activatedProperties@ \ No newline at end of file diff --git a/src/main/resources/local/init.sql b/src/main/resources/local/init.sql index e2ec7c5..11e6b9b 100644 --- a/src/main/resources/local/init.sql +++ b/src/main/resources/local/init.sql @@ -864,7 +864,10 @@ INSERT INTO public.meta_history_type (id,create_date,"name") VALUES ( INSERT INTO public.meta_history_type (id,create_date,"name") VALUES ( 6,'2017-08-23 13:28:26.966','Sensor'); - +INSERT INTO public.meta_email_status (id,create_date,"name") VALUES ( +1,'2017-08-23 13:28:26.966','signup confirm'); +INSERT INTO public.meta_email_status (id,create_date,"name") VALUES ( +2,'2017-08-23 13:28:26.966','reset password confirm'); @@ -885,11 +888,11 @@ INSERT INTO public."member" (company_name,create_date,email,"name",phone,pw,stat INSERT INTO public."member" (company_name,create_date,email,"name",phone,pw,status_id) VALUES ( 'loafle','2017-06-26 11:07:27.625','geekdev@naver.com','geek','000-000-0000','$2a$10$G2bbjoX9.fOnxJx/8DZqPujFYrEQtIEB.f98/8K20XiGWEhwPakZ.',2); -INSERT INTO public.email_auth (auth_confirm_date,create_date,email_auth_key,member_id) VALUES ( -'2017-06-27 15:28:48.895','2017-06-26 15:28:48.895','dbseogns1234',1); +INSERT INTO public.email_auth (auth_confirm_date,create_date,email_auth_key,member_id, status) VALUES ( +'2017-06-27 15:28:48.895','2017-06-26 15:28:48.895','dbseogns1234',1,1); -INSERT INTO public.email_auth (auth_confirm_date,create_date,email_auth_key,member_id) VALUES ( -'2017-12-26 15:28:48.895','2017-11-22 12:28:48.895','dbseogns1234',2); +INSERT INTO public.email_auth (auth_confirm_date,create_date,email_auth_key,member_id, status) VALUES ( +'2017-12-26 15:28:48.895','2017-11-22 12:28:48.895','dbseogns1234',2,1); INSERT INTO public."domain" (create_date,"name") VALUES ( '2017-06-26 11:25:44.866','overFlow''s domain'); diff --git a/src/main/resources/local/mail.properties b/src/main/resources/local/mail.properties index 99f696d..0a01f91 100644 --- a/src/main/resources/local/mail.properties +++ b/src/main/resources/local/mail.properties @@ -2,7 +2,7 @@ mail.host=smtp.worksmobile.com mail.port=465 mail.username=geek@loafle.com -mail.password=@loafle@5795 +mail.password=@cosmos@5795 mail.protocol=smtps mail.properties.mail.smtp.auth=true diff --git a/src/main/resources/signup_email.vm b/src/main/resources/local/vmtemplates/password_reset.vm similarity index 71% rename from src/main/resources/signup_email.vm rename to src/main/resources/local/vmtemplates/password_reset.vm index ac6dcf3..98aacb1 100644 --- a/src/main/resources/signup_email.vm +++ b/src/main/resources/local/vmtemplates/password_reset.vm @@ -4,7 +4,7 @@

Dear ${firstName} ${lastName},

-

Sending Email Velocity Template Test !!!

+

Sending Email ${content}

Thanks

${signature}

${location}

diff --git a/src/main/resources/local/vmtemplates/signup.vm b/src/main/resources/local/vmtemplates/signup.vm new file mode 100644 index 0000000..98aacb1 --- /dev/null +++ b/src/main/resources/local/vmtemplates/signup.vm @@ -0,0 +1,13 @@ + + + + + +

Dear ${firstName} ${lastName},

+

Sending Email ${content}

+

Thanks

+

${signature}

+

${location}

+ + + \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/central/module/email/service/EmailAuthServiceTest.java b/src/test/java/com/loafle/overflow/central/module/email/service/EmailAuthServiceTest.java index 4b6592a..5acc2c6 100644 --- a/src/test/java/com/loafle/overflow/central/module/email/service/EmailAuthServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/email/service/EmailAuthServiceTest.java @@ -3,6 +3,7 @@ package com.loafle.overflow.central.module.email.service; import com.loafle.overflow.central.spring.AppConfigTest; import com.loafle.overflow.model.email.EmailAuth; +import com.loafle.overflow.model.member.Member; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -28,7 +29,10 @@ public class EmailAuthServiceTest { @Test @Ignore public void TestMailSend() throws Exception { - this.emailAuthService.sendEmailByMember((long)1, "geek@loafle.com"); + Member member = new Member(); + member.setId(1); + member.setEmail("overflow@loafle.com"); + this.emailAuthService.sendEmailByMember(member); } @Test