diff --git a/pom.xml b/pom.xml index 4e9e42a..e11d045 100644 --- a/pom.xml +++ b/pom.xml @@ -118,6 +118,13 @@ ${javax.mail.version} + + + commons-codec + commons-codec + 1.5 + + com.loafle overflow_api_server diff --git a/src/main/java/com/loafle/overflow/module/email/dao/EmailAuthDAO.java b/src/main/java/com/loafle/overflow/module/email/dao/EmailAuthDAO.java index 54070bc..f950196 100644 --- a/src/main/java/com/loafle/overflow/module/email/dao/EmailAuthDAO.java +++ b/src/main/java/com/loafle/overflow/module/email/dao/EmailAuthDAO.java @@ -3,8 +3,6 @@ package com.loafle.overflow.module.email.dao; import com.loafle.overflow.module.email.model.EmailAuth; import com.loafle.overflow.module.member.model.Member; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -14,8 +12,7 @@ import java.util.List; */ @Repository public interface EmailAuthDAO extends JpaRepository { - @Query("select e from EmailAuth e where e.emailAuthKey = :authKey") - EmailAuth findByEmailAuthKey(@Param("authKey") String emailAuthKey); + EmailAuth findByEmailAuthKey(String emailAuthKey); List findByMember(Member member); diff --git a/src/main/java/com/loafle/overflow/module/email/model/EmailAuth.java b/src/main/java/com/loafle/overflow/module/email/model/EmailAuth.java index 263e564..d782613 100644 --- a/src/main/java/com/loafle/overflow/module/email/model/EmailAuth.java +++ b/src/main/java/com/loafle/overflow/module/email/model/EmailAuth.java @@ -48,7 +48,7 @@ public class EmailAuth { } @Basic - @Column(name = "AUTH_CONFIRM_DATE", nullable = true, insertable = true, updatable = false) + @Column(name = "AUTH_CONFIRM_DATE", nullable = true, insertable = true, updatable = true) public Date getAuthConfirmDate() { return authConfirmDate; } diff --git a/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java b/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java index e2db11a..98fae8f 100644 --- a/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java +++ b/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java @@ -2,7 +2,10 @@ package com.loafle.overflow.module.email.service; import com.loafle.overflow.module.email.dao.EmailAuthDAO; import com.loafle.overflow.module.email.model.EmailAuth; +import com.loafle.overflow.module.member.dao.MemberDAO; import com.loafle.overflow.module.member.model.Member; +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.core.io.FileSystemResource; import org.springframework.mail.MailException; @@ -11,9 +14,16 @@ 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; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Date; import java.util.List; /** @@ -28,17 +38,28 @@ public class EmailAuthService { @Autowired private EmailAuthDAO emailAuthDAO; - public EmailAuth sendEmailByMember(long memberId, String memberEmail) { - try { - this.sendSimpleEmail(memberEmail, "Confirm Email", "Confirm Email"); - }catch (MailException e) { - e.printStackTrace(); - } + @Autowired + private MemberDAO memberDAO; + + private String key = "loafle@RandomKey"; + private String initVector = "loafleInitVector"; + + public EmailAuth sendEmailByMember(long memberId, String memberEmail) throws UnsupportedEncodingException { EmailAuth auth = new EmailAuth(); auth.setMember(new Member(memberId)); // Todo AuthKey Generation - auth.setEmailAuthKey("djdjdjdjeiejdikdjki"); + String en = this.encrypt(memberEmail); + auth.setEmailAuthKey(en); + + String encode = URLEncoder.encode(en, "UTF-8"); + +// System.out.println("encode = [" + encode + "]"); + try { + 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); @@ -49,10 +70,25 @@ public class EmailAuthService { return this.emailAuthDAO.findOne(id); } - public EmailAuth readByAuthKey(String authKey) { - return this.emailAuthDAO.findByEmailAuthKey(authKey); + public EmailAuth readByAuthKey(String authKey) throws UnsupportedEncodingException { + System.out.println("authKey = [" + authKey + "]"); + String deStr = URLDecoder.decode(authKey, "UTF-8"); + System.out.println("deStr = [" + deStr + "]"); + + EmailAuth auth = this.emailAuthDAO.findByEmailAuthKey(deStr); + + if (auth != null) { + auth.setAuthConfirmDate(new Date()); + this.emailAuthDAO.save(auth); + auth.getMember().setStatus(new MetaMemberStatus((short)2)); + this.memberDAO.save(auth.getMember()); + } + return auth; } + // dZQgXM1o/Cx48X8DM+6ec/oPfqA2l/LdWtijOZ2EnWk= + // dZQgXM1o/Cx48X8DM 6ec/oPfqA2l/LdWtijOZ2EnWk= + public List readByMember(long memberId) { return this.emailAuthDAO.findByMember(new Member(memberId)); } @@ -86,4 +122,44 @@ public class EmailAuthService { 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; + } + } diff --git a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java index 4f8281f..31e611e 100644 --- a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java +++ b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java @@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; +import java.io.UnsupportedEncodingException; + /** * Created by geek on 17. 6. 28. */ @@ -43,7 +45,7 @@ public class MemberService { return m; } - public Member signup(Member member, String pw) { + public Member signup(Member member, String pw) throws UnsupportedEncodingException { Member isMember = this.memberDAO.findByEmail(member.getEmail()); diff --git a/src/test/java/com/loafle/overflow/module/email/service/EmailAuthServiceTest.java b/src/test/java/com/loafle/overflow/module/email/service/EmailAuthServiceTest.java index ee4bdb9..c95647e 100644 --- a/src/test/java/com/loafle/overflow/module/email/service/EmailAuthServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/email/service/EmailAuthServiceTest.java @@ -25,4 +25,5 @@ public class EmailAuthServiceTest { public void TestMailSend() throws Exception { this.emailAuthService.sendEmailByMember((long)1, "geek@loafle.com"); } + } \ No newline at end of file