코딩 연습장/Spring

Spring 공유 캘린더 프로젝트 - 4 (비밀 번호 찾기)

Do아 2021. 6. 24. 16:51
728x90

2021/05/25(화)

 

 

 

 

 

 

 

Spring 공유 캘린더 프로젝트 회원가입 알고리즘 정리

https://cordingdoah.tistory.com/114

 

Spring 공유 캘린더 프로젝트 - 3 (회원 가입)

2021/05/25(화) Spring 공유 캘린더 프로젝트 로그인 알고리즘 참고 https://cordingdoah.tistory.com/112 Spring 공유 캘린더 프로젝트 - 2 (로그인) 2021/05/25(화) Spring 공유캘린더 프로젝트 기본설정 참고 h..

cordingdoah.tistory.com

 

 

 

 

비밀번호 찾기를 구현하도록 하겠다

controller, member.java, xml 등은 회원가입 알고리즘을 참고

 

 

 

 

<LoginController.java>

- 전체 컨트롤러

package org.zerock.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.alpreah.domain.member;
import org.alpreah.persistence.member_dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class LoginController {

	@Autowired
	private member_dao m_dao;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@ResponseBody
	@RequestMapping(value = "Login", method = RequestMethod.POST)
	public int login(member m, HttpSession session) {
		return m_dao.Login(m, session);
	}

	/* 로그인 화면 --> 비밀번호 찾기 버튼 */
	@RequestMapping("/pwFind")
	public String pwFind() {
		return "pwFind";
	}

	/* 비밀번호 찾기 화면 --> 확인 버튼 */
	@ResponseBody
	@RequestMapping(value = "PwFind", method = RequestMethod.POST)
	public int pwFind(member m, HttpSession session) {
		return m_dao.pwFind(m, session);
	}

	/* 아이디와 힌트, 답이 일치하면 전환되는 화면 */
	@RequestMapping("/pw")
	public String pw() {
		return "pw";
	}

	/* 로그인 화면 --> 회원가입 버튼 클릭 */
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register() {
		return "register";
	}

	/* 회원가입 화면 --> 확인 버튼 */
	@ResponseBody
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public int register(member m) {
		return m_dao.Register(m);
	}

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "index";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute("m");
		return "redirect:/";
	}

	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String show() {
		return "show";
	}
	
	@ControllerAdvice
	public class JsonpAdviceController extends AbstractMappingJacksonResponseBodyAdvice {
		public JsonpAdviceController() {
			super();
		}

		@Override
		protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
				MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
			// TODO Auto-generated method stub

		}
	}

	@Bean
	public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		jsonConverter.setObjectMapper(objectMapper);
		return jsonConverter;
	}

}

 

 

비밀번호 찾기와 관련코드

	/* 로그인 화면 --> 비밀번호 찾기 버튼 */
	@RequestMapping("/pwFind")
	public String pwFind() {
		return "pwFind";
	}

	/* 비밀번호 찾기 화면 --> 확인 버튼 */
	@ResponseBody
	@RequestMapping(value = "PwFind", method = RequestMethod.POST)
	public int pwFind(member m, HttpSession session) {
		return m_dao.pwFind(m, session);
	}

	/* 아이디와 힌트, 답이 일치하면 전환되는 화면 */
	@RequestMapping("/pw")
	public String pw() {
		return "pw";
	}

 

 


 

 

<member_dao.java>

package org.alpreah.persistence;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.alpreah.domain.member;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@SuppressWarnings("unused")
@Repository
public class member_dao {
	@Autowired
	private SqlSession session;
	private static final String NameSpace = "memberMapper.";
	
	public int Id_Check(member m) {
		try {
			return session.selectOne(NameSpace + "Id_Check", m);
		}
		catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public int Register(member m) {
		int Id_Check_Result = Id_Check(m);
		if(Id_Check_Result != 0) return Id_Check_Result;
		
		try {
			session.insert(NameSpace + "Register", m);
			return 0;
		}
		catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}
	
	public int Login(member m, HttpSession Hsession) {
		int Id_Search_Result = -1;
		try {
			Id_Search_Result = session.selectOne(NameSpace + "Login", m);
		}
		catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		
		if(Id_Search_Result != 1) return Id_Search_Result;
		
		try {
			member m_info = session.selectOne(NameSpace + "Login_Info", m);
			Hsession.setAttribute("m", m_info);
			return 1;
		}
		catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}	
	public int pwFind(member m, HttpSession Hsession) {
		int Id_Search_Result = -1;
		try {
			Id_Search_Result = session.selectOne(NameSpace + "Pw_find", m);
		}
		catch (Exception e) {
			 e.printStackTrace(); 
		
			return -1;
		}
		
		if(Id_Search_Result != 1) return Id_Search_Result;
		
		try {
			member m_info = session.selectOne(NameSpace + "Pw", m);
			Hsession.setAttribute("m", m_info);
			return 1;
		}
		catch (Exception e) {
			e.printStackTrace(); 
			return -2;
		}
	}
}

 

 

 

비밀번호 찾기와 관련코드

	public int pwFind(member m, HttpSession Hsession) {
		int Id_Search_Result = -1;
		try {
			Id_Search_Result = session.selectOne(NameSpace + "Pw_find", m);
		}
		catch (Exception e) {
			 e.printStackTrace(); 
		
			return -1;
		}
		
		if(Id_Search_Result != 1) return Id_Search_Result;
		
		try {
			member m_info = session.selectOne(NameSpace + "Pw", m);
			Hsession.setAttribute("m", m_info);
			return 1;
		}
		catch (Exception e) {
			e.printStackTrace(); 
			return -2;
		}
	}

 

 


 

 

<memberMapper.xml>

- mapper.xml 전체 코드

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="memberMapper">

