mintmint
200
2020-11-24 15:41:03
0
308

spring 메일 인증하기후 재전송하는 문제


아이디/비밀번호 찾기를 할 때 사용하려고 이메일 인증 소스를 구글링을 하여 인증하는 부분까진 성공을 하였는데 Ctrl+c Ctrl+v를 하지 않고, 직접 입력을 하여 인증을 하고 나면 메일이 재발송이 되는 현상이 발생합니다.. 무엇이 문제인가요???


HomePageController.java

@RequestMapping("/virtualHomePage/idFind/EmailIdFindResult")
	public String emailIdFindResult(HttpServletResponse response, Date vDate, @RequestParam("vName") String vName, @RequestParam("vBirth1") String vBirth1,
			@RequestParam("vBirth2") String vBirth2, @RequestParam("vBirth3") String vBirth3,
			@RequestParam("vEmail") String vEmail, HttpServletRequest request, Model model) throws Exception{
			VDAO vdao = new VDAO();
			model.addAttribute("vIds_Email",vdao.EmailIdFind(response, vName, vBirth1, vBirth2, vBirth3, vEmail, vDate));
			System.out.println("------------ idFindResult() 호출 ------------");
			return "Find/Id/Result/emailIdFindResult";
	}


emailCheck.java

package com.spring.virtualEmail;


import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

public class emailCheck {
	Thread thread = new Thread();
	@SuppressWarnings("static-access")
	public void sendEmail(String vEmail, String authNum) {
		String host = "smtp.gmail.com"; // smtp 서버
		String subject = "가상홈페이지에 요청하신 인증번호를 알려드립니다.";
		String fromName = "가상홈페이지 관리자";
		String from = "구글@gmail.com"; // 보내는 메일
		String to = vEmail;
		String content = "요청하신 인증번호를<br/>발송해드립니다.<br/><br/> 아래의 인증번호를 인증번호 입력창에 입력해 주세요.<br/><br/>인증번호 [" + authNum + "]<br/><br/>가상홈페이지를 이용해 주셔서 감사합니다.<br/>더욱 편리한 서비스를 제공하기 위해 항상 최선을 다하겠습니다.";
		System.out.println("to : " + to);
		try {
			Properties props = new Properties();
			// Gmail SMTP 사용시
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.smtp.host", host);
			props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.port","465"); // 587
			props.put("mail.smtp.user", from);
			props.put("mail.smtp.auth", "true");
			
			Session mailSession = Session.getInstance(props, 
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication("구글@gmail.com", "비밀번호");
						}
			});
			
			Message message = new MimeMessage(mailSession);
			message.setFrom(new InternetAddress(from, MimeUtility.encodeText(fromName, "UTF-8", "B"))); // 보내는 사람 설정
			
			InternetAddress[] address = { new InternetAddress(to) };
			message.setRecipients(Message.RecipientType.TO, address); // 받는 사람설정
			message.setSubject(subject);
			message.setSentDate(new java.util.Date()); // 보내는 날짜 설정
			message.setContent(content, "text/html;charset=utf-8");
			Transport.send(message);
			
		} catch(MessagingException me) {
			me.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	public String RandoNum() {
		StringBuffer buffer = new StringBuffer();
		for(int i = 0; i<= 6; i++) {
			int n = (int)(Math.random() * 10);
			buffer.append(n);
		}
		return buffer.toString();
	}
	
}


emailIdFindForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>

	function checkResult(){
		if($('#vName').val().trim() == ""){
			alert("이름을 먼저 입력하세요.")
			$('#vName').focus();
			return false;
		}
		else if($('#vName').val().length < 2){
			 alert('이름은 두글자 부터 가능합니다.');
	         $('#vName').focus();
	         return false;
		}
		else if($('#birth1').val() == "년도"){
			 alert("년도를 체크 해주세요");
			 return false;
		 }
		 else if($('#birth2').val() == "월"){
			 alert("월을 체크 해주세요");
			 return false;
		 }
		 else if($('#birth3').val() == "일"){
			 alert("일을 체크 해주세요");
			 return false;
		 }
		else if($('#checkRe').val().trim() == ""){
			alert("이메일 인증을 받아주세요.");
			$('#vEmail').focus();
			return false;
		}
		else if ($('#vEmail').val().trim() == ""){
			alert("이메일을 먼저 입력하세요");
			$('#vEmail').focus();
			return false;
		}
	}
	
	function emailCheck(vEmail){
		
		var exptext = /^[A-Za-z0-9_\.\-]+@[A-Za-z0-9\-]+\.[A-Za-z0-9\-]+/;
		if($('#vEmail').val().trim() == ""){
			 alert("이메일을 먼저 입력하세요");
			 $('#vEmail').focus();
			 return false;
		 }

		else if(exptext.test(vEmail) == false){
			//이메일 형식이 알파벳+숫자@알파벳+숫자.알파벳+숫자 형식이 아닐경우
			alert("이메일형식이 올바르지 않습니다.");
			$('#vEmail').focus();
			return false;
		}
		
		else{
			url = "/emailAuth?vEmail="+vEmail;
			alert("인증번호를 발송했습니다. \n인증번호가 오지 않으면 입력하신 정보가 회원정보와 일치하는지 확인해 주세요.");
			window.open(url,"emailAuth","width=300, height=250");
		 }
	}

</script>
<meta charset="UTF-8">
<title>아이디 찾기</title>
</head>
<body>
<div align = "left">
	<span>
		<a href = "/virtualHomePage"><img src = "/resources/image/home.png" class = "EmailIdFindFormImageStyle"/></a> 
		<a href = "/virtualHomePage/idFind"><label class = "idFindlabel">아이디 찾기</label></a>
		<a href = "/virtualHomePage/passwordFind"><label class = "passwordFindlabel">비밀번호 찾기</label></a>
	</span>
	<br/><br/><br/><br/><br/><br/>
