ํฐ์คํ ๋ฆฌ ๋ทฐ
Controller
UsersController
//pw-change ์์ฒญ
@GetMapping("/pw-change")
public ModelAndView pwChange() {
return new ModelAndView ("user/pw-change");
}
๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ ํ์ด์ง ์์ฒญ
ํ์ฌ ๋น๋ฐ๋ฒํธ, ์๋น๋ฐ๋ฒํธ ๊ทธ๋ฆฌ๊ณ ์๋น๋ฐ๋ฒํธ ํ์ธ ์ด ์ธ๊ฐ์ง ์ ๋ณด๋ฅผ ๋ฐ์ ํ ํ์ฌ ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ๊ณ ์๋น๋ฐ๋ฒํธ์ ์๋น๋ฐ๋ฒํธ ํ์ธ์ด ์๋ก ์ผ์นํ ์์๋ง ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์ ํ๋๋ก ํ๋ค.
//๋น๋ฐ๋ฒํธ ํ์ธ ์ฒ๋ฆฌ ์์ฒญ
@PostMapping("/checkPw")
public String checkPw(@RequestBody String pw, HttpSession session) throws Exception {
logger.info("๋น๋ฐ๋ฒํธ ํ์ธ ์์ฒญ ๋ฐ์");
String result = null;
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
UsersVO dbUser = (UsersVO)session.getAttribute("login");
logger.info("DB ํ์์ ๋น๋ฐ๋ฒํธ : " + dbUser.getPw());
logger.info("ํผ์์ ๋ฐ์์จ ๋น๋ฐ๋ฒํธ : " + pw);
if(encoder.matches(pw, dbUser.getPw())) {
result = "pwConfirmOK";
} else {
result = "pwConfirmNO";
}
return result;
}
์์ ํ์๊ฐ์ ์์๋ ๋น๋๊ธฐ์์ผ๋ก ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํ๋๋ฐ ์ฌ๊ธฐ์๋ ๋๊ธฐ๋ฐฉ์์ผ๋ก ํ์ธํ๋๋ก ํ์๋ค. BCryptPassWordEncoder์ matches๋ฅผ ์ด์ฉํด์ ์ผ์น ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
//๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ ์์ฒญ
@PostMapping("/pw-change")
public String pwChange(@RequestBody UsersVO user, HttpSession session) throws Exception {
logger.info("๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ ์์ฒญ ๋ฐ์!!!");
//๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ
usersService.modifyPw(user);
//๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ ์ฑ๊ณต์ ๋ก๊ทธ์ธ ์ธ์
๊ฐ์ฒด ๋ค์ ๋ด์
LoginVO modifyUser = new LoginVO();
modifyUser.setEmail(user.getEmail());
UsersVO mUser = usersService.login(modifyUser);
logger.info("ํ์์ ๋ณด ๋ถ๋ฌ์ค๊ธฐ : " + mUser);
session.setAttribute("login", mUser);
return "changeSuccess";
}
Service
UsersService
//๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ
@Override
public void modifyPw(UsersVO user) throws Exception {
//ํ์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ธ์ฝ๋ฉํ๊ธฐ ์ํด ๊ฐ์ฒด ์ ์ธ
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
System.out.println("์ํธํ ์ ๋น๋ฐ๋ฒํธ : " + user.getPw());
//ํ์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ํธํํ์ฌ user๊ฐ์ฒด์ ๋ค์ ์ ์ฅ
String securePw = encoder.encode(user.getPw());
user.setPw(securePw);
System.out.println("์ํธํ ํ ๋น๋ฐ๋ฒํธ : " + user.getPw());
usersDAO.modifyPw(user);
}
๋น๋ฐ๋ฒํธ ์ํธํ์ฒ๋ฆฌ๋ฅผ service๋จ์์ ์ฒ๋ฆฌํด์ค๋ค.
View
pw-change.jsp
$.ajax({
type: "POST",
url: "/myapp/user/checkPw",
headers: {
"Content-Type": "application/json",
"X-HTTP-Method-Override": "POST"
},
data: pw,
datatype: "json",
success: function(result) {
console.log(result);
if(result === "pwConfirmOK") {
$('#pwMsg').html('');
chk1 = true;
} else {
$('#pwMsg').html('');
chk1 = false;
}
},
error : function(error) {
console.log("error : " + error);
}
});
ํ์ฌ ๋น๋ฐ๋ฒํธ๊ฐ ๋ง๋์ง ์์ฒญํ๊ธฐ
//์๋ก์ด ๋น๋ฒ
$('#newPw').on('keyup', function() {
//๋น๋ฐ๋ฒํธ ๊ณต๋ฐฑ ํ์ธ
if($("#newPw").val() === ""){
$('#newPwMsg').html('<b>๋น๋ฐ๋ฒํธ๋ ํ์ ์ ๋ณด์
๋๋ค.</b>');
chk2 = false;
}
//๋น๋ฐ๋ฒํธ ์ ํจ์ฑ๊ฒ์ฌ
else if(!getPwCheck.test($("#newPw").val()) || $("#newPw").val().length < 8){
$('#newPwMsg').html('<b>ํน์๋ฌธ์ ํฌํจ 8์ ์ด์ ์
๋ ฅํ์ธ์</b>');
chk2 = false;
} else {
$('#newPwMsg').html('');
chk2 = true;
}
}); //end of new password
//๋น๋ฐ๋ฒํธ ํ์ธ
$('#newPwCheck').on('keyup', function() {
if($("#newPwCheck").val() === "") {
$('#newPwMsg').html('<b">๋น๋ฐ๋ฒํธ ํ์ธ์ ํ์ ์ ๋ณด์
๋๋ค.</b>');
chk3 = false;
} else if( $("#newPw").val() != $("#newPwCheck").val() ) {
$('#newPwMsg').html('<b>๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.</b>');
chk3 = false;
} else {
$('#newPwMsg').html('');
chk3 = true;
}
});//end of passwordCheck
์๋ก์ด ๋น๋ฐ๋ฒํธ์ ๋น๋ฐ๋ฒํธ ํ์ธ์ด ์๋ก ์ผ์นํ๋์ง ํ์ธ
//๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ ์์ฒญ์ฒ๋ฆฌํ๊ธฐ
$('.emailChkBtn').click(function(e) {
if(chk1 == false) {
alert('ํ์ฌ ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ์ต๋๋ค.');
} else if(chk2 == false){
alert('2๋ฒ ํ๋ฆผ');
} else if(chk3 == false){
alert('3๋ฒ ํ๋ฆผ');
} else if(chk1 && chk2 && chk3) {
const userNo = $('#userNo').val();
const pw = $("#newPw").val();
const email = $("#email").val();
const id = $("#id").val();
const name = $('#name').val();
const user = {
userNo: userNo,
id: id,
pw: pw,
email: email,
name: name
};
console.log(user);
$.ajax({
type: "POST",
url: "/myapp/user/pw-change",
headers: {
"Content-Type": "application/json",
"X-HTTP-Method-Override": "POST"
},
dataType: "text",
data: JSON.stringify(user),
success: function(result) {
console.log("result: " + result);
if(result === "changeSuccess") {
alert('๋น๋ฐ๋ฒํธ๊ฐ ๋ณ๊ฒฝ๋์์ต๋๋ค.');
location.href ="/myapp/user/pw-change";
} else {
alert('ํ์ฌ ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ์ต๋๋ค.');
}
}
});
} else {
alert('์
๋ ฅ์ ๋ณด๋ฅผ ๋ค์ ํ์ธํ์ธ์.');
}
});
๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ ์์ฒญ ์ฒ๋ฆฌ
'Project > Instagram' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring] ํด๋ก ์ฝ๋ฉ11 User - ํ์ ํํด (0) | 2020.04.12 |
---|---|
[spring] ํด๋ก ์ฝ๋ฉ10 User - ๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ (0) | 2020.04.09 |
[spring] ํด๋ก ์ฝ๋ฉ08 User - ํ๋กํ ์ฌ์ง ๋ฑ๋ก, ์กฐํ, ์ญ์ (0) | 2020.04.08 |
[spring] ํด๋ก ์ฝ๋ฉ07 User - ํ์์ ๋ณด ๋ณ๊ฒฝ (0) | 2020.04.08 |
[spring] ํด๋ก ์ฝ๋ฉ06 User - ๋ก๊ทธ์์ (0) | 2020.04.08 |
- Total
- Today
- Yesterday
- tomcat์ค์น
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- ๊ฒ์๋ฌผ์กฐํ
- Algorithm
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- ๊ฒ์๋ฌผ ์ญ์
- ๋ณ๋ช ์ฒ๋ฆฌ
- java jdk ์ค์น
- ๊ฒ์ํ ์ญ์
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- ์๋ฃ๊ตฌ์กฐ
- mysql์ค์น
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ๊ฒ์ํ ์กฐํ
- ์๋ฐ
- java ํ๊ฒฝ๋ณ์
- ๊ฐ๋ฐ
- ๋ถํธ ์๋์์ฑ
- ์๊ณ ๋ฆฌ์ฆ
- Java
- ์จ๋ฆฌ์์ค
- typeAliases
- ์ดํด๋ฆฝ์ค ์ค์น
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 31 |