LoginInfoHelper cookie 设置超时时间


import com.google.common.base.Strings;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.util.Date;

public class LoginInfoHelper {
    public static Boolean checkLoginInfo(HttpServletRequest request, HttpServletResponse response) {
    String user = CookieHelper.getValue("user", request);
      String name = CookieHelper.getValue("name", request);
        String token = CookieHelper.getValue("token", request);
        String acttime = CookieHelper.getValue("acttime", request);
 if (Strings.isNullOrEmpty(user)
             || Strings.isNullOrEmpty(name)
                || Strings.isNullOrEmpty(token)
                || Strings.isNullOrEmpty(acttime)
                ) {  return false;

        } else try {
            Long t = Long.parseLong(acttime);
            if (t < new Date().getTime() - 30 * 60000) {
                return false;
            } else if (createToken(user, name, t).equalsIgnoreCase(token)) {
      updateLoginInfo(user, name, response);

                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    } public static void updateLoginInfo(String user, String name, HttpServletResponse response) {


        if (!Strings.isNullOrEmpty(user) && !Strings.isNullOrEmpty(name)) {

    Long acttime = new Date().getTime();

            CookieHelper.set("user", user.trim(), response);
            CookieHelper.set("name", name.trim(), response);
            CookieHelper.set("acttime", acttime.toString(), response);  CookieHelper.set("token", createToken(user, name, acttime), response);

        }
    }

   private static String createToken(String user, String name, Long acttime) {

        String str = String.format("%s_%s_%d", user.trim().toLowerCase(), name.trim().toLowerCase(), acttime);
        return MD5Helper.encrypt(str);
    }
}

你可能感兴趣的:(LoginInfoHelper cookie 设置超时时间)