</div>
<div align = "center" class = "emailIdFindFormDivStyle">
	<br/><br/><br/><label class ="EmailIdFindLabel1">가입한 이메일로 아이디 찾기</label><br/><br/><br/><br/><br/>
	<table cellpadding = "0" cellspacing = "0" border = "1" class = "EmailIdFindTableStyle">
		<form:form action = "/virtualHomePage/idFind/EmailIdFindResult" commandName="VVO" method = "post"  onsubmit = "return checkResult()" >
			<tbody>
				<tr>
					<th>이름</th>
					<td colspan = "3" style = "padding :10px;">
						<form:input path = "vName" placeholder = " 이름" id = "vName" style = "border:none; background:transparent; width:100%; vertical-align: center;"/>
					</td>
				</tr>
				<tr>
					<td colspan = "4" align = "right" style = "padding:10px;"><br/></td>
				</tr>
				
				<tr align = "center">
					<th ><label>생년월일</label></th>
					<td style = "padding:10px;">
						<form:select align = "center" path="vBirth1" style = "width:70%;" id = "birth1">
							<form:option value="년도" selected="selected">년도</form:option>
							<form:option value="2006">2006</form:option>
							<form:option value="2005">2005</form:option>
							<form:option value="2004">2004</form:option>
							<form:option value="2003">2003</form:option>
							<form:option value="2002">2002</form:option>
							<form:option value="2001">2001</form:option>
							<form:option value="2000">2000</form:option>
							<form:option value="1999">1999</form:option>
							<form:option value="1998">1998</form:option>
							<form:option value="1997">1997</form:option>
							<form:option value="1996">1996</form:option>
							<form:option value="1995">1995</form:option>
							<form:option value="1994">1994</form:option>
							<form:option value="1993">1993</form:option>
							<form:option value="1992">1992</form:option>
							<form:option value="1991">1991</form:option>
							<form:option value="1990">1990</form:option>
							<form:option value="1989">1989</form:option>
							<form:option value="1988">1988</form:option>
							<form:option value="1987">1987</form:option>
							<form:option value="1986">1986</form:option>
							<form:option value="1985">1985</form:option>
							<form:option value="1984">1984</form:option>
							<form:option value="1983">1983</form:option>
							<form:option value="1982">1982</form:option>
							<form:option value="1981">1981</form:option>
							<form:option value="1980">1980</form:option>
							<form:option value="1979">1979</form:option>
							<form:option value="1978">1978</form:option>
							<form:option value="1977">1977</form:option>
							<form:option value="1976">1976</form:option>
							<form:option value="1975">1975</form:option>
							<form:option value="1974">1974</form:option>
							<form:option value="1973">1973</form:option>
							<form:option value="1972">1972</form:option>
							<form:option value="1971">1971</form:option>
							<form:option value="1970">1970</form:option>
							<form:option value="1969">1969</form:option>
							<form:option value="1968">1968</form:option>
							<form:option value="1967">1967</form:option>
							<form:option value="1966">1966</form:option>
							<form:option value="1965">1965</form:option>
							<form:option value="1964">1964</form:option>
							<form:option value="1963">1963</form:option>
							<form:option value="1962">1962</form:option>
							<form:option value="1961">1961</form:option>
							<form:option value="1960">1960</form:option>
							<form:option value="1959">1959</form:option>
							<form:option value="1958">1958</form:option>
							<form:option value="1957">1957</form:option>
							<form:option value="1956">1956</form:option>
							<form:option value="1955">1955</form:option>
							<form:option value="1954">1954</form:option>
							<form:option value="1953">1953</form:option>
							<form:option value="1952">1952</form:option>
							<form:option value="1951">1951</form:option>
							<form:option value="1950">1950</form:option>
							<form:option value="1949">1949</form:option>
							<form:option value="1948">1948</form:option>
							<form:option value="1947">1947</form:option>
							<form:option value="1946">1946</form:option>
							<form:option value="1945">1945</form:option>
							<form:option value="1944">1944</form:option>
							<form:option value="1943">1943</form:option>
							<form:option value="1942">1942</form:option>
							<form:option value="1941">1941</form:option>
							<form:option value="1940">1940</form:option>
							<form:option value="1939">1939</form:option>
							<form:option value="1938">1938</form:option>
							<form:option value="1937">1937</form:option>
							<form:option value="1936">1936</form:option>
							<form:option value="1935">1935</form:option>
							<form:option value="1934">1934</form:option>
							<form:option value="1933">1933</form:option>
							<form:option value="1932">1932</form:option>
							<form:option value="1931">1931</form:option>
							<form:option value="1930">1930</form:option>
							<form:option value="1929">1929</form:option>
							<form:option value="1928">1928</form:option>
							<form:option value="1927">1927</form:option>
							<form:option value="1926">1926</form:option>
							<form:option value="1925">1925</form:option>
							<form:option value="1924">1924</form:option>
							<form:option value="1923">1923</form:option>
							<form:option value="1922">1922</form:option>
							<form:option value="1921">1921</form:option>
							<form:option value="1920">1920</form:option>
						</form:select><label class = "EmailIdFindLabel">년</label>&nbsp;&nbsp; 
					</td>
					<td>
						<form:select path = "vBirth2" id = "birth2">
							<form:option value="월" selected="selected">월</form:option>
							<form:option value="01">01</form:option>
							<form:option value="02">02</form:option>
							<form:option value="03">03</form:option>
							<form:option value="04">04</form:option>
							<form:option value="05">05</form:option>
							<form:option value="06">06</form:option>
							<form:option value="07">07</form:option>
							<form:option value="08">08</form:option>
							<form:option value="09">09</form:option>
							<form:option value="10">10</form:option>
							<form:option value="11">11</form:option>
							<form:option value="12">12</form:option>
						</form:select><label class = "EmailIdFindLabel">월</label>&nbsp;&nbsp;
					</td>
					<td>
						<form:select path = "vBirth3" id = "birth3">
							<form:option value="일" selected="selected">일</form:option>
							<form:option value="01">01</form:option>
							<form:option value="02">02</form:option>
							<form:option value="03">03</form:option>
							<form:option value="04">04</form:option>
							<form:option value="05">05</form:option>
							<form:option value="06">06</form:option>
							<form:option value="07">07</form:option>
							<form:option value="08">08</form:option>
							<form:option value="09">09</form:option>
							<form:option value="10">10</form:option>
							<form:option value="11">11</form:option>
							<form:option value="12">12</form:option>
							<form:option value="13">13</form:option>
							<form:option value="14">14</form:option>
							<form:option value="15">15</form:option>
							<form:option value="16">16</form:option>
							<form:option value="17">17</form:option>
							<form:option value="18">18</form:option>
							<form:option value="19">19</form:option>
							<form:option value="20">20</form:option>
							<form:option value="21">21</form:option>
							<form:option value="22">22</form:option>
							<form:option value="23">23</form:option>
							<form:option value="24">24</form:option>
							<form:option value="25">25</form:option>
							<form:option value="26">26</form:option>
							<form:option value="27">27</form:option>
							<form:option value="28">28</form:option>
							<form:option value="29">29</form:option>
							<form:option value="30">30</form:option>
							<form:option value="31">31</form:option>
						</form:select><label class = "EmailIdFindLabel">일</label>&nbsp;&nbsp;
					</td>
				</tr>
				<tr>
					<td colspan = "4" align = "right" style = "padding:10px;"><br/></td>
				</tr>
				
				<tr>
					<th style = "padding:10px;">이메일</th>
					<td colspan = "2">
						<form:input path = "vEmail" placeholder = " Email" id = "vEmail" style = "border:none; background:transparent; width:100%; vertical-align: center;" />
					</td>
					<td>
						<button type = "button" onClick = "emailCheck(this.form.vEmail.value)" class = "EmailAuthbutton">인증하기</button>
					</td>
				</tr>
				<tr>
					<td colspan = "4" align = "right">
						<br/><input type = "text" readOnly = "readOnly" id = "checkRe" disabled = "" style = "border:none; width:65%; background:white; color:red; font-size:15px;"><br/>
					</td>
				</tr>
				
				<tr>
					<td colspan = "4" align = "right">
						<br/><br/><input type = "submit" value = "아이디 찾기" class = "EmaillIdFindbutton"/>
					</td>
				</tr>
			</tbody>
		</form:form>
	</table><br/><br/><br/><br/>
