List of usage examples for javax.servlet.http Cookie setMaxAge
public void setMaxAge(int expiry)
From source file:com.lushapp.common.web.utils.WebUtils.java
/** * Cookie./*from w ww .j av a2 s .c o m*/ * @param response * @param cookie * @param path */ public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) { if (cookie != null) { cookie.setMaxAge(0); cookie.setPath(path); response.addCookie(cookie); } }
From source file:com.ofbizcn.securityext.login.LoginEvents.java
public static void setUsername(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); Delegator delegator = (Delegator) request.getAttribute("delegator"); String domain = EntityUtilProperties.getPropertyValue("url.properties", "cookie.domain", delegator); // first try to get the username from the cookie synchronized (session) { if (UtilValidate.isEmpty(getUsername(request))) { // create the cookie and send it back Cookie cookie = new Cookie(usernameCookieName, request.getParameter("USERNAME")); cookie.setMaxAge(60 * 60 * 24 * 365); cookie.setPath("/"); cookie.setDomain(domain);/*from w w w . j av a 2s .co m*/ response.addCookie(cookie); } } }
From source file:org.b3log.latke.util.Requests.java
/** * Determines whether the specified request has been served. * /*from w ww .j a v a 2 s .com*/ * <p> * A "served request" is a request a URI as former one. For example, if a client is request "/test", all requests from the client * subsequent in 24 hours will be treated as served requests, requested URIs save in client cookie (name: "visited"). * </p> * * <p> * If the specified request has not been served, appends the request URI in client cookie. * </p> * * <p> * Sees this issue (https://github.com/b3log/b3log-solo/issues/44) for more details. * </p> * * @param request the specified request * @param response the specified response * @return {@code true} if the specified request has been served, returns {@code false} otherwise */ public static boolean hasBeenServed(final HttpServletRequest request, final HttpServletResponse response) { final Cookie[] cookies = request.getCookies(); if (null == cookies || 0 == cookies.length) { return false; } Cookie cookie; boolean needToCreate = true; boolean needToAppend = true; JSONArray cookieJSONArray = null; try { for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; if (!"visited".equals(cookie.getName())) { continue; } cookieJSONArray = new JSONArray(cookie.getValue()); if (null == cookieJSONArray || 0 == cookieJSONArray.length()) { return false; } needToCreate = false; for (int j = 0; j < cookieJSONArray.length(); j++) { final String visitedURL = cookieJSONArray.optString(j); if (request.getRequestURI().equals(visitedURL)) { needToAppend = false; return true; } } } if (needToCreate) { final StringBuilder builder = new StringBuilder("[").append("\"").append(request.getRequestURI()) .append("\"]"); final Cookie c = new Cookie("visited", builder.toString()); c.setMaxAge(COOKIE_EXPIRY); c.setPath("/"); response.addCookie(c); } else if (needToAppend) { cookieJSONArray.put(request.getRequestURI()); final Cookie c = new Cookie("visited", cookieJSONArray.toString()); c.setMaxAge(COOKIE_EXPIRY); c.setPath("/"); response.addCookie(c); } } catch (final Exception e) { LOGGER.log(Level.WARNING, "Parses cookie failed, clears the cookie[name=visited]", e); final Cookie c = new Cookie("visited", null); c.setMaxAge(0); c.setPath("/"); response.addCookie(c); } return false; }
From source file:com.lushapp.common.web.utils.WebUtils.java
/** * cookie.// w w w.j a v a2 s. c o m * @param response * @param name * @param value * @param path */ public static void setCookie(HttpServletResponse response, String name, String value, String path) { if (logger.isDebugEnabled()) { logger.debug("Cookie '" + name + "',?: '" + path + "'"); } Cookie cookie = new Cookie(name, value); cookie.setSecure(false); cookie.setPath(path); cookie.setMaxAge(2592000); response.addCookie(cookie); }
From source file:org.b3log.solo.util.Solos.java
/** * Logouts the specified user.//from w ww . j av a2 s. c om * * @param request the specified request * @param response the specified response * @return {@code true} if succeed, otherwise returns {@code false} */ public static void logout(final HttpServletRequest request, final HttpServletResponse response) { if (null != response) { final Cookie cookie = new Cookie(COOKIE_NAME, null); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); } }
From source file:cn.vlabs.umt.ui.servlet.login.LoginMethod.java
/** * ??cookie// w w w .ja va2 s.c om * */ public static void generateSsoCookie(HttpServletResponse response, HttpServletRequest request, LoginInfo loginInfo) throws UnsupportedEncodingException { PCookie pcookie = (PCookie) ServiceFactory.getBean(request, "PCookie"); // Pcookie String encrypted = pcookie .encrypt(loginInfo.getUser().getCstnetId() + "/" + RequestUtil.getRemoteIP(request) + "/" + loginInfo.getPasswordType() + "/" + System.currentTimeMillis()); Cookie cookie = new Cookie(Attributes.COOKIE_NAME, encrypted); cookie.setPath("/"); cookie.setMaxAge(MAX_COOKIE_AGE); response.addCookie(cookie); Cookie umtIdCookie = new Cookie(Attributes.SSO_FLAG, SessionUtils.getUserId(request) + ""); umtIdCookie.setDomain(Attributes.SSO_FLAG_DOMAIN); umtIdCookie.setPath("/"); umtIdCookie.setMaxAge(LoginMethod.MAX_COOKIE_AGE); response.addCookie(umtIdCookie); }
From source file:org.apache.ofbiz.securityext.login.LoginEvents.java
public static void setUsername(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); Delegator delegator = (Delegator) request.getAttribute("delegator"); String domain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", delegator); // first try to get the username from the cookie synchronized (session) { if (UtilValidate.isEmpty(getUsername(request))) { // create the cookie and send it back Cookie cookie = new Cookie(usernameCookieName, request.getParameter("USERNAME")); cookie.setMaxAge(60 * 60 * 24 * 365); cookie.setPath("/"); cookie.setDomain(domain);/*from w w w . j av a 2s . c om*/ response.addCookie(cookie); } } }
From source file:cn.vlabs.umt.ui.servlet.login.LoginMethod.java
public static void generateAutoFill(HttpServletResponse response, HttpServletRequest request, LoginInfo loginInfo) {//from ww w .j a v a 2 s . c om Cookie autoFill = new Cookie(Attributes.AUTO_FILL, loginInfo.getLoginNameInfo().getLoginName()); autoFill.setPath("/"); autoFill.setMaxAge(Integer.MAX_VALUE); response.addCookie(autoFill); }
From source file:org.b3log.solo.util.Solos.java
/** * Logins the specified user from the specified request. * * @param response the specified response * @param user the specified user, for example, * {//from www. j a v a2 s. c o m * "userEmail": "", * "userPassword": "" * } */ public static void login(final JSONObject user, final HttpServletResponse response) { try { final String userId = user.optString(Keys.OBJECT_ID); final JSONObject cookieJSONObject = new JSONObject(); cookieJSONObject.put(Keys.OBJECT_ID, userId); cookieJSONObject.put(User.USER_PASSWORD, user.optString(User.USER_PASSWORD)); final String random = RandomStringUtils.randomAlphanumeric(16); cookieJSONObject.put(Keys.TOKEN, user.optString(User.USER_PASSWORD) + ":" + random); final String cookieValue = Crypts.encryptByAES(cookieJSONObject.toString(), COOKIE_SECRET); final Cookie cookie = new Cookie(COOKIE_NAME, cookieValue); cookie.setPath("/"); cookie.setMaxAge(COOKIE_EXPIRY); cookie.setHttpOnly(COOKIE_HTTP_ONLY); response.addCookie(cookie); } catch (final Exception e) { LOGGER.log(Level.WARN, "Can not write cookie", e); } }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
/** * Logs in a user returned from database after the login test was made.<br/> * @param dbUser the user from database (session may not be closed!) * @param passedLogin did the user pass the login test? * @param rememberMe will the user stay logged in? * @param userLogin the login object// w w w . j av a 2s .c o m * @param session the hibernate session for further queries * @return true if the user was logged in */ private static boolean loginUser(final User dbUser, final boolean passedLogin, final boolean rememberMe, final LoginBean userLogin, final Session session) { boolean didLogin = false; if (passedLogin && dbUser.getActive() == ActiveState.ACTIVE) { didLogin = true; userLogin.setUserName(dbUser.getName()); userLogin.setLoggedIn(true); } else { didLogin = false; userLogin.setUserName(userLogin.getUserName()); userLogin.setLoggedIn(false); } dbUser.setLastlogin(new Date()); final UUID loginUUID = UUID.randomUUID(); Cookie cookie = null; final int tenDays = 60 * 60 * 24 * 10; if (rememberMe && passedLogin) { cookie = new Cookie("login", loginUUID.toString()); cookie.setMaxAge(tenDays); dbUser.setCookieid(loginUUID.toString()); } else { cookie = new Cookie("login", ""); cookie.setMaxAge(0); dbUser.setCookieid(""); } userLogin.clearPermissions(); dbUser.transferPermissionsToLogin(userLogin); for (final UserGroup grp : dbUser.getGroups()) { grp.transferPermissionsToLogin(userLogin); } final HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); httpServletResponse.addCookie(cookie); return didLogin; }