ํฐ์คํ ๋ฆฌ ๋ทฐ
Controller
UsersController
//find ์์ฒญ
@GetMapping("/pw-find")
public ModelAndView find() {
return new ModelAndView ("user/pw-find");
}
๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ ํ์ด์ง ์์ฒญ
//๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ ์์ฒญ
@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๋ฅผ ์ด์ฉํด์ ์์ฑํ์๋ค. ์์๋ก ์์ฑํ ๋น๋ฐ๋ฒํธ๋ ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์ ํตํด ๋ณ๊ฒฝ์ ํด์ค๋ค.
'Project > Instagram' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring] ํด๋ก ์ฝ๋ฉ12 Post - Model, Repository, Mapper, Test, Service, Controller (0) | 2020.04.13 |
---|---|
[spring] ํด๋ก ์ฝ๋ฉ11 User - ํ์ ํํด (0) | 2020.04.12 |
[spring] ํด๋ก ์ฝ๋ฉ09 User - ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ (0) | 2020.04.09 |
[spring] ํด๋ก ์ฝ๋ฉ08 User - ํ๋กํ ์ฌ์ง ๋ฑ๋ก, ์กฐํ, ์ญ์ (0) | 2020.04.08 |
[spring] ํด๋ก ์ฝ๋ฉ07 User - ํ์์ ๋ณด ๋ณ๊ฒฝ (0) | 2020.04.08 |
- Total
- Today
- Yesterday
- ๊ฒ์ํ ์กฐํ
- ๋ถํธ ์๋์์ฑ
- ๊ฒ์๋ฌผ ์ญ์
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- ๊ฒ์ํ ์ญ์
- Algorithm
- ์๊ณ ๋ฆฌ์ฆ
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- mysql์ค์น
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ๊ฐ๋ฐ
- ๊ฒ์๋ฌผ์กฐํ
- ์๋ฐ
- tomcat์ค์น
- java jdk ์ค์น
- ๋ณ๋ช ์ฒ๋ฆฌ
- Java
- typeAliases
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- ์๋ฃ๊ตฌ์กฐ
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- ์จ๋ฆฌ์์ค
- ์ดํด๋ฆฝ์ค ์ค์น
- java ํ๊ฒฝ๋ณ์
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |