Spring 비밀번호 찾기 기능 변경 (내 프로젝트 적용)

Back-End/Spring 2019. 8. 28. 16:40

- 현재(비밀번호 찾기)-


아이디와 이메일을 입력하면 비밀번호가 페이지에 출력됨




- 변경(비밀번호 변경) -


-> 아이디와 이메일을 입력한 후 확인 버튼을 누르면 해당 아이디와 이메일이 db에 저장되어 있을 경우에 입력한 이메일로 인증번호를 보내서 

인증번호를 입력하게 한 후에

인증번호가 맞을 경우에 비밀번호 변경 페이지로 이동하게해서 비밀번호를 변경하게한후에 db에 다시 변경한 비밀번호를 저장하게 할 예정



  view

  ㄴpass_email.jsp

  ㄴpass_change.jsp


  controller

  ㄴMemberController.java


  model

  ㄴMemberDAO.java

  ㄴMemberDAOImpl.java


  service

  ㄴMemberBoardService.java

  ㄴMemberBoardServiceImpl.java





pass_change.jsp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%@ include file="../include/header.jsp"%>
<%@ include file="../include/menu.jsp"%><br>
<body>
 
<table border="1" width="300" height="300" align= "center">
<center>
<span style="color: green; font-weight: bold;">변경할 비밀번호를 입력해주세요.</span> <br> <br>    
        <br> <br>
        
        
        <div style="text-align:center;">
            <tr>        
                <td>
                <center>
                    <form action="pass_change.do${e_mail}" method="post">
                    
                    <center>
                        <br>
                        <div>
                            변경할 비밀번호 입력 : <input type = "number" name = "member_pass"
placeholder = "비밀번호를 입력하세요." >
                        </div>                                        
 
                        <br> <br>
                        <button type="submit" name="submit">비밀번호 변경</button>
 
                        </div>
                    </td>
                </tr>
                    </center>
            </table>
        </form>
</center>
 
 
</body>
</html>
cs



pass_email.jsp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%@ include file="../include/header.jsp"%>
<%@ include file="../include/menu.jsp"%><br>
<body>
 
<table border="1" width="300" height="300" align"center">
<center>
<span style="color: green; font-weight: bold;">입력한 이메일로 받은 인증번호를 입력하세요. (인증번호가 맞아야 비밀번호를 변경하실 수 있습니다.)</span> <br> <br>    
        <br> <br>
        
        
        <div style="text-align:center;">
            <tr>        
                <td>
                <center>
                    <form action="pass_injeung.do${dice},${e_mail}" method="post">
                    
                    <center>
                        <br>
                        <div>
                            인증번호 입력 : <input type="number" name="pass_injeung"
                                placeholder="  인증번호를 입력하세요. ">
                        </div>                                        
 
                        <br> <br>
                        <button type="submit" name="submit">인증번호 전송</button>
 
                        </div>
                    </td>
                </tr>
                    </center>
            </table>
        </form>
</center>
 
 
</body>
</html>
cs



