This commit is contained in:
geek 2018-05-04 16:38:36 +09:00
parent 93be2d0e2d
commit cfa920cf37
11 changed files with 162 additions and 76 deletions

View File

@ -336,25 +336,25 @@
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>local</environment>
<activatedProperties>local</activatedProperties>
</properties>
</profile>
<profile>
<id>development</id>
<properties>
<environment>development</environment>
<activatedProperties>development</activatedProperties>
</properties>
</profile>
<profile>
<id>staging</id>
<properties>
<environment>staging</environment>
<activatedProperties>staging</activatedProperties>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<environment>production</environment>
<activatedProperties>production</activatedProperties>
</properties>
</profile>
</profiles>

View File

@ -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<String, Object> model) {
private String getContentFromTemplate(Map<String, Object> 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();
}

View File

@ -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<String, Object> 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<EmailAuth> 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<String, Object> 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<String, Object> 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;

View File

@ -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);
}

View File

@ -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;

View File

@ -0,0 +1 @@
spring.profiles.active=@activatedProperties@

View File

@ -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');

View File

@ -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

View File

@ -4,7 +4,7 @@
<body>
<p>Dear ${firstName} ${lastName},</p>
<p>Sending Email <b>Velocity Template Test !!!</b></p>
<p>Sending Email <b>${content}</b></p>
<p>Thanks</p>
<p>${signature}</p>
<p>${location}</p>

View File

@ -0,0 +1,13 @@
<html>
<head></head>
<body>
<p>Dear ${firstName} ${lastName},</p>
<p>Sending Email <b>${content}</b></p>
<p>Thanks</p>
<p>${signature}</p>
<p>${location}</p>
</body>
</html>

View File

@ -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