ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

Controller


UsersController

//find ์š”์ฒญ
@GetMapping("/pw-find")
public ModelAndView find() {
	return new ModelAndView ("user/pw-find");
}

๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ ํŽ˜์ด์ง€ ์š”์ฒญ

pw-find.jspํŽ˜์ด์ง€๋กœ ์ด๋™

//๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ ์š”์ฒญ
@PostMapping("pw-find")
public String findPw(@RequestBody LoginVO login) throws Exception {
	
	System.out.println("ํผ์—์„œ ๋ฐ›์•„์˜จ email๊ฐ’ : " + login);
	
	return usersService.findPw(login);
}

์ด๋ฉ”์ผ๋กœ ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฐœ์†กํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ฐพ์•„๋ณธ๋‹ค.

์ •๋ณด๋Š” LoginVO์— ๋‹ด์•„์„œ ๊ฐ€์ ธ์˜จ๋‹ค. 

 

๋ฉ”์ผ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์•„ํŒŒ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” commons email ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋„๋ก ํ•œ๋‹ค.

๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.


pom.xml ๋ฉ”์ผ ๊ด€๋ จ api ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด์ค€๋‹ค.

<!-- ๋ฉ”์ผ ๊ด€๋ จ api -->    
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-email</artifactId>
	<version>1.5</version>
</dependency>

 

 

 

Commons


MailUtil

package com.hastagram.myapp.commons.mail;

import org.apache.commons.mail.HtmlEmail;

import com.hastagram.myapp.users.model.UsersVO;

//๋ฉ”์ผ์„ ๋ณด๋‚ผ ํด๋ž˜์Šค
public class MailUtil {

	public void sendMail(UsersVO user) throws Exception{
		
		//Mail Server ์„ค์ •
		String charSet = "utf-8";
		String hostSMTP = "smtp.naver.com";		//SMTP ์„œ๋ฒ„๋ช…
		String hostSMTPid = "dev_hado"; 		//๋„ค์ด๋ฒ„ ์•„์ด๋””
		String hostSMTPpw = "2020hastagram!"; 	//๋„ค์ด๋ฒ„ ๋น„๋ฐ€๋ฒˆํ˜ธ
		
		//๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ
		String fromEmail = "dev_hado@naver.com";	//๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ ๋ฉ”์ผ
		String fromName = "HASTAGRAM";			//๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„
		
		
		String subject = ""; 	//๋ฉ”์ผ ์ œ๋ชฉ
		String msg = "";		//๋ฉ”์ผ ๋‚ด์šฉ
		
		
		subject = "[HASTAGRAM] ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฐœ๊ธ‰ ์•ˆ๋‚ด";
		msg += "<div align='left'>";
		msg += "<h3>";
		msg += user.getId() + "๋‹˜์˜ ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค.<br>๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉํ•˜์„ธ์š”.</h3>";
		msg += "<p>์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ : ";
		msg += user.getPw() + "</p></div>";
		
		
		//email ์ „์†ก
		String mailRecipient = user.getEmail(); //๋ฐ›๋Š” ์‚ฌ๋žŒ ์ด๋ฉ”์ผ ์ฃผ์†Œ
		try {
			//๊ฐ์ฒด ์„ ์–ธ
			HtmlEmail mail = new HtmlEmail();
			mail.setDebug(true);
			mail.setCharset(charSet);
			mail.setSSLOnConnect(true); //SSL ์‚ฌ์šฉ (TLS๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์— SSL์‚ฌ์šฉ)
			mail.setHostName(hostSMTP);
			mail.setSmtpPort(587); 		//SMTP ํฌํŠธ ๋ฒˆํ˜ธ
			mail.setAuthentication(hostSMTPid, hostSMTPpw);
			mail.setStartTLSEnabled(true); //TLS ์‚ฌ์šฉ
			mail.addTo(mailRecipient, charSet);
			mail.setFrom(fromEmail, fromName, charSet);
			mail.setSubject(subject);
			mail.setHtmlMsg(msg);
			mail.send();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

๋ฉ”์ผ์„ ๋ณด๋‚ผ ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฅธ ํŒจํ‚ค์ง€ ์•ˆ์— ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

 

 

Service


UsersService

//๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ
@Override
public String findPw(LoginVO login) throws Exception {
	
	BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
	String result = null;
	
	System.out.println("login ํ™•์ธ : " + login);
	
	//ํšŒ์›์ •๋ณด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
	UsersVO user = usersDAO.login(login);
	
	//๊ฐ€์ž…๋œ ์ด๋ฉ”์ผ์ด ์กด์žฌํ•œ๋‹ค๋ฉด ์ด๋ฉ”์ผ ๋ฐœ์†ก
	if(user != null) {
		
		//์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ(UUID ์ด์šฉ - ํŠน์ˆ˜๋ฌธ์ž๋Š” ๋ชป๋„ฃ์Œ ใ… ใ… )
		String tempPw = UUID.randomUUID().toString().replace("-", ""); // -๋ฅผ ์ œ๊ฑฐ
		tempPw = tempPw.substring(0,10); //tempPw๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ 10์ž๋ฆฌ ์ž˜๋ผ์คŒ
		
		System.out.println("์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ : " + tempPw);
		
		//user๊ฐ์ฒด์— ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋‹ด๊ธฐ
		user.setPw(tempPw);
		
		//๋ฉ”์ผ ์ „์†ก
		MailUtil mail = new MailUtil();
		mail.sendMail(user);
		
		//ํšŒ์› ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ  user๊ฐ์ฒด์— ๋‹ค์‹œ ์ €์žฅ
		String securePw = encoder.encode(user.getPw());
		user.setPw(securePw);
		
		//๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ
		usersDAO.modifyPw(user);
		
		result = "Success";
		
	} else {
		
		result = "Fail";
	}
	
	
	return result;
}

ํšŒ์› ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๊ฐ€์ž…๋œ ํšŒ์›์ด ์กด์žฌํ•œ๋‹ค๋ฉด ์ด๋ฉ”์ผ์„ ๋ฐœ์†กํ•œ๋‹ค. ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” UUID๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์„ฑํ•˜์˜€๋‹ค. ์ž„์‹œ๋กœ ์ƒ์„ฑํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ์„ ํ†ตํ•ด ๋ณ€๊ฒฝ์„ ํ•ด์ค€๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€