FrameWorks/Spring & Boot

Spring Boot에서 email인증하기

ABCD 2023. 2. 7.

 

ViewPage

비밀번호 찾기 form

  • 해당 input에 적절하게 입력하지 않을 시 css로 error효과를 넣기위해 div 삽입
1
2
3
4
5
6
7
8
9
10
11
12
13
<form class = "content" action="pw_auth.me" method="post">
    <div class="textbox">
          <input id="text" name=name required="" type="text" />
          <label for="text">이름</label>
           <div class="error">이름을 입력하세요  </div>
     </div>
    <div class="textbox">
          <input id="email" name=email required="/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)?$/i" type="email" />
          <label for="email">이메일</label>
      <div class="error">유효하지 않은 이메일주소 입니다  </div>
    </div><br><br>
       <input type="submit" id="check" value="비밀번호찾기">
</form>
cs

 

이메일 인증번호 확인 form

1
2
3
4
5
6
7
8
9
<form action="pw_set.me" method="post">
    <input type="hidden" name ="num" value="${num }">
        <div class=content>
            <div class="textbox">
            <input type="text" name="email_injeung" placeholder="인증번호를 입력하세요"><label>인증번호 </label>
            <div class="error"> </div>
        </div><br><br>
            <input type="submit" id="check" value="확인">
</form>
cs

 

비밀번호 재설정 form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<form action="pw_new.me" method="POST" class="content">
    <div class="textbox">
        <input id="pw" name="pw" type="password"><label>새비밀번호</label>
        <div class="error">
                 Invalid password
        </div> 
    </div>
    <div class="textbox">
        <input id="pw2" type="password" onchange="isSame();"><label>새비밀번호 확인</label>
        <div class="error">
                Invalid password
        </div>
    </div>
    <span id=same></span>
    <br><br>
    <input type="submit" id="check" value="비밀번호변경">
    <input type="hidden" name="email" value=<%=email %>>
</form>
cs

 

인증번호 발송 Controller

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
@RequestMapping(value = "/pw_auth.me")
    public ModelAndView pw_auth(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String email = (String)request.getParameter("email");
        String name = (String)request.getParameter("name");
 
        MemberVO vo = memberSV.selectMember(email);
            
        if(vo != null) {
        Random r = new Random();
        int num = r.nextInt(999999); // 랜덤난수설정
        
        if (vo.getName().equals(name)) {
            session.setAttribute("email", vo.getEmail());
 
            String setfrom = "ivedot@naver.com"// naver 
            String tomail = email; //받는사람
            String title = "[삼삼하개] 비밀번호변경 인증 이메일 입니다"
            String content = System.getProperty("line.separator"+ "안녕하세요 회원님" + System.getProperty("line.separator")
                    + "삼삼하개 비밀번호찾기(변경) 인증번호는 " + num + " 입니다." + 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.getMessage());
            }
 
            ModelAndView mv = new ModelAndView();
            mv.setViewName("YM/pw_auth");
            mv.addObject("num", num);
            return mv;
        }else {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("YM/pw_find");
            return mv;
        }
        }else {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("YM/pw_find");
            return mv;
        }
cs

 

인증번호 입력한 값이 동일하지 확인하는 Controller

1
2
3
4
5
6
7
8
9
10
11
@RequestMapping(value = "/pw_set.me", method = RequestMethod.POST)
public String pw_set(@RequestParam(value="email_injeung"String email_injeung,
            @RequestParam(value = "num"String num) throws IOException{
        
        if(email_injeung.equals(num)) {
            return "YM/pw_new";
        }
        else {
            return "YM/pw_find";
        }
//이메일 인증번호 확인
cs

 

새비밀번호 입력 후 DB업데이트 Controller

1
2
3
4
5
6
7
8
9
10
11
@RequestMapping(value = "/pw_new.me", method = RequestMethod.POST)
    public String pw_new(MemberVO vo, HttpSession session) throws IOException{
        int result = memberSV.pwUpdate_M(vo);
        if(result == 1) {
            return "jj/loginForm";
        }
        else {
            System.out.println("pw_update"+ result);
            return "YM/pw_new";
        }
}
cs

 

Mapper.xml

1
2
3
4
5
6
7
8
9
<!-- 회원정보조회 -->
<select id="selectMember" parameterType="String" resultType="MemberVO">
select * from member_list where email = #{email}
</select>
 
<!-- 패스워드변경 -->
<update id = "pwUpdate_M" parameterType="MemberVO">
update member_list set pw=#{pw} where email= #{email}
</update>
cs

 

pom.xml

1
2
3
4
5
  <!-- SpringBoot mailService -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
cs

 

Application.propertis

1
2
3
4
5
6
7
8
9
10
# spring-boot를 사용하면 JavaMailSender Bean을 생성할 필요 없이, 
# 자동으로 생성이 된다. 그렇기 때문에 properties에 smtp설정만 해주면 된다. 
# 일반적인 spring을 사용한다면 JavaMailSender bean을 생성하여, 
# bean에서 smtp 값들을 설정을 해줘야 한다. 
spring.mail.host=smtp.gmail.com 
spring.mail.port=587 
spring.mail.username=앱 비밀번호를 발급받은 구글 계정 예)sample.gmail.com 
spring.mail.password=앱 비밀번호 
spring.mail.properties.mail.smtp.starttls.enable=true 
spring.mail.properties.mail.smtp.auth=true
cs

 

Spring 사용시 root-context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<bean id="mailSender"
        class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="465" />
        <property name="username" value="보내는 사람 이메일" />
        <property name="password" value="이메일 비밀번호" />
        <property name="defaultEncoding" value="utf-8" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtps.ssl.checkserveridentity">true</prop>
                <prop key="mail.smtps.ssl.trust">*</prop>
                <prop key="mail.debug">true</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            </props>
        </property>
    </bean>
cs
728x90
반응형

댓글

💲 추천 글