emailsender and forgot password
This commit is contained in:
parent
db7244a85e
commit
ebbdfa2d64
|
@ -0,0 +1,96 @@
|
||||||
|
package com.loafle.overflow.commons.utils;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
import org.springframework.mail.MailException;
|
||||||
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by geek on 17. 8. 30.
|
||||||
|
*/
|
||||||
|
@Service("EmailSender")
|
||||||
|
public class EmailSender {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JavaMailSender mailSender;
|
||||||
|
|
||||||
|
private String key = "loafle@RandomKey";
|
||||||
|
private String initVector = "loafleInitVector";
|
||||||
|
|
||||||
|
|
||||||
|
public void sendSimpleEmail(String to, String sub, String message) throws MailException {
|
||||||
|
|
||||||
|
SimpleMailMessage message1 = new SimpleMailMessage();
|
||||||
|
message1.setTo(to);
|
||||||
|
message1.setSubject(sub);
|
||||||
|
message1.setText(message);
|
||||||
|
message1.setFrom("geek@loafle.com");
|
||||||
|
|
||||||
|
mailSender.send(message1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMailWithAttachment(String to, String sub, String text, String path) throws MessagingException {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
// pass 'true' to the constructor to create a multipart message
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||||
|
|
||||||
|
helper.setTo(to);
|
||||||
|
helper.setSubject(sub);
|
||||||
|
helper.setText(text);
|
||||||
|
helper.setFrom("geek@loafle.com");
|
||||||
|
FileSystemResource file = new FileSystemResource(new File(path));
|
||||||
|
helper.addAttachment("Invoice", file);
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encrypt(String value) {
|
||||||
|
try {
|
||||||
|
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
|
||||||
|
|
||||||
|
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
||||||
|
|
||||||
|
byte[] encrypted = cipher.doFinal(value.getBytes());
|
||||||
|
System.out.println("encrypted string: "
|
||||||
|
+ Base64.encodeBase64String(encrypted));
|
||||||
|
|
||||||
|
return Base64.encodeBase64String(encrypted);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String decrypt(String encrypted) {
|
||||||
|
try {
|
||||||
|
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
|
||||||
|
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
||||||
|
|
||||||
|
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
|
||||||
|
|
||||||
|
return new String(original);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,25 +1,21 @@
|
||||||
package com.loafle.overflow.module.email.service;
|
package com.loafle.overflow.module.email.service;
|
||||||
|
|
||||||
|
import com.loafle.overflow.commons.utils.EmailSender;
|
||||||
|
import com.loafle.overflow.module.domain.dao.DomainDAO;
|
||||||
|
import com.loafle.overflow.module.domain.dao.DomainMemberDAO;
|
||||||
|
import com.loafle.overflow.module.domain.model.Domain;
|
||||||
|
import com.loafle.overflow.module.domain.model.DomainMember;
|
||||||
|
import com.loafle.overflow.module.domain.service.DomainMemberService;
|
||||||
|
import com.loafle.overflow.module.domain.service.DomainService;
|
||||||
import com.loafle.overflow.module.email.dao.EmailAuthDAO;
|
import com.loafle.overflow.module.email.dao.EmailAuthDAO;
|
||||||
import com.loafle.overflow.module.email.model.EmailAuth;
|
import com.loafle.overflow.module.email.model.EmailAuth;
|
||||||
import com.loafle.overflow.module.member.dao.MemberDAO;
|
import com.loafle.overflow.module.member.dao.MemberDAO;
|
||||||
import com.loafle.overflow.module.member.model.Member;
|
import com.loafle.overflow.module.member.model.Member;
|
||||||
import com.loafle.overflow.module.meta.model.MetaMemberStatus;
|
import com.loafle.overflow.module.meta.model.MetaMemberStatus;
|
||||||
import org.apache.commons.codec.binary.Base64;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.io.FileSystemResource;
|
|
||||||
import org.springframework.mail.MailException;
|
import org.springframework.mail.MailException;
|
||||||
import org.springframework.mail.SimpleMailMessage;
|
|
||||||
import org.springframework.mail.javamail.JavaMailSender;
|
|
||||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
|
||||||
import javax.mail.MessagingException;
|
|
||||||
import javax.mail.internet.MimeMessage;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
@ -32,34 +28,35 @@ import java.util.List;
|
||||||
@Service("EmailAuthService")
|
@Service("EmailAuthService")
|
||||||
public class EmailAuthService {
|
public class EmailAuthService {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private JavaMailSender mailSender;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmailAuthDAO emailAuthDAO;
|
private EmailAuthDAO emailAuthDAO;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MemberDAO memberDAO;
|
private MemberDAO memberDAO;
|
||||||
|
|
||||||
private String key = "loafle@RandomKey";
|
@Autowired
|
||||||
private String initVector = "loafleInitVector";
|
private DomainDAO domainDAO;
|
||||||
|
|
||||||
public EmailAuth sendEmailByMember(long memberId, String memberEmail) throws UnsupportedEncodingException {
|
@Autowired
|
||||||
|
private DomainMemberDAO domainMemberDAO;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmailSender emailSender;
|
||||||
|
|
||||||
|
|
||||||
|
public EmailAuth sendEmailByMember(long memberId, String memberEmail) throws UnsupportedEncodingException, MailException {
|
||||||
|
|
||||||
EmailAuth auth = new EmailAuth();
|
EmailAuth auth = new EmailAuth();
|
||||||
auth.setMember(new Member(memberId));
|
auth.setMember(new Member(memberId));
|
||||||
// Todo AuthKey Generation
|
// Todo AuthKey Generation
|
||||||
String en = this.encrypt(memberEmail);
|
|
||||||
|
String en = emailSender.encrypt(memberEmail);
|
||||||
auth.setEmailAuthKey(en);
|
auth.setEmailAuthKey(en);
|
||||||
|
|
||||||
String encode = URLEncoder.encode(en, "UTF-8");
|
String encode = URLEncoder.encode(en, "UTF-8");
|
||||||
|
|
||||||
// System.out.println("encode = [" + encode + "]");
|
// System.out.println("encode = [" + encode + "]");
|
||||||
try {
|
emailSender.sendSimpleEmail(memberEmail, "Confirm Email", "http://127.0.0.1:19080/account/check_email?key="+ encode +"\r\nConfirm Email");
|
||||||
this.sendSimpleEmail(memberEmail, "Confirm Email", "http://127.0.0.1:19080/account/check_email?key="+ encode +"\r\nConfirm Email");
|
|
||||||
}catch (MailException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emailAuthDAO.save(auth);
|
this.emailAuthDAO.save(auth);
|
||||||
|
|
||||||
|
@ -82,7 +79,17 @@ public class EmailAuthService {
|
||||||
this.emailAuthDAO.save(auth);
|
this.emailAuthDAO.save(auth);
|
||||||
auth.getMember().setStatus(new MetaMemberStatus((short)2));
|
auth.getMember().setStatus(new MetaMemberStatus((short)2));
|
||||||
this.memberDAO.save(auth.getMember());
|
this.memberDAO.save(auth.getMember());
|
||||||
|
|
||||||
|
Domain domain = new Domain();
|
||||||
|
domain.setName(auth.getMember().getCompanyName());
|
||||||
|
Domain domain1 = this.domainDAO.save(domain);
|
||||||
|
|
||||||
|
DomainMember domainMember = new DomainMember();
|
||||||
|
domainMember.setDomain(domain1);
|
||||||
|
domainMember.setMember(auth.getMember());
|
||||||
|
this.domainMemberDAO.save(domainMember);
|
||||||
}
|
}
|
||||||
|
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,69 +104,6 @@ public class EmailAuthService {
|
||||||
return this.emailAuthDAO.save(emailAuth);
|
return this.emailAuthDAO.save(emailAuth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendSimpleEmail(String to, String sub, String message) throws MailException {
|
|
||||||
|
|
||||||
SimpleMailMessage message1 = new SimpleMailMessage();
|
|
||||||
message1.setTo(to);
|
|
||||||
message1.setSubject(sub);
|
|
||||||
message1.setText(message);
|
|
||||||
message1.setFrom("geek@loafle.com");
|
|
||||||
|
|
||||||
mailSender.send(message1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendMailWithAttachment(String to, String sub, String text, String path) throws MessagingException {
|
|
||||||
MimeMessage message = mailSender.createMimeMessage();
|
|
||||||
// pass 'true' to the constructor to create a multipart message
|
|
||||||
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
||||||
|
|
||||||
helper.setTo(to);
|
|
||||||
helper.setSubject(sub);
|
|
||||||
helper.setText(text);
|
|
||||||
helper.setFrom("geek@loafle.com");
|
|
||||||
FileSystemResource file = new FileSystemResource(new File(path));
|
|
||||||
helper.addAttachment("Invoice", file);
|
|
||||||
|
|
||||||
mailSender.send(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String encrypt(String value) {
|
|
||||||
try {
|
|
||||||
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
|
|
||||||
|
|
||||||
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
|
|
||||||
|
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
|
||||||
|
|
||||||
byte[] encrypted = cipher.doFinal(value.getBytes());
|
|
||||||
System.out.println("encrypted string: "
|
|
||||||
+ Base64.encodeBase64String(encrypted));
|
|
||||||
|
|
||||||
return Base64.encodeBase64String(encrypted);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String decrypt(String encrypted) {
|
|
||||||
try {
|
|
||||||
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
|
|
||||||
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
|
|
||||||
|
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
|
||||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
|
||||||
|
|
||||||
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
|
|
||||||
|
|
||||||
return new String(original);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
package com.loafle.overflow.module.member.service;
|
package com.loafle.overflow.module.member.service;
|
||||||
|
|
||||||
import com.loafle.overflow.commons.model.SessionMetadata;
|
import com.loafle.overflow.commons.model.SessionMetadata;
|
||||||
|
import com.loafle.overflow.commons.utils.EmailSender;
|
||||||
import com.loafle.overflow.module.email.service.EmailAuthService;
|
import com.loafle.overflow.module.email.service.EmailAuthService;
|
||||||
import com.loafle.overflow.module.member.dao.MemberDAO;
|
import com.loafle.overflow.module.member.dao.MemberDAO;
|
||||||
import com.loafle.overflow.module.member.exception.*;
|
import com.loafle.overflow.module.member.exception.*;
|
||||||
import com.loafle.overflow.module.member.model.Member;
|
import com.loafle.overflow.module.member.model.Member;
|
||||||
import com.loafle.overflow.module.meta.model.MetaMemberStatus;
|
import com.loafle.overflow.module.meta.model.MetaMemberStatus;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.mail.MailException;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by geek on 17. 6. 28.
|
* Created by geek on 17. 6. 28.
|
||||||
|
@ -24,6 +28,9 @@ public class MemberService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmailAuthService emailAuthService;
|
private EmailAuthService emailAuthService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmailSender emailSender;
|
||||||
|
|
||||||
private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||||
|
|
||||||
public Member signin(String signinId, String signinPw) throws SignInIdNotExistException, EmailNotConfirmedException, SignInPwNotMatchException {
|
public Member signin(String signinId, String signinPw) throws SignInIdNotExistException, EmailNotConfirmedException, SignInPwNotMatchException {
|
||||||
|
@ -66,6 +73,35 @@ public class MemberService {
|
||||||
return resMember;
|
return resMember;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Member sendEmailForPassword(String email) throws UnsupportedEncodingException, MailException {
|
||||||
|
Member member = this.memberDAO.findByEmail(email);
|
||||||
|
|
||||||
|
if (null == member) {
|
||||||
|
throw new SignInIdNotExistException();
|
||||||
|
}
|
||||||
|
String en = this.emailSender.encrypt(email);
|
||||||
|
String encode = URLEncoder.encode(en, "UTF-8");
|
||||||
|
|
||||||
|
this.emailSender.sendSimpleEmail(email,"Password Reset Email", "http://127.0.0.1:19080/account/reset_password?key="+ encode +"\r\nConfirm Email");
|
||||||
|
|
||||||
|
return member;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Member resetPassword(String encodeEmail, String pw) throws UnsupportedEncodingException {
|
||||||
|
String deStr = URLDecoder.decode(encodeEmail, "UTF-8");
|
||||||
|
|
||||||
|
String deEmail = this.emailSender.decrypt(deStr);
|
||||||
|
Member member = this.memberDAO.findByEmail(deEmail);
|
||||||
|
|
||||||
|
if (null == member) {
|
||||||
|
throw new SignInIdNotExistException();
|
||||||
|
}
|
||||||
|
|
||||||
|
member.setPw(passwordEncoder.encode(pw));
|
||||||
|
|
||||||
|
return this.memberDAO.save(member);
|
||||||
|
}
|
||||||
|
|
||||||
public void signout(Member member) {
|
public void signout(Member member) {
|
||||||
// Todo websocket session remove
|
// Todo websocket session remove
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user