packagecom.mmall.util;
importlombok.extern.slf4j.Slf4j;
importorg.apache.commons.lang.StringUtils;
importjavax.servlet.http.Cookie;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@Slf4jpublicclassCookieUtil {
privatefinalstaticStringCOOKIE_DOMAIN=".happymmall.com";
privatefinalstaticStringCOOKIE_NAME="mmall_login_token";
publicstaticStringreadLoginToken(HttpServletRequestrequest){
Cookie[] cks=request.getCookies();
if(cks!=null){
for(Cookieck : cks){
log.info("read cookieName:{},cookieValue:{}",ck.getName(),ck.getValue());
if(StringUtils.equals(ck.getName(),COOKIE_NAME)){
log.info("return cookieName:{},cookieValue:{}",ck.getName(),ck.getValue());
returnck.getValue();
}
}
}
returnnull;
}
publicstaticvoidwriteLoginToken(HttpServletResponseresponse,Stringtoken){
Cookieck=newCookie(COOKIE_NAME,token);
ck.setDomain(COOKIE_DOMAIN);
ck.setPath("/");ck.setHttpOnly(true);
ck.setMaxAge(60*60*24*365);
log.info("write cookieName:{},cookieValue:{}",ck.getName(),ck.getValue());
response.addCookie(ck);
}
publicstaticvoiddelLoginToken(HttpServletRequestrequest,HttpServletResponseresponse){
Cookie[] cks=request.getCookies();
if(cks!=null){
for(Cookieck : cks){
if(StringUtils.equals(ck.getName(),COOKIE_NAME)){
ck.setDomain(COOKIE_DOMAIN);
ck.setPath("/");
ck.setMaxAge(0); log.info("del cookieName:{},cookieValue:{}",ck.getName(),ck.getValue());
response.addCookie(ck);
return;
}
}
}
}
}