32. 회원정보 수정하기

yuzu sim's avatar
Feb 13, 2024
32. 회원정보 수정하기

1. 회원정보 변경 페이지 확인하기

  • password만 수정 가능함
  • 나머지 정보는 DB에 조회해서 넣어줘야 함
notion image
notion image
  • 원래는 민감한 정보를 제외한 정보만 class에 옮겨 담아서 session에 담음
우리가 만든 블로그에서는 session 정보 바로 담기가 가능함

2. UserRepository에서 UpdateDTO 만들기

package shop.mtcoding.blog.user; import lombok.Data; public class UserRequest { @Data public static class JoinDTO { private String username; private String password; private String email; } @Data public static class LoginDTO { private String username; private String password; } @Data public static class UpdateDTO { private String password; } }
 

3. UserController에서 updateForm 주소 세팅하기

  • 인증 체크하기
package shop.mtcoding.blog.user; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; import lombok.AllArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardRequest; @AllArgsConstructor @Controller public class UserController { // fianl 변수는 반드시 초기화 되어야 함 private final UserRepository userRepository; // null private final HttpSession session; // @AllArgsConstructor를 사용하면서 필요 없어짐 // public UserController(UserRepository userRepository, HttpSession session) { // this.userRepository = userRepository; // this.session = session; // } // 원래는 get요청이나 예외 post요청하면 됨 // 민감한 정보는 쿼리 스트링에 담아보낼 수 없음 //원래는 get요청이나 예외 post요청하면 됨 //민감한 정보는 쿼리 스트링에 담아보낼 수 없음 @PostMapping("/login") public String login(UserRequest.LoginDTO requestDTO) { // 1. 유효성 검사 if (requestDTO.getUsername().length() < 3) { return "error/400"; } // 2. 모델 필요 select * from user_tb where username=? and password=? User user = userRepository.findByUsernameAndPassword(requestDTO); // DB에 조회할때 필요하니까 데이터를 받음 if (user == null) { return "error/401"; } else { session.setAttribute("sessionUser", user); return "redirect:/"; } } @PostMapping("/join") public String join(UserRequest.JoinDTO requestDTO) { System.out.println(requestDTO); // 1. 유효성 검사 if (requestDTO.getUsername().length() < 3) { return "error/400"; } userRepository.save(requestDTO); // 모델에 위임하기 return "redirect:/loginForm"; //리다이렉션불러놓은게 있어서 다시부른거 } @GetMapping("/joinForm") // view만 원함 public String joinForm() { return "user/joinForm"; } @GetMapping("/loginForm") // view만 원함 public String loginForm() { return "user/loginForm"; } @GetMapping("/user/updateForm") public String updateForm() { // 인증 체크하기 User sessionUser = (User) session.getAttribute("sessionUser"); if (sessionUser == null) { return "redirect:/loginForm"; } userRepository.findByIdAndEmail(sessionUser.getId()); return "/user/updateForm"; } @GetMapping("/logout") public String logout() { // 1번 서랍에 있는 uset를 삭제해야 로그아웃이 됨 session.invalidate(); // 서랍의 내용 삭제 return "redirect:/"; } }

3. DB 조회해서 ID, EMAIL 출력하기

  • UserRepository에서 findByIdAndEmail() 구현하기
package shop.mtcoding.blog.user; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; import jdk.swing.interop.SwingInterOpUtils; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardRequest; @Repository // IoC에 new하는 방법 public class UserRepository { // DB에 접근할 수 있는 매니저 객체 // 스프링이 만들어서 IoC에 넣어둔다. // DI에서 꺼내 쓰기만 하면된다. private EntityManager em; // 컴포지션 // 생성자 주입 (DI 코드) public UserRepository(EntityManager em) { this.em = em; } @Transactional // db에 write 할때는 필수 public void save(UserRequest.JoinDTO requestDTO){ Query query = em.createNativeQuery("insert into user_tb(username, password, email, created_at) values(?,?,?, now())"); query.setParameter(1, requestDTO.getUsername()); query.setParameter(2, requestDTO.getPassword()); query.setParameter(3, requestDTO.getEmail()); query.executeUpdate(); } public User findByUsernameAndPassword(UserRequest.LoginDTO requestDTO) { Query query = em.createNativeQuery("select * from user_tb where username=? and password=?", User.class); // 알아서 매핑해줌 query.setParameter(1, requestDTO.getUsername()); query.setParameter(2, requestDTO.getPassword()); try { // 내부적으로 터지면 터지는 위치를 찾아서 내가 잡으면 됨 User user = (User) query.getSingleResult(); return user; } catch (Exception e) { return null; } } public User findByIdAndEmail(int id) { try { Query query = em.createNativeQuery("select username, email from user_tb where id=?"); query.setParameter(1, id); User user = (User) query.getSingleResult(); return user; } catch (Exception e) { return null; } } }
  • updateForm.mustache에서 ID, EMAIL의 placeholder 부분에 {{}}를 이용해서 자바 코드의 변수 출력하기
{{> /layout/header}} <div class="container p-5"> <!-- 요청을 하면 localhost:8080/join POST로 요청됨 username=사용자입력값&password=사용자값&email=사용자입력값 --> <div class="card"> <div class="card-header"><b>회원수정을 해주세요</b></div> <div class="card-body"> <form action="/user/update" method="post" enctype="application/x-www-form-urlencoded"> <div class="mb-3"> <input type="text" class="form-control" placeholder={{sessionUser.username}} name=username disabled> </div> <div class="mb-3"> <input type="password" class="form-control" placeholder="Enter password" name=password> </div> <div class="mb-3"> <input type="email" class="form-control" placeholder={{sessionUser.email}} name=email disabled> </div> <button type="submit" class="btn btn-primary form-control">회원가입수정</button> </form> </div> </div> </div> {{> /layout/footer}}
notion image
notion image
 

4. update 주소 만들기

  • 인증하기
  • UserRepository 에서 비밀번호 업데이트를 위한 userUpdate() 구현하기
package shop.mtcoding.blog.user; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; import jdk.swing.interop.SwingInterOpUtils; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardRequest; @Repository // IoC에 new하는 방법 public class UserRepository { // DB에 접근할 수 있는 매니저 객체 // 스프링이 만들어서 IoC에 넣어둔다. // DI에서 꺼내 쓰기만 하면된다. private EntityManager em; // 컴포지션 // 생성자 주입 (DI 코드) public UserRepository(EntityManager em) { this.em = em; } @Transactional // db에 write 할때는 필수 public void save(UserRequest.JoinDTO requestDTO){ Query query = em.createNativeQuery("insert into user_tb(username, password, email, created_at) values(?,?,?, now())"); query.setParameter(1, requestDTO.getUsername()); query.setParameter(2, requestDTO.getPassword()); query.setParameter(3, requestDTO.getEmail()); query.executeUpdate(); } public User findByUsernameAndPassword(UserRequest.LoginDTO requestDTO) { Query query = em.createNativeQuery("select * from user_tb where username=? and password=?", User.class); // 알아서 매핑해줌 query.setParameter(1, requestDTO.getUsername()); query.setParameter(2, requestDTO.getPassword()); try { // 내부적으로 터지면 터지는 위치를 찾아서 내가 잡으면 됨 User user = (User) query.getSingleResult(); return user; } catch (Exception e) { return null; } } public User findByIdAndEmail(int id) { try { Query query = em.createNativeQuery("select username, email from user_tb where id=?"); query.setParameter(1, id); User user = (User) query.getSingleResult(); return user; } catch (Exception e) { return null; } } @Transactional public void userUpdate(UserRequest.UpdateDTO requestDTO, int id){ Query query = em.createNativeQuery("update user_tb set password=? where id = ?"); query.setParameter(1,requestDTO.getPassword() ); query.setParameter(2, id); query.executeUpdate(); System.out.println("query:" + query); } }

5. 세션 동기화 시키기

package shop.mtcoding.blog.user; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; import lombok.AllArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardRequest; @AllArgsConstructor @Controller public class UserController { // fianl 변수는 반드시 초기화 되어야 함 private final UserRepository userRepository; // null private final HttpSession session; // @AllArgsConstructor를 사용하면서 필요 없어짐 // public UserController(UserRepository userRepository, HttpSession session) { // this.userRepository = userRepository; // this.session = session; // } // 원래는 get요청이나 예외 post요청하면 됨 // 민감한 정보는 쿼리 스트링에 담아보낼 수 없음 //원래는 get요청이나 예외 post요청하면 됨 //민감한 정보는 쿼리 스트링에 담아보낼 수 없음 @PostMapping("/login") public String login(UserRequest.LoginDTO requestDTO) { // 1. 유효성 검사 if (requestDTO.getUsername().length() < 3) { return "error/400"; } // 2. 모델 필요 select * from user_tb where username=? and password=? User user = userRepository.findByUsernameAndPassword(requestDTO); // DB에 조회할때 필요하니까 데이터를 받음 if (user == null) { return "error/401"; } else { session.setAttribute("sessionUser", user); return "redirect:/"; } } @PostMapping("/join") public String join(UserRequest.JoinDTO requestDTO) { System.out.println(requestDTO); // 1. 유효성 검사 if (requestDTO.getUsername().length() < 3) { return "error/400"; } userRepository.save(requestDTO); // 모델에 위임하기 return "redirect:/loginForm"; //리다이렉션불러놓은게 있어서 다시부른거 } @GetMapping("/joinForm") // view만 원함 public String joinForm() { return "user/joinForm"; } @GetMapping("/loginForm") // view만 원함 public String loginForm() { return "user/loginForm"; } @GetMapping("/user/updateForm") public String updateForm() { // 인증 체크하기 User sessionUser = (User) session.getAttribute("sessionUser"); if (sessionUser == null) { return "redirect:/loginForm"; } userRepository.findByIdAndEmail(sessionUser.getId()); return "/user/updateForm"; } @PostMapping("/user/update") public String updateUser(UserRequest.UpdateDTO requestDTO, HttpServletRequest request) { // 세션에서 사용자 정보 가져오기 User sessionUser = (User) session.getAttribute("sessionUser"); if (sessionUser == null) { return "redirect:/loginForm"; // 로그인 페이지로 리다이렉트 } // 비밀번호 업데이트 userRepository.userUpdate(requestDTO, sessionUser.getId()); session.setAttribute("sessionUser", sessionUser); // 세션 동기화 : 덮어씌움 return "redirect:/"; // 홈 페이지로 리다이렉트 } @GetMapping("/logout") public String logout() { // 1번 서랍에 있는 uset를 삭제해야 로그아웃이 됨 session.invalidate(); // 서랍의 내용 삭제 return "redirect:/"; } }
 

비밀번호 변경 확인하기

  • 비밀번호 변경하기
notion image
  • 기존 비밀번호로 확인하기
notion image
notion image
  • 변경된 비밀번호로 확인하기
notion image
Share article

Coding_study