	<!-- ID 중복체크 -->
	<select id="Id_Check" resultType="int">
		select count(*) from member m
		where m.m_id = #{m_id}
	</select>

	<!-- 회원가입 -->
	<insert id='Register'>
		INSERT INTO member (m_name, m_id, m_pw, m_pwc,
		m_hint, m_anw)
		VALUES (#{m_name}, #{m_id}, #{m_pw}, #{m_pwc},
		#{m_hint}, #{m_anw})
	</insert>

	<select id="Login" resultType="int">
		select count(*) from member m
		where m.m_id = #{m_id} and m.m_pw = #{m_pw}
	</select>

	<select id="Login_Info" resultType="Member">
		select * from member m
		where
		m.m_id = #{m_id} and m.m_pw = #{m_pw}
	</select>

	<!-- 입력한 id와 힌트, 대답을 가지고 조회했을 때 갯수 출력 -->
	<select id="Pw_find" resultType="int">
		select count(*) from member m
		where m.m_id = #{m_id} and m.m_hint = #{m_hint} and m.m_anw = #{m_anw}
	</select>

	<!-- 입력한 id와 힌트, 대답과 일치하는 회원의 모든 정보 출력 -->
	<select id="Pw" resultType="Member">
		select * from member m
		where m.m_id =
		#{m_id} and m.m_hint = #{m_hint} and m.m_anw = #{m_anw}
	</select>
</mapper>

 

 

비밀번호 찾기와 관련코드

	<!-- 입력한 id와 힌트, 대답을 가지고 조회했을 때 갯수 출력 -->
	<select id="Pw_find" resultType="int">
		select count(*) from member m
		where m.m_id = #{m_id} and m.m_hint = #{m_hint} and m.m_anw = #{m_anw}
	</select>

	<!-- 입력한 id와 힌트, 대답과 일치하는 회원의 모든 정보 출력 -->
	<select id="Pw" resultType="Member">
		select * from member m
		where m.m_id =
		#{m_id} and m.m_hint = #{m_hint} and m.m_anw = #{m_anw}
	</select>

 

 


 

 

<pwFind.jsp>

- 비밀번호 찾기화면

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.css"
	rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<meta charset="UTF-8">
<title>비번찾기 데모</title>
</head>
<body>
	<div id="login">
		<h3 class="text-center text-white pt-5">Password Find form</h3>
		<div class="container">
			<div id="login-row"
				class="row justify-content-center align-items-center">
				<div id="login-column" class="col-md-6">
					<div id="login-box" class="col-md-12">
						<form id="login-form" class="form" action="" method="post">
							<h3 class="text-center text-info">Password Search</h3>
							<div class="form-group">
								<label for="m_id" class="text-info">Id:</label><br> <input
									type="text" name="m_id" id="m_id" class="form-control" placeholder="ID">
							</div>
							<div class="form-group">
								<label for="m_hint" class="text-info">Password Hint:</label><br>
								<select id="m_hint" name="m_hint" class="form-control">
									<option>Password Search Hint</option>
									<option>What year were you born?</option>
									<option>Birth region</option>
									<option>My Treasure No. 1</option>
									<option>Graduated elementary school</option>
									<option>the place of one's family register</option>
								</select>
							</div>
							<div class="form-group">
								<label for="m_anw" class="text-info">Answer:</label><br> <input
									type="text" name="m_anw" id="m_anw" class="form-control" placeholder="Answer">
							</div>
							<div class="form-group" style="display:flex;">
								<button id="pwfind_process" type="button"
									class="btn btn-info btn-md" style="margin: 0 auto;">Submit</button>
							</div>
							<div id="login-link" class="text-right">
								<a href="/" class="text-info">Login here</a>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script
		src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
	<script>
		$(document).ready(function() {
			$("#pwfind_process").click(function() {
				var json = {
					m_id : $("#m_id").val(),
					m_hint : $("#m_hint").val(),
					m_anw : $("#m_anw").val()
				};

				for ( var str in json) {
					if (json[str].length == 0) {
						alert($("#" + str).attr("placeholder") + "를 입력해주세요.");
						$("#" + str).focus();
						return;
					}
				}

				$.ajax({
					type : "post",
					url : "PwFind",
					data : json,
					success : function(data) {
						switch (Number(data)) {
						case 0:
							alert("정보가 일치하지 않습니다.");
							break;
						case 1:
							window.location.href = "/pw";
						default:
							break;
						}
					},
					error : function(error) {
						alert("오류 발생" + error);
					}
				});
			});
		});
	</script>
</body>
</html>

입력한 아이디와 힌트, 대답을 가지고 db조회 후 /pw로 화면전환

 

 

--> 결과화면

 

 


 

 

<pw.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.css"
	rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<div id="login">
		<h3 class="text-center text-white pt-5">PW form</h3>
		<div class="container">
			<div id="login-row"
				class="row justify-content-center align-items-center">
				<div class="form-group">
					<label for="password" class="text-info">Password</label><br>
					<label for="password" class="text-info">${sessionScope.m.m_pw}</label><br>
					<button id="back_to_login" type="button"
						class="btn btn-info btn-md" onclick="location.href = '/'">Go
						Login</button>
				</div>
			</div>
		</div>
	</div>
</body>
</html>

회원의 패스워드값을 출력해주는 페이지

 

 

--> 결과화면

비밀번호 출력

 

 

 

 

 

 

728x90