email auth mail encode url

This commit is contained in:
geek 2017-08-29 18:35:52 +09:00
parent bf85817e8b
commit 62cdb0cceb
6 changed files with 98 additions and 15 deletions

View File

@ -118,6 +118,13 @@
<version>${javax.mail.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.loafle</groupId>
<artifactId>overflow_api_server</artifactId>

View File

@ -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<EmailAuth, Long> {
@Query("select e from EmailAuth e where e.emailAuthKey = :authKey")
EmailAuth findByEmailAuthKey(@Param("authKey") String emailAuthKey);
EmailAuth findByEmailAuthKey(String emailAuthKey);
List<EmailAuth> findByMember(Member member);

View File

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

View File

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

View File

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

View File

@ -25,4 +25,5 @@ public class EmailAuthServiceTest {
public void TestMailSend() throws Exception {
this.emailAuthService.sendEmailByMember((long)1, "geek@loafle.com");
}
}