νμκ°μ
ν΄λΌμ΄μΈνΈ
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.
4.
Utils
a.
PasswordUtils.java
5.
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.
UserServiceImpl.java μμ±
UserService.java μμ±
package shop.Service;
import shop.DTO.Users;
public interface UserService {
// νμκ°μ
public int signup(Users user);
}
SQL
볡μ¬
UserServiceImpl.java μμ±
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
볡μ¬