package com.study.common.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
public class CookieUtils {
private Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
public CookieUtils(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
cookieMap.put(cookie.getName(), cookie);
}
}
}
public boolean exist(String name) {
Cookie cookie = cookieMap.get(name);
return cookie != null;
}
public String getValue(String name) {
return cookieMap.get(name).getValue();
}
public Cookie getCookie(String name) {
return cookieMap.get(name);
}
public static Cookie createCookie(String name, String value) {
return createCookie(name, value, "/", "", -1);
}
public static Cookie createCookie(String name, String value, String path) {
return createCookie(name, value, path, "", -1);
}
public static Cookie createCookie(String name, String value, String path, int maxAge) {
return createCookie(name, value, path, "", maxAge);
}
public static Cookie createCookie(String name, String value, int maxAge) {
return createCookie(name, value, "/", "", maxAge);
}
public static Cookie createCookie(String name, String value, String path, String domain, int maxAge) {
Cookie cookie = new Cookie(name, value);
cookie.setDomain(domain);
cookie.setPath(path);
cookie.setMaxAge(maxAge);
return cookie;
}
}
package com.study.login.vo;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class UserVO {
private String userId;
private String userName;
private String userPass;
private String userRole;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
public UserVO() {
}
public UserVO(String userId, String userName, String userPass, String userRole) {
this.userId = userId;
this.userName = userName;
this.userPass = userPass;
this.userRole = userRole;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
}
package com.study.common.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.study.login.vo.UserVO;
public class UserList {
private Map<String, UserVO> usersMap = null;
public UserList() {
UserVO user = null;
usersMap = new HashMap<String, UserVO>();
user = new UserVO("malja", "๋ง์", "1004", "ADMIN");
usersMap.put(user.getUserId(), user);
user = new UserVO("sunja", "์์", "1111", "USER");
usersMap.put(user.getUserId(), user);
user = new UserVO("nolja", "์ผ๋์", "1004", "USER");
usersMap.put(user.getUserId(), user);
user = new UserVO("milkis", "๋ฐํค์ค", "1004", "MANAGER");
usersMap.put(user.getUserId(), user);
user = new UserVO("areum", "์๋ฆ", "0000", "MANAGER");
usersMap.put(user.getUserId(), user);
}
public UserVO getUser(String id) {
System.out.println("UserList getUser id=" + id);
if (usersMap.containsKey(id)) {
System.out.println("[" + id + "] ํ์ ์กด์ฌ");
return usersMap.get(id);
} else {
System.out.println("[" + id + "] ํ์์ด ์กด์ฌํ์ง ์์");
return null;
}
}
public List<UserVO> getUserList() {
return new ArrayList<UserVO>(usersMap.values());
}
public Map<String, UserVO> getUsersMap() {
return usersMap;
}
}
<%@page import="com.study.common.util.CookieUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/inc/header.jsp"%>
<title>Insert title here</title>
</head>
<body>
<!--
์กฐ๊ฑด ์ข
๋ฅ
msg๋ผ๋ ํ๋ผ๋ฏธํฐ, AUTH์ฟ ํค ์กด์ฌ์ฌ๋ถ
-->
<%@include file="/WEB-INF/inc/top.jsp"%>
<%
String msg = request.getParameter("msg");
CookieUtils cookieUtils = new CookieUtils(request);
Cookie cookie = cookieUtils.getCookie("AUTH");
Cookie saveCookie = cookieUtils.getCookie("SAVE_ID");
String checked="";
String id="";
if(saveCookie != null){
checked="checked='checked'";
id=saveCookie.getValue();
}
if(cookie == null){
%>
<%=msg %>
<div class="container">
<form action="loginCheck.jsp" class="loginForm">
<h2>๋ก๊ทธ์ธ</h2>
<table class="table table-bordered">
<tbody>
<tr>
<th>์์ด๋</th>
<td><input type="text" name="userId"
class="form-control input-sm" value="<%=id%>"></td>
</tr>
<tr>
<th>๋น๋ฐ๋ฒํธ</th>
<td><input type="password" name="userPass"
class="form-control input-sm"></td>
</tr>
<tr>
<td colspan="2"><label><input type="checkbox"
name="rememberMe" value="Y" <%=checked %> >ID ๊ธฐ์ตํ๊ธฐ</label></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary btn-sm pull-right">๋ก๊ทธ์ธ</button>
</td>
</tr>
</tbody>
</table>
</form>
</div>
<%
}else{
%>
<!-- container -->
๋ก๊ทธ์ธ ์ค
<a href="logout.jsp" class="btn btn-success btn-sm">๋ก๊ทธ์์</a>
<%
}
%>
</body>
</html>
<%@page import="java.util.Map"%>
<%@page import="com.study.common.util.CookieUtils"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.study.login.vo.UserVO"%>
<%@page import="com.study.common.util.UserList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/inc/header.jsp" %>
<title></title>
</head>
<body>
<%
String id = request.getParameter("userId");
String pw = request.getParameter("userPass");
String rememberMe = request.getParameter("rememberMe");
String redirectPage = "";
if(id == null || id.isEmpty() || pw == null || pw.isEmpty()){
redirectPage = "login.jsp?msg=" + URLEncoder.encode("id ๋๋ pw๊ฐ ์์ต๋๋ค.", "UTF-8");
}
UserList userList = new UserList();
UserVO user = userList.getUser(id);
if(user==null){
if(redirectPage.length()<2){
redirectPage = "login.jsp?msg=" + URLEncoder.encode("id๋๋ pw ํ์ธํด์ฃผ์ธ์.", "UTF-8");
}
}else{
if(pw.equals(user.getUserPass())){
Cookie cookie = CookieUtils.createCookie("AUTH", id);
response.addCookie(cookie);
redirectPage="login.jsp";
if(rememberMe != null){
if(rememberMe.equals("Y")){
Cookie cookie2 = CookieUtils.createCookie("SAVE_ID", id, 60*60*24*7);
response.addCookie(cookie2);
}else{
Cookie cookie2 = CookieUtils.createCookie("SAVE_ID", id, 0);
response.addCookie(cookie2);
}
}
}else{
if(redirectPage.length()<2){
redirectPage = "login.jsp?msg=" + URLEncoder.encode("id๋๋ pw ํ์ธํด์ฃผ์ธ์.", "UTF-8");
}
}
}
response.sendRedirect(redirectPage);
%>
</body>
</html>
<%@page import="com.study.common.util.CookieUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/inc/header.jsp" %>
<title>Insert title here</title>
</head>
<body>
<%@include file="/WEB-INF/inc/top.jsp" %>
<%
Cookie cookie = CookieUtils.createCookie("AUTH", "", 0);
response.addCookie(cookie);
response.sendRedirect("login.jsp");
%>
</body>
</html>