Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

From source file:org.shareok.data.webserv.HomeController.java

@RequestMapping("/home")
public ModelAndView home(HttpServletRequest req, HttpServletResponse res) {

    ModelAndView model = new ModelAndView();
    model.setViewName("home");
    HttpSession session = (HttpSession) req.getSession(false);
    if (null != session) {
        RedisUser user = (RedisUser) session
                .getAttribute(ShareokdataManager.getSessionRedisUserAttributeName());
        if (null != user) {
            Cookie userCookie = new Cookie("userId", String.valueOf(user.getUserId()));
            userCookie.setMaxAge(30 * 60);
            res.addCookie(userCookie);
            model.addObject("user", user);
            model.addObject("loginTime", session.getCreationTime());
        }//from w w w  . j  ava2  s.  c  o  m
    }
    return model;
}

From source file:com.tamnd.app.filters.CsrfHeaderFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
    if (csrf != null) {
        Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
        String token = csrf.getToken();
        if (cookie == null || token != null && !token.equals(cookie.getValue())) {
            cookie = new Cookie("XSRF-TOKEN", token);
            cookie.setPath("/");
            response.addCookie(cookie);
        }/*from  w  w  w .  j av a2s  . com*/
    }
    filterChain.doFilter(request, response);

    //      CsrfToken token = (CsrfToken) request.getAttribute(REQUEST_ATTRIBUTE_NAME);
    //        if (token != null) {
    //            response.setHeader(RESPONSE_HEADER_NAME, token.getHeaderName());
    //            response.setHeader(RESPONSE_PARAM_NAME, token.getParameterName());
    //            response.setHeader(RESPONSE_TOKEN_NAME , token.getToken());
    //        }
    //        filterChain.doFilter(request, response);
}

From source file:pl.szcze.userserviceproject.CsrfHeaderFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    CsrfToken csrfToken = (CsrfToken) request.getAttribute(CsrfToken.class.getName());

    if (csrfToken != null) {
        Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
        String token = csrfToken.getToken();

        if (cookie == null || token != null && !token.equals(cookie.getValue())) {
            cookie = new Cookie("XSRF-TOKEN", token);
            cookie.setPath("/");
            response.addCookie(cookie);
        }/*from www .  j a va  2 s.  co m*/
    }

    filterChain.doFilter(request, response);
}

From source file:com.jredrain.session.HttpSessionFilter.java

private Cookie generateCookie(HttpServletRequest request, HttpServletResponse response) {
    Cookie sessionIdCookie;/*from  w  w  w.j av  a2s. co  m*/
    String sid = null;
    if (StringUtils.isBlank(sid)) {
        sid = CommonUtils.uuid();
    }
    sessionIdCookie = new Cookie(sessionIdCookieName, sid);

    String domain = request.getServerName();

    if (domain != null) {
        sessionIdCookie.setDomain(domain);
    }

    sessionIdCookie.setPath("/");
    response.addCookie(sessionIdCookie);
    return sessionIdCookie;
}

From source file:com.glaf.core.util.RequestUtils.java

public static void removeLoginUser(HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(cookie.getName(), Constants.COOKIE_NAME)) {
                cookie.setMaxAge(0);// w w w  . ja  va  2  s.c  o  m
                cookie.setPath("/");
                cookie.setValue(UUID32.getUUID());
                response.addCookie(cookie);
                logger.debug("remove user from cookie");
            }
        }
    }

    HttpSession session = request.getSession(false);
    if (session != null) {
        session.removeAttribute(Constants.LOGIN_INFO);
        session.invalidate();
    }
}

From source file:it.scoppelletti.programmerpower.web.security.SsoRememberMeServices.java

/**
 * Registra il cookie per l’autenticazione persistente.
 *
 * @param tokens Token codificati nel cookie.
 * @param maxAge Scadenza.//from   w  w  w  .  ja  v a 2  s .  c  o  m
 * @param req    Richiesta.
 * @param resp   Risposta.
 */
@Override
protected void setCookie(String[] tokens, int maxAge, HttpServletRequest req, HttpServletResponse resp) {
    String value;
    Cookie cookie;

    value = encodeCookie(tokens);
    cookie = buildCookie(value, maxAge);
    cookie.setSecure(mySecureCookie);
    resp.addCookie(cookie);
}

From source file:com.companyname.services.PlatCookieService.java

public void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String _cookieName) {
    logger.info("cancelling cookie named: " + _cookieName);

    // if cookie does not exist, do nothing
    Cookie cookie = getCookie(request, _cookieName);

    if (cookie == null) {
        return;/*from  w  w  w  .ja  va2 s .c om*/
    }

    cookie = new Cookie(_cookieName, null);
    cookie.setValue("");
    cookie.setMaxAge(0);
    cookie.setPath(getCookiePath());
    cookie.setDomain(getCookieDomain());
    response.addCookie(cookie);
}

From source file:de.hska.ld.core.controller.HomeController.java

@RequestMapping("/logout")
public String logout(HttpServletRequest request, HttpServletResponse response, Principal p)
        throws ServletException {
    request.logout();//  ww  w  .j av a  2 s  . c o m
    javax.servlet.http.Cookie cookie = new Cookie("sessionID", "");
    cookie.setPath("/");
    if (!"localhost".equals(env.getProperty("module.core.oidc.server.endpoint.main.domain"))) {
        cookie.setDomain(env.getProperty("module.core.oidc.server.endpoint.main.domain"));
    }
    cookie.setMaxAge(0);
    response.addCookie(cookie);
    return "redirect:";
}

From source file:com.hypersocket.session.json.SessionUtils.java

public void setLocale(HttpServletRequest request, HttpServletResponse response, String locale) {

    request.getSession().setAttribute(USER_LOCALE, locale);

    Cookie cookie = new Cookie(HYPERSOCKET_LOCALE, locale);
    cookie.setMaxAge(Integer.MAX_VALUE);
    cookie.setPath("/");
    cookie.setSecure(request.getProtocol().equalsIgnoreCase("https"));
    cookie.setDomain(request.getServerName());
    response.addCookie(cookie);

}

From source file:au.org.paperminer.main.UserFilter.java

/**
 * Effectively logs out the user//from  w  ww  .  ja v a 2 s.  c  o m
 * @param resp
 */
private void removeCookie(HttpServletResponse resp) {
    Cookie cookie = new Cookie(PaperMinerConstants.PM_COOKIE, "");
    cookie.setMaxAge(0);
    resp.addCookie(cookie);
}