MemberController.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//비밀번호 찾기 처리 (1) 이메일 발송
        @RequestMapping(value = "find_pass.do", method = RequestMethod.POST)
        public ModelAndView find_pass(HttpServletRequest request, String user_id, String e_mail,
                HttpServletResponse response_email) throws IOException{
            
            //랜덤한 난수 (인증번호)를 생성해서 이메일로 보내고 그 인증번호를 입력하면 비밀번호를 변경할 수 있는 페이지로 이동시킴
            
            Random r = new Random();
            int dice = r.nextInt(157211)+48271;
            
            String setfrom = "dlgkstjq623@gmail.com";
            String tomail = request.getParameter("e_mail");    //받는 사람의 이메일
            String title = "비밀번호 찾기 인증 이메일 입니다.";    //제목
            String content =
            
                    System.getProperty("line.separator")+
                    
                    System.getProperty("line.separator")+
                            
                    "안녕하세요 회원님 저희 홈페이지를 찾아주셔서 감사합니다"
                    
                    +System.getProperty("line.separator")+
                    
                    System.getProperty("line.separator")+
            
                    "비밀번호 찾기 인증번호는 " +dice+ " 입니다. "
                    
                    +System.getProperty("line.separator")+
                    
                    System.getProperty("line.separator")+
                    
                    "받으신 인증번호를 홈페이지에 입력해 주시면 다음으로 넘어갑니다."// 내용
            
            try {
 
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper messageHelper = new MimeMessageHelper(message, true"UTF-8");
 
                messageHelper.setFrom(setfrom); // 보내는사람 생략하면 정상작동을 안함
                messageHelper.setTo(tomail); // 받는사람 이메일
                messageHelper.setSubject(title); // 메일제목은 생략이 가능하다
                messageHelper.setText(content); // 메일 내용
                
                mailSender.send(message);
        
            } catch (Exception e) {
                System.out.println(e);
            }
            
            
            ModelAndView mv = new ModelAndView();    //ModelAndView로 보낼 페이지를 지정하고, 보낼 값을 지정한다.
            mv.setViewName("/member/pass_email");     //뷰의이름
            mv.addObject("dice", dice);
            mv.addObject("e_mail", e_mail);
            
            System.out.println("mv : "+mv);
 
            response_email.setContentType("text/html; charset=UTF-8");
            PrintWriter out_email = response_email.getWriter();
            out_email.println("<script>alert('이메일이 발송되었습니다. 인증번호를 입력해주세요.');</script>");
            out_email.flush();
            
            
            return mv;
            
        }
        
        
        //인증번호를 입력한 후에 확인 버튼을 누르면 자료가 넘어오는 컨트롤러
        @RequestMapping(value = "pass_injeung.do{dice},{e_mail}", method = RequestMethod.POST)
            public ModelAndView pass_injeung(String pass_injeung, @PathVariable String dice, @PathVariable String e_mail, 
                    HttpServletResponse response_equals) throws IOException{
            
            System.out.println("마지막 : pass_injeung : "+pass_injeung);
            
            System.out.println("마지막 : dice : "+dice);
            
            ModelAndView mv = new ModelAndView();
            
            mv.setViewName("/member/pass_change");
            
            mv.addObject("e_mail",e_mail);
            
            if (pass_injeung.equals(dice)) {
                
                //인증번호가 일치할 경우 인증번호가 맞다는 창을 출력하고 비밀번호 변경창으로 이동시킨다
            
                mv.setViewName("member/pass_change");
                
                mv.addObject("e_mail",e_mail);
                
                //만약 인증번호가 같다면 이메일을 비밀번호 변경 페이지로 넘기고, 활용할 수 있도록 한다.
                
                response_equals.setContentType("text/html; charset=UTF-8");
                PrintWriter out_equals = response_equals.getWriter();
                out_equals.println("<script>alert('인증번호가 일치하였습니다. 비밀번호 변경창으로 이동합니다.');</script>");
                out_equals.flush();
        
                return mv;
                
                
            }else if (pass_injeung != dice) {
                
                
                ModelAndView mv2 = new ModelAndView(); 
                
                mv2.setViewName("member/pass_email");
                
                response_equals.setContentType("text/html; charset=UTF-8");
                PrintWriter out_equals = response_equals.getWriter();
                out_equals.println("<script>alert('인증번호가 일치하지않습니다. 인증번호를 다시 입력해주세요.'); history.go(-1);</script>");
                out_equals.flush();
                
        
                return mv2;
                
            }    
        
            return mv;
            
        }
        
        
        
        //변경할 비밀번호를 입력한 후에 확인 버튼을 누르면 넘어오는 컨트롤러
        @RequestMapping(value = "pass_change.do{e_mail}", method = RequestMethod.POST)
        public ModelAndView pass_change(@PathVariable String e_mail, HttpServletRequest request, MemberDTO dto, HttpServletResponse pass) throws Exception{
                    
        String member_pass = request.getParameter("member_pass");
                    
        String e_mail1 = e_mail;
                    
        dto.setE_mail(e_mail1);
        dto.setMember_pass(member_pass);
        
        //값을 여러개 담아야 하므로 해쉬맵을 사용해서 값을 저장함
        
        Map<String, Object> map = new HashMap<>();
        
        map.put("e_mail", dto.getE_mail());
        map.put("member_pass", dto.getMember_pass());
        
        memberservice.pass_change(map,dto);
        
        ModelAndView mv = new ModelAndView();
        
        mv.setViewName("member/find_pass_result");
        
        return mv;
                    
        }
cs




MemberService.java 중 일부

1
2
3
4
@Override
    public void pass_change(Map<String, Object> map, MemberDTO dto) throws Exception {
        memberdao.pass_change(map,dto);
    }
cs




MemberDAOImpl.java 중 일부

1
2
3
4
5
6
7
8
@Override
    public void pass_change(Map<String, Object> map, MemberDTO dto)throws Exception{
        
        map.get("member_pass");
        map.get("e_mail");
 
        sqlSession.update("member.pass_change", map);
    }
cs



memberMapper.xml 중 일부

1
2
3
4
5
6
<!-- 비밀번호 변경 관련 mapper -->
    
    <update id = "pass_change" parameterType="hashMap">
    update member set member_pass=#{member_pass} where e_mail=#{e_mail}
 
    </update>
cs






: