Example usage for javax.servlet.http Cookie setSecure

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

Introduction

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

Prototype

public void setSecure(boolean flag) 

Source Link

Document

Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.

Usage

From source file:net.ymate.platform.mvc.web.support.CookieHelper.java

/**
 * @param key /*from  w w w. ja  va2s  . c  om*/
 * @param value 
 * @param maxAge 
 * @return ?Cookie
 */
public CookieHelper setCookie(String key, String value, int maxAge) {
    Cookie _cookie = new Cookie(WebMVC.getConfig().getCookiePrefix() + key,
            StringUtils.isBlank(value) ? "" : encodeValue(value));
    _cookie.setMaxAge(maxAge);
    _cookie.setPath(WebMVC.getConfig().getCookiePath());
    if (StringUtils.isNotBlank(WebMVC.getConfig().getCookieDomain())) {
        _cookie.setDomain(WebMVC.getConfig().getCookieDomain());
    }
    _cookie.setSecure(__request.getServerPort() == 443 ? true : false);
    WebContext.getResponse().addCookie(_cookie);
    return this;
}

From source file:org.kuali.mobility.shared.interceptors.NativeCookieInterceptor.java

/**
 * Attempts to detect the platform and sets the platform cookie
 *
 * @param request/*www. j av a 2 s  .  c o m*/
 * @param response
 * @return
 */
private String checkPlatform(HttpServletRequest request, HttpServletResponse response) {
    String platformParam = request.getParameter(COOKIE_PLATFORM);
    String platformCookie = findCookie(request.getCookies(), COOKIE_PLATFORM);
    String platformName;

    // If there is a platform param, rather use that
    if (!StringUtils.isEmpty(platformParam)) {
        platformName = platformParam;
    }
    // if there is a platform cookie, refresh it
    else if (!StringUtils.isEmpty(platformCookie)) {
        platformName = platformCookie;
    }
    // If there still is no platform, try and detect it
    else {
        platformName = findPlatform(request);
    }

    boolean useSecureCookies = Boolean
            .parseBoolean(getKmeProperties().getProperty("kme.secure.cookie", "false"));
    Cookie cookie = new Cookie(COOKIE_PLATFORM, platformName);
    int cookieMaxAge = Integer.parseInt(getKmeProperties().getProperty("cookie.max.age", "3600"));
    cookie.setMaxAge(cookieMaxAge); // default one hour, should implement in kme.config properties.
    cookie.setPath(request.getContextPath());
    cookie.setSecure(useSecureCookies);
    response.addCookie(cookie);
    LOG.debug("Setting platform cookie : " + platformName);

    request.getSession().setAttribute(SESSION_PLATFORM, platformName);
    return platformName;
}

From source file:org.kuali.mobility.shared.interceptors.NativeCookieInterceptor.java

/**
 * Attempts to detect the phonegap version and sets a cookie with the value
 *
 * @param request//from  w  w  w  .jav a 2  s .c o  m
 * @param response
 * @return
 */
private String checkPhonegap(HttpServletRequest request, HttpServletResponse response) {
    String phonegapParam = request.getParameter(COOKIE_PHONEGAP);
    String phoneGapCookie = findCookie(request.getCookies(), COOKIE_PHONEGAP);
    String phonegapVersion = null;

    // If there is a phonegap param present, rather use that
    if (!StringUtils.isEmpty(phonegapParam)) {
        phonegapVersion = phonegapParam;
    }
    // Else use the existing cookie if present
    else if (!StringUtils.isEmpty(phoneGapCookie)) {
        phonegapVersion = phoneGapCookie;
    }

    boolean useSecureCookies = Boolean
            .parseBoolean(getKmeProperties().getProperty("kme.secure.cookie", "false"));
    Cookie cookie = new Cookie(COOKIE_PHONEGAP, phonegapVersion);

    int cookieMaxAge = Integer.parseInt(getKmeProperties().getProperty("cookie.max.age", "3600"));
    cookie.setMaxAge(cookieMaxAge); // default one hour, should implement in kme.config properties.
    cookie.setPath(request.getContextPath());
    cookie.setSecure(useSecureCookies);
    response.addCookie(cookie);

    LOG.debug("Setting cordova version : " + phonegapVersion);
    request.getSession().setAttribute(SESSION_PHONEGAP, phonegapVersion);

    return phonegapVersion;
}

From source file:org.josso.gateway.signon.SignonBaseAction.java

protected void removeJossoSessionId(HttpServletRequest request, HttpServletResponse response) {
    SSOContext ctx = SSOContext.getCurrent();

    try {/*from  ww w .j  a  v a  2s  .c  om*/
        SSOWebConfiguration cfg = Lookup.getInstance().lookupSSOWebConfiguration();

        if (cfg.isSessionTokenOnClient()) {
            Cookie ssoCookie = newJossoCookie(request.getContextPath(),
                    JOSSO_SINGLE_SIGN_ON_COOKIE + "_" + ctx.getSecurityDomain().getName(), "-");
            ssoCookie.setMaxAge(0);
            response.addCookie(ssoCookie);
        } else {
            HttpSession session = request.getSession();
            session.removeAttribute(JOSSO_SINGLE_SIGN_ON_COOKIE + "_" + ctx.getSecurityDomain().getName());
        }

        if (cfg.isRememberMeEnabled()) {

            // Clear the remember me cookie
            Cookie rememberMeCookie = new Cookie(Constants.JOSSO_REMEMBERME_TOKEN + "_"
                    + SSOContext.getCurrent().getSecurityDomain().getName(), "-");
            rememberMeCookie.setMaxAge(0);
            rememberMeCookie.setSecure(cfg.isSessionTokenSecure());
            rememberMeCookie.setPath("/");

            response.addCookie(rememberMeCookie);
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled())
            logger.debug("  [removeJossoSessionId()] cant find SSOWebConfiguration");
    }
}

From source file:org.kuali.mobility.shared.interceptors.NativeCookieInterceptor.java

/**
 * Attempts tp check if the device is running natively and sets the native cookie
 *
 * @param request// w  w  w .  ja va  2 s  .c  om
 * @param phonegap
 * @return
 */
private boolean checkNative(HttpServletRequest request, HttpServletResponse response, String phonegap) {
    String nativeParam = request.getParameter("native");
    String nativeCookie = findCookie(request.getCookies(), COOKIE_NATIVE);
    boolean isNative = false;
    if (!StringUtils.isEmpty(nativeParam)) {
        isNative = "yes".equalsIgnoreCase(nativeParam);
    }
    // If there is a phonegap version, it must be native
    else if (!StringUtils.isEmpty(phonegap)) {
        isNative = true;
    }
    // Use the previous cookie value
    else if (!StringUtils.isEmpty(nativeCookie)) {
        isNative = "yes".equalsIgnoreCase(nativeCookie);
    }

    /*
     *  If detected a native setting, but there was no phonegap version, we have to 
     *  assume something is wrong and not enable nativeness
     */
    if (isNative && StringUtils.isEmpty(phonegap)) {
        LOG.info(
                "We detected a native user, but has no reference to a phonegap version - disabling nativeness");
        isNative = false;
    }

    // If there is a cordova version, it must be native
    boolean useSecureCookies = Boolean
            .parseBoolean(getKmeProperties().getProperty("kme.secure.cookie", "false"));
    Cookie cookie = new Cookie(COOKIE_NATIVE, (isNative ? "yes" : "no"));
    int cookieMaxAge = Integer.parseInt(getKmeProperties().getProperty("cookie.max.age", "3600"));
    cookie.setMaxAge(cookieMaxAge); // default one hour, should implement in kme.config properties.
    cookie.setPath(request.getContextPath());
    cookie.setSecure(useSecureCookies);
    response.addCookie(cookie);
    LOG.debug("Setting native cookie : " + isNative);

    request.getSession().setAttribute(SESSION_NATIVE, isNative);
    return isNative;
}

From source file:org.sakaiproject.nakamura.auth.trusted.TrustedTokenServiceImpl.java

/**
 * @param response/*from  w  w w  .j  a va2s  . c  o  m*/
 */
void clearCookie(HttpServletResponse response) {
    Cookie c = new HttpOnlyCookie(trustedAuthCookieName, "");
    c.setMaxAge(0);
    c.setPath("/");
    c.setSecure(secureCookie);
    response.addCookie(c);
}

From source file:com.zz.globalsession.filter.AbstractGlobalSessionFilter.java

private Cookie generateSessionIdCookie(String sessionIdValue) {

    Cookie sessionIdCookie = new Cookie(settings.getSessionIdKey(), sessionIdValue);
    if (settings.getDomain() != null) {
        sessionIdCookie.setDomain(settings.getDomain());
    }//w w w.  j a v  a  2s. c o  m
    if (settings.getPath() != null) {
        sessionIdCookie.setPath(settings.getPath());
    } else {
        sessionIdCookie.setPath("/");
    }
    if (settings.isSecure())
        sessionIdCookie.setSecure(settings.isSecure());
    // [Note] httpOnly is not supported by Servlet API 2.x, so add it
    // manually later.
    return sessionIdCookie;
}

From source file:com.codeabovelab.dm.gateway.proxy.common.HttpProxy.java

/**
 * Copy cookie from the proxy to the servlet client.
 * Replaces cookie path to local path and renames cookie to avoid collisions.
 *///  ww  w. j  av  a2  s  .  c  o m
private void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        Header header) {
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string
    for (int i = 0, l = cookies.size(); i < l; i++) {
        HttpCookie cookie = cookies.get(i);
        //set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = getCookieNamePrefix() + cookie.getName();
        Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        servletCookie.setPath(path); //set to the path of the proxy servlet
        // don't set cookie domain
        servletCookie.setSecure(cookie.getSecure());
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}

From source file:com.activecq.experiments.redis.impl.RedisSessionUtilImpl.java

@Override
public Cookie createSessionCookie() {
    final Cookie cookie = new Cookie(this.getSessionCookieName(), java.util.UUID.randomUUID().toString());

    cookie.setPath("/");
    cookie.setSecure(this.secureCookie);

    // Expire with browser session
    cookie.setMaxAge(-1);/*from w w  w .ja v a 2 s .co m*/

    return cookie;
}

From source file:org.jboss.web.loadbalancer.Loadbalancer.java

protected HttpClient prepareServerRequest(HttpServletRequest request, HttpServletResponse response,
        HttpMethod method) {/*  w  ww  . ja  v  a  2s . co m*/
    // clear state
    HttpClient client = new HttpClient(connectionManager);
    client.setStrictMode(false);
    client.setTimeout(connectionTimeout);
    method.setFollowRedirects(false);
    method.setDoAuthentication(false);
    client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);

    Enumeration reqHeaders = request.getHeaderNames();

    while (reqHeaders.hasMoreElements()) {
        String headerName = (String) reqHeaders.nextElement();
        String headerValue = request.getHeader(headerName);

        if (!ignorableHeader.contains(headerName.toLowerCase())) {
            method.setRequestHeader(headerName, headerValue);
        }
    }

    //Cookies
    Cookie[] cookies = request.getCookies();
    HttpState state = client.getState();

    for (int i = 0; cookies != null && i < cookies.length; ++i) {
        Cookie cookie = cookies[i];

        org.apache.commons.httpclient.Cookie reqCookie = new org.apache.commons.httpclient.Cookie();

        reqCookie.setName(cookie.getName());
        reqCookie.setValue(cookie.getValue());

        if (cookie.getPath() != null) {
            reqCookie.setPath(cookie.getPath());
        } else {
            reqCookie.setPath("/");
        }

        reqCookie.setSecure(cookie.getSecure());

        reqCookie.setDomain(method.getHostConfiguration().getHost());
        state.addCookie(reqCookie);
    }
    return client;
}