fairnet/src/main/java/seed/utils/SeedCookieUtil.java

64 lines
1.8 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package seed.utils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SeedCookieUtil {
/**
* 쿠키를 생성하는 메소드
* @param HttpServletResponse response response객체
* @param Integer maxAge 시간설정 60*60*24*365 1년œ
* @param String cookiePath 쿠키가 저장되는 경로 기본 / 입력
* @param String key 쿠키 key값
* @param Stirng value 쿠키 value
* */
public void setCookie(HttpServletResponse response, Integer maxAge, String cookiePath, String key, String value, boolean httpOnly){
if(httpOnly){
response.setHeader("Set-Cookie", key+"="+value+"; path=/; HttpOnly");
}
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(maxAge);
cookie.setSecure(true);
cookie.setPath(cookiePath);
response.addCookie(cookie);
}
/**
* key에 해당하는 쿠키 정보를 가지고 오는 메소드
* @param HttpServletRequest request 객체
* @param String key 쿠키 key값
* @param String 쿠키 정보
* */
public String getCookie(HttpServletRequest request, String key){
Cookie[] cookies = request.getCookies();
String cookieValue = "";
if(cookies!=null){
int iLoop = cookies.length;
for (int i=0; i<iLoop; i++){
if(key.equals(cookies[i].getName())){
cookieValue = cookies[i].getValue();
break;
}
}
}
return cookieValue;
}
/**
* 쿠키 정보를 삭제 하는 메소드
* @param HttpServletResponse response 객체
* @param String cookiePath 쿠키경로 설정
* @param String key 삭제할 key값
* */
public void deleteCookie(HttpServletResponse response, String cookiePath, String key){
Cookie cookie = new Cookie(key, "");
cookie.setMaxAge(0);
cookie.setSecure(true);
cookie.setPath(cookiePath);
response.addCookie(cookie);
}
}