Example usage for javax.servlet.http Cookie setDomain

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

Introduction

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

Prototype

public void setDomain(String domain) 

Source Link

Document

Specifies the domain within which this cookie should be presented.

Usage

From source file:io.gravitee.management.security.JWTCookieGenerator.java

public Cookie generate(final String value) {
    final Cookie cookie = new Cookie(HttpHeaders.AUTHORIZATION, value);
    cookie.setHttpOnly(true);//from   w ww .  j a  v a2  s.co  m
    cookie.setSecure(environment.getProperty("jwt.cookie-secure", Boolean.class, DEFAULT_JWT_COOKIE_SECURE));
    cookie.setPath(environment.getProperty("jwt.cookie-path", DEFAULT_JWT_COOKIE_PATH));
    cookie.setDomain(environment.getProperty("jwt.cookie-domain", DEFAULT_JWT_COOKIE_DOMAIN));
    return cookie;
}

From source file:com.junly.service.helper.TicketHelper.java

/** <p class="detail">
* cookie/* w  w  w.j av  a 2 s.c o m*/
* </p>
* @author junly
* @date 2016422 
* @param response
* @param ticket    
*/
public void setCookie(HttpServletRequest request, HttpServletResponse response, String ticket) {
    // ?
    Cookie cookie = new Cookie(ViewContants.LOGIN_TICKET_KEY, ticket);

    // ? ???
    cookie.setDomain(request.getServerName());
    // path
    cookie.setPath("/");
    // ??
    cookie.setMaxAge(ViewContants.TRUST_COOKIE_TIME); // 
    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();//from  w w w  .j av  a2s. 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.alfaariss.oa.util.web.CookieTool.java

/**
 * Set Cookie with optional extra context in application context
 * @param sCookie/*from   w w  w. j  a  va2  s .  co  m*/
 * @param sValue
 * @param sExtraContext
 * @param oRequest
 * @return
 */
public Cookie createCookie(String sCookie, String sValue, String sExtraContext, HttpServletRequest oRequest) {
    assert sValue != null : "Supplied value == null";
    assert oRequest != null : "Supplied request == null";

    Cookie cookie = new Cookie(sCookie, sValue);
    if (_sCookieDomain != null) {
        cookie.setDomain(_sCookieDomain);
        _logger.debug("Created domain cookie on " + _sCookieDomain);
    }

    if (_iCookieVersion != -1) {
        cookie.setVersion(_iCookieVersion);
        _logger.debug("Setting cookie version: " + _iCookieVersion);
    }

    /* format sExtraContext */
    if (sExtraContext == null) {
        sExtraContext = "";
    } else {
        if (!sExtraContext.startsWith("/")) {
            sExtraContext = "/" + sExtraContext;
        }
    }

    String path = oRequest.getContextPath();
    if (path != null && path.length() > 0) {//only set path if path not is empty (when hosted as server root, getContextPath() will return an empty string)
        cookie.setPath(path + sExtraContext);// /openaselect
    } else {//if no contextpath available then setting the cookie path on '/' instead of on the default path (which is for the sso cookie: /openaselect/sso)
        cookie.setPath("/" + sExtraContext);
    }

    cookie.setSecure(_bSecureCookie);

    StringBuffer sbDebug = new StringBuffer("Created '");
    sbDebug.append(sCookie);
    sbDebug.append("' on path=");
    sbDebug.append(cookie.getPath());
    _logger.debug(sbDebug.toString());

    return cookie;
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDefaultPath() {
    cookieGenerator.setCookieName("guid");
    cookieGenerator.setHttpOnly(true);//server side
    BDDMockito.given(request.getContextPath()).willReturn("/");
    cookieGenerator.addCookie(response, "cookie_monster");
    cookieGenerator.setUseDefaultPath(false);

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

    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "guid=cookie_monster; Version=1; Domain=\"what a domain\"; Path=/; HttpOnly");
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side
    cookieGenerator.addCookie(response, "cookie_monster");
    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);// w  ww. j a  v a 2  s  .c  om
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

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

From source file:com.byd.test.actions.OrderAction.java

License:asdf

@RequestMapping("createCookie")
public void createCookie(HttpServletResponse response) {
    System.out.println("cookie start");
    Cookie cookie = new Cookie("cookie_name", "whatisthis");
    cookie.setHttpOnly(Boolean.TRUE);
    cookie.setDomain("chengangxiong");
    cookie.setVersion(1);//w  w  w. java2 s.  co m
    cookie.setMaxAge(15);//15
    response.addCookie(cookie);

}

From source file:com.ms.commons.cookie.parser.CookieNameHelper.java

/**
 * ????CookieName?Response//ww  w  .j av  a2  s.com
 * 
 * <pre>
 * cookie<code>null</code>blankCookie
 * </pre>
 */
public void saveIfModified(HttpServletResponse response) {
    if (!isModified) {
        return;
    }
    String value = config.isSimpleValue() ? simpleValue : CookieUtils.mapToStr(allCookieKeyValues);
    if (config.isEncrypt()) {
        value = CookieUtils.encrypt(value);
    }
    Cookie cookie = new Cookie(cookieName, value);
    if (StringUtils.isBlank(value)) {
        cookie.setMaxAge(CookieMaxAge.OUT_OF_DATE);
    } else {
        cookie.setMaxAge(config.getMaxAge());
    }
    cookie.setDomain(config.getDomain().getDomain());
    cookie.setPath(config.getPath().getPath());
    response.addCookie(cookie);

    // ?????
    this.isModified = false;
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(true);//server side
    cookieGenerator.setUseDefaultPath(false);
    BDDMockito.given(request.getContextPath()).willReturn("/some_path");
    cookieGenerator.addCookie(response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/some_path");
    expectedCookie.setSecure(false);/* w  ww. j a  va2  s  .  co  m*/
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "JSESSIONID=cookie_monster; Version=1; Domain=\"what a domain\"; Path=/some_path; HttpOnly");
}

From source file:com.mmj.app.common.cookie.parser.CookieNameHelper.java

/**
 * ????CookieName?Response//from  w w  w .  j  a v  a  2 s .c o m
 * 
 * <pre>
 * cookie<code>null</code>blankCookie
 * </pre>
 */
public void saveIfModified(HttpServletResponse response) {
    if (!isModified) {
        return;
    }
    String value = config.isSimpleValue() ? simpleValue : CookieUtils.mapToStr(allCookieKeyValues);
    if (config.isEncrypt()) {
        value = EncryptBuilder.getInstance().encrypt(value);
    }
    Cookie cookie = new Cookie(cookieName, value);
    if (StringUtils.isBlank(value)) {
        cookie.setMaxAge(CookieMaxAge.OUT_OF_DATE);
    } else {
        cookie.setMaxAge(config.getMaxAge());
    }
    cookie.setDomain(config.getDomain().getDomain());
    cookie.setPath(config.getPath().getPath());
    response.addCookie(cookie);

    // ?????
    this.isModified = false;
}