-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
525b984
commit 1baed30
Showing
10 changed files
with
744 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/main/java/com/mab/user/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/** | ||
* @author 강경석 | ||
* 로그인,로그아웃 | ||
* 회원 탈퇴 | ||
* 아이디, 비밀번호 찾기 | ||
*/ | ||
package com.mab.user.controller; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.mab.user.model.EmailVO; | ||
import com.mab.user.model.UserVO; | ||
import com.mab.user.service.UserService; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Api(tags = "유저 컨트롤러") | ||
@Slf4j | ||
@Controller | ||
@RequestMapping("/rence") | ||
public class UserController { | ||
|
||
@Autowired | ||
UserService service; | ||
|
||
@Autowired | ||
HttpSession session; | ||
|
||
@Autowired | ||
UserSendEmail authSendEmail; | ||
|
||
// 자동 개행 및 줄 바꿈 (new Gson은 일자로 나옴) | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
|
||
// ********************** | ||
// 로그인 완료 | ||
// ********************** | ||
@ApiOperation(value = "로그인 성공", notes = "로그인 성공 입니다") | ||
@PostMapping("/loginSuccess") | ||
@ResponseBody | ||
public String user_loginOK(@RequestParam String username, HttpServletResponse response) { | ||
log.info("user_loginOK ()..."); | ||
log.info("username: {}", username); | ||
|
||
// 로그인 성공시 기존의 유저관련쿠키 제거 | ||
Cookie cc = new Cookie("user_no", null); // choiceCookieName(쿠키 이름)에 대한 값을 null로 지정 | ||
Cookie cc2 = new Cookie("user_image", null); | ||
cc.setMaxAge(0); // 유효시간을 0으로 설정 | ||
cc2.setMaxAge(0); | ||
response.addCookie(cc); // 응답 헤더에 추가해서 없어지도록 함 | ||
response.addCookie(cc2); | ||
|
||
UserVO uvo = service.user_login_info(username); | ||
|
||
Map<String, String> map = new HashMap<String, String>(); | ||
|
||
session.setAttribute("user_id", uvo.getUser_id()); | ||
|
||
Cookie cookie = new Cookie("user_no", uvo.getUser_no()); // 고유번호 쿠키 저장 | ||
Cookie cookie2 = new Cookie("user_image", uvo.getUser_image()); // 고유번호 쿠키 저장 | ||
cookie.setPath("/"); | ||
cookie2.setPath("/"); | ||
response.addCookie(cookie); | ||
response.addCookie(cookie2); | ||
|
||
log.info("User Login success....."); | ||
map.put("result", "1"); // 로그인 성공 | ||
|
||
String jsonObject = gson.toJson(map); | ||
|
||
return jsonObject; | ||
} | ||
|
||
// ********************** | ||
// 로그인 실패 | ||
// ********************** | ||
@ApiOperation(value = "로그인 실패", notes = "로그인 실패 입니다") | ||
@PostMapping("/loginFail") | ||
@ResponseBody | ||
public String user_loginFail(UserVO uvo, HttpServletResponse response) { | ||
log.info("user_loginFail ()..."); | ||
log.info("result: {}", uvo); | ||
|
||
Map<String, String> map = new HashMap<String, String>(); | ||
|
||
log.info("User Login failed....."); | ||
map.put("result", "0"); // 로그인 실패 | ||
|
||
String jsonObject = gson.toJson(map); | ||
|
||
return jsonObject; | ||
} | ||
|
||
// ********************** | ||
// 아이디 찾기 | ||
// ********************** | ||
@ApiOperation(value = "아이디 찾기", notes = "아이디 찾기 입니다") | ||
@PostMapping("/find_id") | ||
@ResponseBody | ||
public String user_find_id(UserVO uvo, EmailVO evo) { | ||
log.info("user_find_id ()..."); | ||
log.info("result: {}", uvo); | ||
|
||
Map<String, String> map = new HashMap<String, String>(); | ||
|
||
UserVO uvo2 = service.user_email_select(uvo); | ||
log.info("uvo2: {}", uvo2); | ||
if (uvo2 != null) { | ||
uvo2 = authSendEmail.findId(uvo2, evo); // 유저의 메일로 아이디 전송 | ||
|
||
if (uvo2 != null) { | ||
log.info("user_fine_id successed..."); | ||
map.put("result", "1"); | ||
|
||
} else { | ||
log.info("user_fine_id failed..."); | ||
map.put("result", "0"); | ||
} | ||
} | ||
|
||
String jsonObject = gson.toJson(map); | ||
|
||
return jsonObject; | ||
} | ||
|
||
// ********************** | ||
// 비밀번호 찾기 | ||
// ********************** | ||
// 사용자가 비밀번호를 찾으면 초기화된 비밀번호를 이메일로 전송,데이터베이스에는 초기화된 비번 저장 | ||
// @ApiOperation(value = "비밀번호 찾기", notes = "비밀번호 찾기 입니다") | ||
// @PostMapping("/find_pw") | ||
// @ResponseBody | ||
// public String user_find_pw(UserVO uvo, EmailVO evo) { | ||
// log.info("user_find_pw ()..."); | ||
// log.info("result{}", uvo); // 넘어오는 값 출력 | ||
// | ||
// UserVO uvo2 = service.user_id_email_select(uvo); // 아이디 이메일 체크 | ||
// Map<String, String> map = new HashMap<String, String>(); | ||
// if (uvo2 != null) { | ||
// // uvo2가 null이 아니면(테이블에 데이터가 존재하면) 메일을 통해 수정링크 제공 | ||
// uvo2 = authSendEmail.findPw(uvo2, evo); | ||
// log.info("비밀번호 찾기 메일 전송완료"); | ||
// int result = service.user_pw_init(uvo2); | ||
// log.info("비밀번호 초기화 업데이트 완료"); | ||
// | ||
// if (result != 0) { | ||
// log.info("user_fine_pw successed..."); | ||
// map.put("result", "1"); | ||
// } else { | ||
// log.info("user_fine_pw failed..."); | ||
// map.put("result", "0"); | ||
// } | ||
// | ||
// } | ||
// String jsonObject = gson.toJson(map); | ||
// return jsonObject; | ||
// } | ||
|
||
}// end class |
131 changes: 131 additions & 0 deletions
131
src/main/java/com/mab/user/controller/UserSendEmail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* @author 강경석 | ||
* 이메일 인증 및 아이디/비밀번호 찾기 | ||
*/ | ||
|
||
package com.mab.user.controller; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.internet.InternetAddress; | ||
import javax.mail.internet.MimeMessage; | ||
import javax.mail.internet.MimeMessage.RecipientType; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.mab.user.model.AuthVO; | ||
import com.mab.user.model.EmailVO; | ||
import com.mab.user.model.UserVO; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
public class UserSendEmail { | ||
|
||
@Autowired | ||
JavaMailSender javaMailSender; | ||
|
||
// ********************** | ||
// 회원가입 이메일 인증 | ||
// ********************** | ||
public AuthVO sendEmail(AuthVO vo, EmailVO evo) { | ||
log.info("User sendEmail"); | ||
log.info("vo: {}", vo); | ||
|
||
// 이메일 제목, 내용 설정 | ||
evo.setSubject("[rence] 회원가입 이메일 인증코드"); | ||
|
||
// 인증코드 생성 | ||
int RANDOM_BOUND = 100000; | ||
ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
vo.setAuth_code(String.valueOf(random.nextInt(RANDOM_BOUND, RANDOM_BOUND * 10))); | ||
log.info("RANDOM_BOUND: {}", RANDOM_BOUND); | ||
try { | ||
|
||
// 전송 | ||
MimeMessage msg = javaMailSender.createMimeMessage(); | ||
msg.setSubject(evo.getSubject()); | ||
msg.setContent( | ||
"안녕하세요. "+ "<br><br>" | ||
+ "회원가입 인증코드는 다음과 같습니다." + "<br><br>" + "<strong>인증 코드</strong> : " + vo.getAuth_code(), | ||
"text/html; charset=utf-8"); | ||
|
||
msg.setRecipient(RecipientType.TO, new InternetAddress(vo.getUser_email())); | ||
|
||
javaMailSender.send(msg); | ||
} catch (MessagingException e) { | ||
vo = null; | ||
} | ||
return vo; | ||
} | ||
|
||
// ********************** | ||
// 아이디 찾기 | ||
// ********************** | ||
public UserVO findId(UserVO uvo, EmailVO evo) { | ||
log.info("User findId"); | ||
log.info("uvo: {}", uvo); | ||
// 이메일 제목, 내용 설정 | ||
evo.setSubject("[rence] User 아이디 재설정"); | ||
|
||
try { | ||
// 전송 | ||
MimeMessage msg = javaMailSender.createMimeMessage(); | ||
msg.setSubject(evo.getSubject()); | ||
msg.setContent("안녕하세요. "+ "<br><br>" | ||
+ uvo.getUser_name() + " 회원님의 아이디는 다음과 같습니다" + | ||
|
||
"<br><br>" + "<strong>아이디 : </strong>" + uvo.getUser_id(), "text/html; charset=utf-8"); | ||
msg.setRecipient(RecipientType.TO, new InternetAddress(uvo.getUser_email())); | ||
|
||
javaMailSender.send(msg); | ||
} catch (MessagingException e) { | ||
uvo = null; | ||
} | ||
return uvo; | ||
} | ||
|
||
// ********************** | ||
// 비밀번호 찾기 | ||
// ********************** | ||
public UserVO findPw(UserVO uvo, EmailVO evo) { | ||
log.info("User findPw"); | ||
log.info("uvo: {}", uvo); | ||
|
||
String random_pw = RandomStringUtils.randomAlphanumeric(10); | ||
log.info("Create random_pw: {}", random_pw); | ||
|
||
// 비밀번호 암호화 | ||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); | ||
uvo.setPassword(encoder.encode(random_pw)); | ||
log.info("uvo.getUser_pw: {}", uvo.getPassword()); | ||
|
||
// 이메일 제목, 내용 설정 | ||
evo.setSubject("[rence] 비밀번호 재설정"); | ||
|
||
try { | ||
// 전송 | ||
MimeMessage msg = javaMailSender.createMimeMessage(); | ||
msg.setSubject(evo.getSubject()); | ||
msg.setContent( | ||
"안녕하세요. "+ "<br><br>"+ | ||
uvo.getUser_name() + " 회원님의 초기화된 비밀번호는 다음과 같습니다" + "<br><br>" + "<font color=\"red\">로그인후 재설정을 권장합니다 </font>" | ||
+ "<br><br>" + "<strong>초기화 비밀번호 : </strong>" + random_pw, | ||
"text/html; charset=utf-8"); | ||
msg.setRecipient(RecipientType.TO, new InternetAddress(uvo.getUser_email())); | ||
|
||
javaMailSender.send(msg); | ||
} catch (MessagingException e) { | ||
uvo = null; | ||
} | ||
|
||
return uvo; | ||
} | ||
|
||
}// end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @author 강경석 | ||
*/ | ||
package com.mab.user.model; | ||
|
||
import java.util.Date; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.SequenceGenerator; | ||
import javax.persistence.Table; | ||
|
||
import org.hibernate.annotations.ColumnDefault; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
@Table(name="AUTH") | ||
public class AuthVO { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "seq_auth") | ||
@SequenceGenerator(sequenceName = "seq_auth",allocationSize = 1,name = "seq_auth") | ||
@Column(name="auth_no") | ||
private String auth_no; | ||
|
||
@Column(name="user_email") | ||
private String user_email; | ||
|
||
@Column(name="auth_code") | ||
private String auth_code; | ||
|
||
@Column(name="auth_stime", insertable= false, updatable = false) | ||
@ColumnDefault(value="current_date") | ||
private Date auth_stime; | ||
|
||
|
||
}//end class |
Oops, something went wrong.