Search

νšŒμ›κ°€μž…

νšŒμ›κ°€μž…

ν΄λΌμ΄μ–ΈνŠΈ
1.
νšŒμ› κ°€μž… 정보 μž…λ ₯
a.
form μš”μ²­
i.
method=”post”
ii.
μž…λ ₯ 정보
1.
아이디
2.
λΉ„λ°€λ²ˆν˜Έ
3.
λΉ„λ°€λ²ˆν˜Έ 확인
4.
이름
5.
이메일
2.
응닡 λ©”μ‹œμ§€μ— 따라 ν™”λ©΄ 이동
μ„œλ²„
1.
νšŒμ› κ°€μž… 정보 전달 λ°›μŒ
2.
User 객체 생성
3.
μ„œλΉ„μŠ€
4.
DAO
5.
DB
a.
insert
6.
응닡
a.
성곡 : 201 CREATED
i.
β€œSUCCESS”
ii.
λ¦¬λ‹€μ΄λ ‰νŠΈ : /login
b.
μ‹€νŒ¨
i.
β€œFAIL”
ii.
λ¦¬λ‹€μ΄λ ‰νŠΈ : /signup?error

μž‘μ—… μˆœμ„œ

1.
users ν…Œμ΄λΈ” 생성
a.
DDL
2.
DTO
a.
User.java (DTO) 생성
3.
DAO
a.
JDBConnection.java (DB μ—°κ²° 객체)
b.
UserDAO.java (DAO) 생성
4.
Utils
a.
PasswordUtils.java
5.
Service
a.
UserService.java 생성
b.
6.
JSP
a.
νšŒμ› κ°€μž… μš”μ²­ νŽ˜μ΄μ§€
i.
signup.jsp
b.
νšŒμ› κ°€μž… 처리 νŽ˜μ΄μ§€
i.
signup_pro.jsp

users ν…Œμ΄λΈ” 생성

DDL

DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `no` INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'νšŒμ›λ²ˆν˜Έ', `useranme` VARCHAR(100) NOT NULL COMMENT '아이디', `password` VARCHAR(100) NULL COMMENT 'λΉ„λ°€λ²ˆν˜Έ', `name` VARCHAR(20) NOT NULL COMMENT '이름', `email` VARCHAR(100) NULL COMMENT '이메일', `enabled` BOOLEAN NULL COMMENT 'νœ΄λ©΄μ—¬λΆ€', `reg_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'λ“±λ‘μΌμž', `upd_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'μˆ˜μ •μΌμž' );
SQL
볡사
β€’
AUTO_INCREMENT : κΈ°λ³Έν‚€(PRIMARY KEY)에 μ μš©ν•  수 있고, 값을 μ§€μ •ν•˜μ§€ μ•Šκ³  데이터λ₯Ό μΆ”κ°€ν•˜λ©΄ 일식 μžλ™μœΌλ‘œ μ¦κ°€ν•œλ‹€
β€’
PRIMARY KEY : κΈ°λ³Έν‚€λ‘œ 지정. 값이 κ³ μœ ν•΄μ•Όν•˜κ³ , NULL 일 수 μ—†λ‹€.
β€’
BOOLEAN : TINYINT(1) 둜 μ§€μ •λœλ‹€. true 이면 1, false 이면 0
β€’
DEFAULT CURRENT_TIMESTAMP : 기본값을 ν˜„μž¬ λ‚ μ§œ μ‹œκ°„ κ°’μœΌλ‘œ 지정

DTO

User.java (DTO) 생성

package shop.DTO; import java.util.Date; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @Builder public class Users { private int no; private String username; private String password; private String name; private String email; private Boolean enabled = true; private Date regDate; private Date updDate; }
Java
볡사
@Data : getter, setter, κΈ°λ³Έ μƒμ„±μž, toString μžλ™ 생성 @NoArgsConstructor : κΈ°λ³Έ μƒμ„±μž @AllArgsConstructor : λͺ¨λ“  λ§€κ°œλ³€μˆ˜κ°€ μžˆλŠ” μƒμ„±μž @Builder : λΉŒλ” νŒ¨ν„΄
Users user = Users.builder() .username(username) .password(password) .name(name) .email(email) .enabled(true) .build();
SQL
볡사

DAO

1.
JDBConnection.java (DB μ—°κ²° 객체)
2.
UserDAO.java (DAO) 생성

JDBConnection.java (DB μ—°κ²° 객체)

package shop.DAO; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class JDBConnection { public Connection con; // μ—°κ²°λœ λ“œλΌμ΄λ²„μ— SQL을 μš”μ²­ν•  객체λ₯Ό μƒμ„±ν•˜λŠ” 클래슀 public Statement stmt; // SQL μ‹€ν–‰ μš”μ²­μ„ ν•˜λŠ” 클래슀 public PreparedStatement psmt; // Statement μ—μ„œ ? νŒŒλΌλ―Έν„° ν™•μž₯κΈ°λŠ₯을 μΆ”κ°€λ‘œ μ œκ³΅ν•˜λŠ” 클래슀 public ResultSet rs; // SQL μ‹€ν–‰ κ²°κ³Όλ₯Ό λ°›μ•„μ˜€λŠ” 클래슀 // κΈ°λ³Έ μƒμ„±μž public JDBConnection() { // JDBC λ“œλΌμ΄λ²„ λ‘œλ“œ // MySQL try { // mysql-connector-j.xxx.jar λ“œλΌμ΄λ²„μ˜ 클래슀λ₯Ό λ‘œλ“œν•œλ‹€. Class.forName("com.mysql.cj.jdbc.Driver"); // DB에 μ—°κ²° // - 연결에 ν•„μš”ν•œ 정보 : URL, id, pw // URL : jdbc:mysql://도메인:[PORT]/[μŠ€ν‚€λ§ˆ]?μ˜΅μ…˜νŒŒλΌλ―Έν„° // * λ‚΄ PC의 IPμ£Όμ†Œ : localhost : 127.0.0.1 // * 3306 : MySQL λ°μ΄ν„°λ² μ΄μŠ€μ˜ κΈ°λ³Έ 포트 // * ~/shop : μŠ€ν‚€λ§ˆ String url = "jdbc:mysql://127.0.0.1:3306/shop?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false"; String id = "aloha"; String pw = "123456"; // μžλ°” ν”„λ‘œκ·Έλž¨μ—μ„œ JDBC λ“œλΌμ΄λ²„λ₯Ό μ—°κ²°μ‹œμΌœμ£ΌλŠ” 클래슀 // getConnection() λ©”μ†Œλ“œλ‘œ DB에 μ—°κ²° μš”μ²­ν•˜κ³  μƒμ„±λœ Connection 객체λ₯Ό λ°˜ν™˜λ°›λŠ”λ‹€. con = DriverManager.getConnection(url, id, pw); System.out.println("DB μ—°κ²° 성곡"); } catch (Exception e) { System.err.println("DB μ—°κ²° μ‹€νŒ¨"); e.printStackTrace(); } } }
Java
볡사

UserDAO.java (DAO) 생성

package shop.DAO; import shop.DTO.Users; public class UserDAO extends JDBConnection { /** * νšŒμ› κ°€μž… * @param user * @return */ public int signup(Users user) { int result = 0; String sql = " INSERT INTO users(username, password, name, email, enabled )" + " VALUES ( ?, ?, ?, ?, ? ) "; try { psmt = con.prepareStatement(sql); psmt.setString(1, user.getUsername()); psmt.setString(2, user.getPassword()); psmt.setString(3, user.getName()); psmt.setString(4, user.getEmail()); psmt.setBoolean(5, user.getEnabled()); result = psmt.executeUpdate(); } catch (Exception e) { System.err.println("νšŒμ› 등둝 μ‹œ, μ˜ˆμ™Έ λ°œμƒ"); e.printStackTrace(); } return result; } }
Java
볡사

Utils

Bcrypt μ•”ν˜Έν™” μ•Œκ³ λ¦¬μ¦˜ 라이브러리

PasswordUtils.java

package shop.utils; import org.mindrot.jbcrypt.BCrypt; /** * μ•”ν˜Έν™” μœ ν‹Έ */ public class PasswordUtils { // Bcrypt μ•”ν˜Έν™” μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ μ•”ν˜Έν™” public static String encoded(String password) { // SALT : μ•”ν˜Έν™” κ²°κ³Όλ₯Ό λ‹¬λΌμ§€κ²Œ ν•˜λŠ” μž„μ˜μ˜ 데이터 return BCrypt.hashpw(password, BCrypt.gensalt()); } // λΉ„λ°€λ²ˆν˜Έ 검증 public static boolean check(String password, String encodedPassword) { return BCrypt.checkpw(password, encodedPassword); } }
Java
볡사

Service

1.
UserService.java 생성
2.

UserService.java 생성

package shop.Service; import shop.DTO.Users; public interface UserService { // νšŒμ›κ°€μž… public int signup(Users user); }
SQL
볡사
package shop.Service; import shop.DAO.UserDAO; import shop.DTO.Users; import shop.utils.PasswordUtils; public class UserServiceImpl implements UserService { private UserDAO userDAO = new UserDAO(); @Override public int signup(Users user) { // λΉ„λ°€λ²ˆν˜Έ μ•”ν˜Έν™” // * μ•”ν˜Έν™” μ•Œκ³ λ¦¬μ¦˜ : SHA-256, Bcrypt ... // 123456 ---> FIJ3124890J12/@3J9 String encodedPassword = PasswordUtils.encoded(user.getPassword()); user.setPassword(encodedPassword); // νšŒμ› 정보 등둝 μš”μ²­ int result = userDAO.signup(user); if( result > 0 ) System.out.println("νšŒμ› κ°€μž… 성곡!"); else System.out.println("νšŒμ› κ°€μž… μ‹€νŒ¨!"); // νšŒμ› κΈ°λ³Έ κΆŒν•œ 등둝... return result; } }
SQL
볡사

JSP

1.
νšŒμ› κ°€μž… μš”μ²­ νŽ˜μ΄μ§€
a.
signup.jsp
2.
νšŒμ› κ°€μž… 처리 νŽ˜μ΄μ§€
a.
signup_pro.jsp

νšŒμ› κ°€μž… μš”μ²­ νŽ˜μ΄μ§€

signup.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>νšŒμ› κ°€μž…</title> </head> <body> <h1>νšŒμ› κ°€μž…</h1> <c:if test="${ param.error == 0 }"> <p style="color: red;">νšŒμ›κ°€μž…μ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.</p> </c:if> <form action="signup_pro.jsp" method="post"> <p> <label for="username">아이디</label> <input type="text" name="username" id="username" /> </p> <p> <label for="password">λΉ„λ°€λ²ˆν˜Έ</label> <input type="password" name="password" id="password" /> </p> <p> <label for="name">이름</label> <input type="text" name="name" id="name" /> </p> <p> <label for="email">이메일</label> <input type="text" name="email" id="email" /> </p> <div> <input type="submit" value="νšŒμ›κ°€μž…" /> </div> </form> </body> </html>
HTML
볡사

νšŒμ› κ°€μž… 처리 νŽ˜μ΄μ§€

signup_pro.jsp

<%@page import="shop.Service.UserServiceImpl"%> <%@page import="shop.Service.UserService"%> <%@page import="shop.DTO.Users"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = request.getParameter("username"); String password = request.getParameter("password"); String name = request.getParameter("name"); String email = request.getParameter("email"); // User 객체 생성 Users user = Users.builder() .username(username) .password(password) .name(name) .email(email) .enabled(true) .build(); // νšŒμ› κ°€μž… μš”μ²­ UserService userService = new UserServiceImpl(); int result = userService.signup(user); // νšŒμ›κ°€μž… 성곡 if( result > 0 ) { response.sendRedirect("index.jsp"); // λ©”μΈν™”λ©΄μœΌλ‘œ 이동 } // νšŒμ›κ°€μž… μ‹€νŒ¨ else { response.sendRedirect("signup.jsp?error=0"); // λ‹€μ‹œ νšŒμ›κ°€μž…νŽ˜μ΄μ§€λ‘œ (μ—λŸ¬ν¬ν•¨) } %>
Java
볡사