-
이메일 인증 구현 / 병원 실시간 예약 기능 구현 프로젝트학원 프로젝트 2023. 5. 10. 16:42
환자가 병원에 진료를 예약할 수 있는 기능을 구현 할 때
본인 인증을 할 수 있도록 만들고 싶었다.
Pom.xml
Spring에서 지원하는 메일 전송 라이브러리를 pom.xml에 추가하고 maven을 업데이트해서 라이브러리를 다운로드 받았다.
root-context
이메일을 보낼 계정을 설정해줘야한다. 네이버, 구글 등의 계정이 가능하며,
root-context에 다음과 같이 계정에 대한 정보를 설정해야한다. 메일을 보낼 계정 또한 해당 기능을 허용하도록 설정 해야한다.
jsp
인증메일을 받을 이메일 주소를 전송할 수 있는 폼 태그가 필요하다.
Java Script
이메일 인증번호 받기
폼 태그를 전송하면, 사용자가 입력한 데이터를 이메일 형식으로 변환하고 변환된 이메일을 컨트롤러로 전송한다.
컨트롤러로 부터 랜덤하게 생성 된 인증번호를 리턴 받고 변수에 저장한다. 인증번호를 리턴 받지 못 했다면 이메일 전송에 실패한 것이다.
인증번호 확인
사용자가 입력한 인증번호가 없으면, alert 창을 띄운다 이 때 사용자가 입력한 인증번호를 .trim을 이용해서 공백을 없에고 확인한다.
사용자가 입력한 인증번호와, 변수에 저장했던 인증번호가 같을 경우 이메일 인증을 성공적으로 완료한 것이다.
Controller
ajax로 부터 이메일 데이터를 받으면, 6자리 랜덤한 인증번호를 생성한다
Random random = new Random(); int checkNum = random.nextInt(888888) + 111111;
랜덤한 난수를 생성한 후, .nextInt(888888) 를 이용해서 난수를 0 ~ 888888 까지의 수로 설정한 다음, 111111을 더해주면,
111111 ~ 999999 까지의 수로 난수가 설정된다. 이 방법으로 6자리 이상의 난수를 생성할 수 있다.
setFrom, toMail, title, content 에 인증 이메일의 내용을 구성하고
mailSender 객체를 사용하여 .createMimeMessage() 메서드를 호출하여 MimeMessage 객체를 생성한다.
MimeMessage 객체는 JavaMail API에서 이메일을 보내기 위한 필수 객체이다.
MimeMessageHelper 클래스를 생성해서 이메일의 내용을 설정한다. MimeMessageHelper 는 MimeMessage 객체를 이용하기 쉽게 도와준다.
작성이 완료 된 MimeMessage 를 mailSender 객체의 send() 메서드를 이용해서 전송한다.
결과
'학원 프로젝트' 카테고리의 다른 글
간호사 업무 해소 환자 위치 찾기 프로젝트 (2. 공공데이터 활용 ) (0) 2023.05.30 간호사 업무 해소 환자 위치 찾기 프로젝트 (1. Geolacation API ) (0) 2023.05.26 로그인 기능 구현하기 / 병원 실시간 예약 기능 구현 프로젝트 (0) 2023.05.10 ajax 를 이용해서 DB 이용하기 / 병원 실시간 예약 기능 구현 프로젝트 (0) 2023.05.09 팀프로젝트)예약 달력 만들기 (0) 2023.04.20