Search

๋กœ๊ทธ์ธ

๋กœ๊ทธ์ธ

ํด๋ผ์ด์–ธํŠธ
โ€ข
๋กœ๊ทธ์ธ ํ™”๋ฉด - 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.
i.
๋ฒˆํ˜ธ๋กœ ํšŒ์› ์กฐํšŒ - select( no )
ii.
์•„์ด๋””๋กœ ํšŒ์› ์กฐํšŒ - select( username )
2.
Service
a.
i.
login( user )
b.
i.
login ( user )
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
๋ณต์‚ฌ