Example usage for javax.servlet.http Cookie Cookie

List of usage examples for javax.servlet.http Cookie Cookie

Introduction

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

Prototype

public Cookie(String name, String value) 

Source Link

Document

Constructs a cookie with the specified name and value.

Usage

From source file:io.interface21.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())) {
            // Angular is capable to handle CSRF protection and expected a token with name XSRF-TOKEN
            cookie = new Cookie("XSRF-TOKEN", token);
            cookie.setPath("/");
            response.addCookie(cookie);//from   ww  w  .java2s  . co  m
        }
    }
    filterChain.doFilter(request, response);
}

From source file:org.tsm.concharto.auth.AuthHelper.java

public static void setCookie(HttpServletResponse response, String cookieName, int maxAge, String value) {
    Cookie cookie = new Cookie(cookieName, value);
    cookie.setMaxAge(maxAge);//  w ww  .  j  a  v  a 2  s  .  c om
    response.addCookie(cookie);
}

From source file:org.uaa.security.core.LoginSuccessHandler.java

public void handle(UsernamePasswordToken token, HttpServletRequest request, HttpServletResponse response) {
    // set cookie
    Map<String, Object> session = new LinkedHashMap<String, Object>();
    session.put("uid", token.getUid());
    session.put("username", token.getUsername());
    session.put("password", token.getPassword());
    session.put("roles", token.getRoles());
    session.put("isAuthenticated", token.isAuthenticated());

    try {//from  w ww  . j av  a2 s.  co m
        String sessionId = Crypto.encrypt(JsonFactory.toJson(session));
        log.debug(sessionId);
        Cookie cookie = new Cookie(ConfigUtil.getValue("SESSION_ID"), sessionId);
        cookie.setMaxAge(7 * 24 * 3600);
        response.addCookie(cookie);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.netflix.genie.web.controllers.UIController.java

/**
 * Return the index.html file for requests to root.
 *
 * @param response       The servlet response to add cookies to
 * @param authentication The Spring Security authentication if present
 * @return getIndex//  w  ww. jav a2s.c  om
 */
@GetMapping(value = { "/", "/applications/**", "/clusters/**", "/commands/**", "/jobs/**", "/output/**" })
public String getIndex(@NotNull final HttpServletResponse response,
        @Nullable final Authentication authentication) {
    if (authentication != null) {
        response.addCookie(new Cookie("genie.user", authentication.getName()));
    } else {
        response.addCookie(new Cookie("genie.user", "user@genie"));
    }
    return "index.html";
}

From source file:de.appsolve.padelcampus.utils.LoginUtil.java

public void updateLoginCookie(HttpServletRequest request, HttpServletResponse response) {
    Player player = sessionUtil.getUser(request);
    if (player != null) {
        UUID cookieUUID = UUID.randomUUID();
        UUID cookieValue = UUID.randomUUID();
        String cookieValueHash = BCrypt.hashpw(cookieValue.toString(), BCrypt.gensalt());
        LoginCookie loginCookie = new LoginCookie();
        loginCookie.setUUID(cookieUUID.toString());
        loginCookie.setPlayerUUID(player.getUUID());
        loginCookie.setLoginCookieHash(cookieValueHash);
        loginCookie.setValidUntil(new LocalDate().plusYears(1));
        loginCookieDAO.saveOrUpdate(loginCookie);
        Cookie cookie = new Cookie(COOKIE_LOGIN_TOKEN, cookieUUID.toString() + ":" + cookieValue.toString());
        cookie.setDomain(request.getServerName());
        cookie.setMaxAge(ONE_YEAR_SECONDS);
        cookie.setPath("/");
        response.addCookie(cookie);//from   w w w.  j a  v a  2 s .  co m
    }
}

From source file:de.knightsoftnet.validators.server.security.CsrfCookieHandler.java

/**
 * set csrf/xsrf cookie.//from w  w  w.  j av  a 2s. c om
 */
public void setCookie(final HttpServletRequest prequest, final HttpServletResponse presponse)
        throws IOException {
    final CsrfToken csrf = (CsrfToken) prequest.getAttribute(CsrfToken.class.getName());
    if (csrf != null) {
        Cookie cookie = WebUtils.getCookie(prequest, ResourcePaths.XSRF_COOKIE);
        final String token = csrf.getToken();
        if (cookie == null || token != null && !token.equals(cookie.getValue())) {
            cookie = new Cookie(ResourcePaths.XSRF_COOKIE, token);
            cookie.setPath(StringUtils.defaultString(StringUtils.trimToNull(prequest.getContextPath()), "/"));
            presponse.addCookie(cookie);
        }
    }
}

From source file:com.leixl.easyframework.web.CookieUtils.java

/**
 * ?cookie?//from   w  w  w  .  j  a  v a 2s .c o  m
 * 
 * @param request
 * @param response
 * @param name
 * @param value
 * @param expiry
 * @param domain
 * @return
 */
public static Cookie addCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, Integer expiry, String domain) {
    Cookie cookie = new Cookie(name, value);
    if (expiry != null) {
        cookie.setMaxAge(expiry);
    }
    if (StringUtils.isNotBlank(domain)) {
        cookie.setDomain(domain);
    }
    String ctx = request.getContextPath();
    cookie.setPath(StringUtils.isBlank(ctx) ? "/" : ctx);
    response.addCookie(cookie);
    return cookie;
}

From source file:com.epam.cme.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);// client side

    cookieGenerator.addCookie(request, response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);//from   ww w  . j av  a 2 s.com
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    assertNoHeaderAdjustments();

}

From source file:de.hybris.platform.ytelcoacceleratorstorefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side

    cookieGenerator.addCookie(request, response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);/*from w  w w .j a va  2  s .  c  o  m*/
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    assertNoHeaderAdjustments();

}

From source file:edu.indiana.d2i.sloan.ui.LogoutAction.java

public String execute() throws Exception {
    String user = (String) session.get(Constants.SESSION_USERNAME);
    logger.info(user + " log out.");
    session.clear();/*  ww  w  .j  a va2  s . c om*/
    httpResponse.addCookie(new Cookie(Constants.SESSION_EXIST_BEFORE, "false"));
    return SUCCESS;
}