CookieUtils.java์ ์ฝ๋๋ด์ฉ
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 {
//์์ฑ์, exist, getValue, getCookie, createCookie
private Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
//request์ ์๋ ๋ชจ๋ ์ฟ ํค๋ค์ ๋งต์๋ค ์ ์ฅ
public CookieUtils(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
cookieMap.put(cookie.getName(), cookie);
}
}
}
//name์ ํด๋นํ๋ ์ฟ ํค๊ฐ ์๋ ์๋? ์์ผ๋ฉด true
public boolean exist(String name) {
Cookie cookie = cookieMap.get(name);
return cookie != null;
}
//name์ด๋ผ๋ ์ด๋ฆ์ ์ฟ ํค์ ๊ฐ
//์ฟ ํค์ ํธ ์ฌ์ฉํ๋ ๊ณณ์์ null์ฒดํฌ ์์์ ํ๋๋ก ํ์
public String getValue(String name) {
return cookieMap.get(name).getValue();
}
//name์ ์ฟ ํค
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) {
// Cookie cookie = new Cookie(name, value);
// cookie.setMaxAge(maxAge);
// return cookie;
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;
}
}
UserVO.java์ ์ฝ๋๋ด์ฉ
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;
// toString() ๊ตฌํ
@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;
}
// ๋งด๋ฒํ๋์ get/set ๋ฉ์๋ ์์ฑ
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;
}
}
UserList.java์ ์ฝ๋๋ด์ฉ
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;
}
} // class
login.jsp์ ์ฝ๋๋ด์ฉ
<%@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>
loginCheck.jsp์ ์ฝ๋๋ด์ฉ
<%@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"); //login์์ ๋ฐ์์จ ์
๋ ฅ๊ฐ
String pw = request.getParameter("userPass"); //login์์ ๋ฐ์์จ ์
๋ ฅ๊ฐ
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");
}
//id๋ฅผ ๋ชป ์ฐพ์ ๋
UserList userList = new UserList();
UserVO user = userList.getUser(id); //์์ผ๋ฉด null ์์ผ๋ฉด ํด๋น userVO๋ฅผ ๋ฆฌํด
if(user==null){ //user๊ฐ ์์ ๋, id๊ฐ ํ๋ ธ์ ๋
if(redirectPage.length()<2){
redirectPage = "login.jsp?msg=" + URLEncoder.encode("id๋๋ pw ํ์ธํด์ฃผ์ธ์.", "UTF-8");
}
}else{ //id๋ ๋ง์๋ค.
if(pw.equals(user.getUserPass())){
//pw๊น์ง ๋ง์
Cookie cookie = CookieUtils.createCookie("AUTH", id);
response.addCookie(cookie);
redirectPage="login.jsp";
//๋ก๊ทธ์ธ ์ฑ๊ณต + id๊ธฐ์ตํ๊ธฐ ์ฒดํฌ ๋์ด์๋ ๊ฒฝ์ฐ
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{//id๋ ๋ง์์ง๋ง pw๋ ํ๋ฆผ
if(redirectPage.length()<2){
redirectPage = "login.jsp?msg=" + URLEncoder.encode("id๋๋ pw ํ์ธํด์ฃผ์ธ์.", "UTF-8");
}
}
}
response.sendRedirect(redirectPage);
%>
</body>
</html>
logout.jsp์ ์ฝ๋๋ด์ฉ
<%@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>