</div>
</body>
</html>


emailAuth.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<script>
	function check(){
		var form = document.authenform;
		var authNum = ${authNum};
		if(!form.authnum.value){
			alert("인증번호를 입력하세요");
			return false;
		}
		if(form.authnum.value != authNum){
			alert("인증번호가 일치하지 않습니다. 인증번호를 다시 입력해주세요.");
			form.authnum.value = "";
			return false;
		}
		if(form.authnum.value == authNum){
			alert("인증완료.");
			opener.document.getElementById('checkRe').value = "인증완료";
			self.close();
			return;
		}
	}
</script>
<meta charset="UTF-8">
<title>이메일 인증</title>
</head>
<body>
	<center>
		<div align = "center" class = "emailAuthDivStyle"><br/><br/><br/>
			<form method = "post" name = "authenform" onSubmit = "return check();">
				<label class = "EmailAuthLabel"> 인증번호 7자리를 입력해주세요. </label><br/><br/><br/>
				<input type = "text" name ="authnum" style = "font-size:15px; padding:5px;"/><br/><br/><br/>
				<button type = "button" class = "EmailAuthCheckbutton"> 인증하기 </button> <br/><br/>
			</form>	
		</div>
	</center>
</body>
</html>


이메일 입력후 인증하기 버튼 클릭

이메일 도착Ctrl+c Ctrl+v 를 이용 하여 인증

메일 재발송 x

새로고침 후 이메일 입력후 인증하기 버튼 클릭

메일 도착 직접 입력하여 인증완료

메일을 재발송 해버림.... 왜 이런 상태가 발생하는 거죠???



0
  • 답변 0

  • 로그인을 하시면 답변을 등록할 수 있습니다.