๋ก๊ทธ์ธ
ํด๋ผ์ด์ธํธ
โข
๋ก๊ทธ์ธ ํ๋ฉด - login.jsp
โข
๋ก๊ทธ์ธ ์์ฒญ
โฆ
์์ฒญ ๊ฒฝ๋ก : login_pro.jsp
โฆ
์์ฒญ ๋ฉ์๋ : post
โฆ
ํ๋ผ๋ฏธํฐ
โช
username
โช
password
โข
๋ก๊ทธ์ธ ์คํจ
โฆ
โ์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.โ
์๋ฒ
โข
๋ก๊ทธ์ธ ์์ฒญ ์ฒ๋ฆฌ
โข
UserService
โฆ
login( user )
โข
UserServiceImpl
โฆ
๋ก๊ทธ์ธ ์ฒ๋ฆฌ ๋ก์ง - login( user )
โช
์์ด๋๋ก ํ์ ์กฐํ
โช
๋น๋ฐ๋ฒํธ ์ผ์น ์ฌ๋ถ ํ์ธ
โช
์์ด๋&๋น๋ฐ๋ฒํธ ์ผ์น
๋ก๊ทธ์ธ ์ฑ๊ณต
โช
์์ด๋&๋น๋ฐ๋ฒํธ ๋ถ์ผ์น
๋ก๊ทธ์ธ ์คํจ
โข
UserDAO
โฆ
๋ฒํธ๋ก ํ์ ์กฐํ - select( no )
โฆ
์์ด๋๋ก ํ์ ์กฐํ - select( username )
โข
์๋ต
โฆ
๋ก๊ทธ์ธ ์ฑ๊ณต
โช
์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด ๋ฑ๋ก
โช
๋ฆฌ๋ค์ด๋ ํธ : ๋ฉ์ธ ํ๋ฉด
โข
๊ฒฝ๋ก : /
โฆ
๋ก๊ทธ์ธ ์คํจ
โช
๋ฆฌ๋ค์ด๋ ํธ : ๋ก๊ทธ์ธ
โข
๊ฒฝ๋ก : /login?error=0
์์ ์์
1.
DAO
a.
UserDAO.java (DAO)
i.
๋ฒํธ๋ก ํ์ ์กฐํ - select( no )
ii.
์์ด๋๋ก ํ์ ์กฐํ - select( username )
2.
3.
JSP
a.
๋ก๊ทธ์ธ ์์ฒญ ํ์ด์ง
i.
login.jsp
b.
๋ก๊ทธ์ธ ์์ฒญ ์ฒ๋ฆฌ
i.
login_pro.jsp
DAO
UserDAO.java (DAO)
1.
๋ฒํธ๋ก ํ์ ์กฐํ - select( no )
2.
์์ด๋๋ก ํ์ ์กฐํ - select( username )
/**
* ํ์ ๋ฒํธ๋ก ์กฐํ
* @param no
* @return
*/
public Users select(int no) {
String sql = " SELECT * "
+ " FROM users"
+ " WHERE no = ? ";
Users user = null;
try {
psmt = con.prepareStatement(sql);
psmt.setInt(1, no);
rs = psmt.executeQuery();
if( rs.next() ) {
user = new Users();
user.setNo( rs.getInt("no") );
user.setUsername( rs.getString("username") );
user.setPassword( rs.getString("password") );
user.setName( rs.getString("name") );
user.setEmail( rs.getString("email") );
user.setEnabled( rs.getBoolean("enabled") );
user.setRegDate( rs.getTimestamp("reg_date") );
user.setUpdDate( rs.getTimestamp("upd_date") );
}
} catch (Exception e) {
System.err.println("ํ์ ์ ๋ณด ๋ฒํธ๋ก ์กฐํ ์ ์์ธ ๋ฐ์");
e.printStackTrace();
}
return user;
}
/**
* ํ์ ์์ด๋๋ก ์กฐํ
* @param username
* @return
*/
public Users select(String username) {
String sql = " SELECT * "
+ " FROM users"
+ " WHERE username = ? ";
Users user = null;
try {
psmt = con.prepareStatement(sql);
psmt.setString(1, username);
rs = psmt.executeQuery();
if( rs.next() ) {
user = new Users();
user.setNo( rs.getInt("no") );
user.setUsername( rs.getString("username") );
user.setPassword( rs.getString("password") );
user.setName( rs.getString("name") );
user.setEmail( rs.getString("email") );
user.setEnabled( rs.getBoolean("enabled") );
user.setRegDate( rs.getTimestamp("reg_date") );
user.setUpdDate( rs.getTimestamp("upd_date") );
}
} catch (Exception e) {
System.err.println("ํ์ ์ ๋ณด ์์ด๋๋ก ์กฐํ ์ ์์ธ ๋ฐ์");
e.printStackTrace();
}
return user;
}
Java
๋ณต์ฌ
Service
1.
login( user )
package shop.Service;
import shop.DTO.Users;
public interface UserService {
// ํ์๊ฐ์
public int signup(Users user);
// ๋ก๊ทธ์ธ
public Users login(Users user);
}
Java
๋ณต์ฌ
1.
login ( user )
@Override
public Users login(Users user) {
String username = user.getUsername();
Users selectedUser = userDAO.select(username);
// ํ์ ๊ฐ์
์ด ์ ๋ ์์ด๋
if( selectedUser == null )
return null;
// ๋น๋ฐ๋ฒํธ ์ผ์น ์ฌ๋ถ ํ์ธ
String loginPassword = user.getPassword();
String password = selectedUser.getPassword();
// * BCrypt.checkpw(๋ก๊ทธ์ธ ๋น๋ฐ๋ฒํธ, ์ํธํธ๋ ๋น๋ฐ๋ฒํธ);
boolean check = PasswordUtils.check(loginPassword, password);
// ๋น๋ฐ๋ฒํธ ๋ถ์ผ์น
if( !check )
return null;
// ๋ก๊ทธ์ธ ์ฑ๊ณต
return selectedUser;
}
Java
๋ณต์ฌ
JSP
๋ก๊ทธ์ธ ์์ฒญ ํ์ด์ง
1.
login.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>
<form action="login_pro.jsp" method="post">
<p>์์ด๋ : <input type="text" name="username" id="username" /> </p>
<p>๋น๋ฐ๋ฒํธ : <input type="password" name="password" id="password" /> </p>
<p>
<input type="submit" value="๋ก๊ทธ์ธ" />
</p>
</form>
</body>
</html>
HTML
๋ณต์ฌ
๋ก๊ทธ์ธ ์์ฒญ ์ฒ๋ฆฌ
1.
login_pro.jsp
<%@page import="shop.Service.UserServiceImpl"%>
<%@page import="shop.Service.UserService"%>
<%@page import="shop.DTO.Users"%>
<%@ 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"%>
<%
// ์์ด๋ ๋น๋ฐ๋ฒํธ ๊ฐ์ ธ์ค๊ธฐ
String username = request.getParameter("username");
String password = request.getParameter("password");
// ๋ก๊ทธ์ธ ์์ฒญ
Users user = Users.builder()
.username(username)
.password(password)
.build();
UserService userService = new UserServiceImpl();
Users loginUser = userService.login(user);
// ๋ก๊ทธ์ธ ์คํจ
if( loginUser == null ) {
// ๋ฆฌ๋ค์ด๋ ํธ -> ๋ก๊ทธ์ธ ํ๋ฉด์ผ๋ก ๋ค์ ์ด๋
response.sendRedirect("login.jsp?error=0");
return;
}
// ๋ก๊ทธ์ธ ์ฑ๊ณต
if( loginUser != null ) {
// ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด ๋ฑ๋ก
session.setAttribute("loginId", loginUser.getUsername());
session.setAttribute("loginUser", loginUser);
// ๋ฆฌ๋ค์ด๋ ํธ -> ๋ฉ์ธ ํ๋ฉด
response.sendRedirect("index.jsp");
}
%>
HTML
๋ณต์ฌ