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:org.springframework.http.server.reactive.ServletServerHttpResponse.java

@Override
protected void writeCookies() {
    for (String name : getCookies().keySet()) {
        for (ResponseCookie httpCookie : getCookies().get(name)) {
            Cookie cookie = new Cookie(name, httpCookie.getValue());
            if (!httpCookie.getMaxAge().isNegative()) {
                cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
            }/*  ww  w  .  java 2  s . c  o m*/
            httpCookie.getDomain().ifPresent(cookie::setDomain);
            httpCookie.getPath().ifPresent(cookie::setPath);
            cookie.setSecure(httpCookie.isSecure());
            cookie.setHttpOnly(httpCookie.isHttpOnly());
            this.response.addCookie(cookie);
        }
    }
}

From source file:net.yacy.cora.protocol.ResponseHeader.java

/**
 * Sets Cookie on the client machine./*from w  ww  . ja v  a2s.  c o m*/
 *
 * @param name Cookie name
 * @param value Cookie value
 * @param maxage time to live in seconds, none negative number, according to https://tools.ietf.org/html/rfc2109, 0=discard in https://tools.ietf.org/html/rfc2965
 * @param path Path the cookie belongs to. Default - "/". Can be <b>null</b>.
 * @param domain Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
 * @param secure If true cookie will be send only over safe connection such as https
 * @see further documentation: <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
 */
public void setCookie(final String name, final String value, final Integer maxage, final String path,
        final String domain, final boolean secure) {
    /*
    * TODO:Here every value can be validated for correctness if needed
    * For example semicolon should be not in any of the values
    * However an exception in this case would be an overhead IMHO.
    */
    if (!name.isEmpty()) {
        if (this.cookieStore == null)
            this.cookieStore = new ArrayList<Cookie>();
        Cookie c = new Cookie(name, value);
        if (maxage != null && maxage >= 0)
            c.setMaxAge(maxage);
        if (path != null)
            c.setPath(path);
        if (domain != null)
            c.setDomain(domain);
        if (secure)
            c.setSecure(secure);
        this.cookieStore.add(c);
    }
}

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

/**
 * Registra il cookie per l&rsquo;autenticazione persistente.
 *
 * @param tokens Token codificati nel cookie.
 * @param maxAge Scadenza.//from  w ww. j  a 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.mockey.model.ResponseFromService.java

private void setCookiesFromHeader(Header[] headers) {
    for (Header header : headers) {

        if (header.getName().equals("Set-Cookie")) {
            String headerValue = header.getValue();
            // Parse cookie
            String[] fields = headerValue.split(";\\s*");

            //String cookieValue = fields[0];
            //String expires = null;
            String path = null;/*from   w w w .j  a  va 2  s .c o  m*/
            String domain = null;
            boolean secure = false;

            // Parse each field
            for (int j = 1; j < fields.length; j++) {
                if ("secure".equalsIgnoreCase(fields[j])) {
                    secure = true;
                } else if (fields[j].indexOf('=') > 0) {
                    String[] f = fields[j].split("=");
                    if ("expires".equalsIgnoreCase(f[0])) {
                        //expires = f[1];
                    } else if ("domain".equalsIgnoreCase(f[0])) {
                        domain = f[1];
                    } else if ("path".equalsIgnoreCase(f[0])) {
                        path = f[1];
                    }
                }
            }
            String[] cookieParts = headerValue.split("=", 2);
            String cookieBody = cookieParts[1];
            String[] cookieBodyParts = cookieBody.split("; ");
            Cookie cookie = new Cookie(cookieParts[0], cookieBodyParts[0]);
            cookie.setDomain(domain);
            cookie.setPath(path);
            cookie.setSecure(secure);
            //            if(expires!=null){
            //            Date expiresTime = null;
            //            try {
            //               expiresTime = HttpCookieDateUtil.parseDate(expires);
            //               Date nowTime = new Date();
            //               long maxAge = nowTime.getTime() - expiresTime.getTime();
            //               cookie.setMaxAge((int) maxAge/1000);
            //            }catch(Exception e){
            //               log.error("Unable to calculate maxAge with expiration date "+expiresTime, e);
            //            }
            //            }
            this.cookieList.add(cookie);
        }

    }
}

From source file:com.silverpeas.authentication.AuthenticationServlet.java

/**
 * Write connections cookie.//  w ww  . j  a va2s . c  om
 *
 * @param name
 * @param value
 * @param duration
 * @return
 */
private void writeCookie(HttpServletResponse response, String name, String value, int duration,
        boolean secure) {
    String cookieValue;
    try {
        cookieValue = URLEncoder.encode(value, CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException ex) {
        cookieValue = value;
    }
    Cookie cookie = new Cookie(name, cookieValue);
    cookie.setMaxAge(duration);
    cookie.setPath("/");
    if (secure) {
        cookie.setSecure(true);
    }
    response.addCookie(cookie);
}

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

/**
 * Attempts to detect REMOTE_USER and sets currentNetworkId cookie with the value
 *
 * @param request/*from w w  w .  ja  v  a 2s . c om*/
 * @param response
 * @return
 * @deprecated This could should be placed in an other interceptor, this interceptor is only meant to detect platform specifics
 */
@Deprecated
private void checkAuthenticatedUser(HttpServletRequest request, HttpServletResponse response) {
    String loggedInUser = request.getRemoteUser();
    User user;
    if (StringUtils.isEmpty(loggedInUser)
            && ((user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY)) != null)) {
        loggedInUser = user.getLoginName();
    }

    LOG.debug("REMOTE_USER: " + loggedInUser);
    if (loggedInUser != null && !loggedInUser.trim().isEmpty()) {
        boolean useSecureCookies = Boolean
                .parseBoolean(getKmeProperties().getProperty("kme.secure.cookie", "false"));
        Cookie userCookie = new Cookie("currentNetworkId", loggedInUser);
        userCookie.setMaxAge(60 * 60); //1hr
        userCookie.setPath(request.getContextPath());
        userCookie.setSecure(useSecureCookies);
        response.addCookie(userCookie);
        LOG.debug("Setting currentNetworkId cookie : " + loggedInUser);
    }
}

From source file:fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService.java

/**
 * set a paris connect cokkie in the HttpServletResponse
 *
 * @param strPCUID/*  w w  w. j  a  v a 2 s.  c om*/
 *            the user PCUID
 * @param response
 *            The HTTP response
 */
public void removeConnectionCookie(HttpServletResponse response) {
    // remove  openam cookie using the setMaxAgeParameters
    Cookie openamCookie = new Cookie(COOKIE_OPENAM_NAME, null);
    openamCookie.setDomain(COOKIE_OPENAM_DOMAIN);
    openamCookie.setSecure(COOKIE_OPENAM_SECURE);
    openamCookie.setMaxAge(0);
    openamCookie.setPath(COOKIE_OPENAM_PATH);
    response.addCookie(openamCookie);
}

From source file:com.shenit.commons.utils.HttpUtils.java

/**
 * cookie/*w  w  w  .  j  a v  a2 s .co  m*/
 * 
 * @param name
 *            Cookie??
 * @param val
 *            
 * @param expiry
 *            
 * @param domain
 *            
 * @param path
 *            
 * @param httpOnly
 *            ??HTTPcooie
 * @param secure
 *            ?SSL
 * @return
 */
public static Cookie cookie(String name, Object val, Integer expiry, String domain, String path,
        boolean httpOnly, boolean secure) {
    Cookie cookie = new Cookie(name, val == null ? null : val.toString());
    if (expiry != null)
        cookie.setMaxAge(expiry);
    if (!StringUtils.isEmpty(domain))
        cookie.setDomain(domain);
    cookie.setSecure(secure);
    if (!StringUtils.isEmpty(path))
        cookie.setPath(path);
    cookie.setHttpOnly(httpOnly);
    return cookie;
}

From source file:fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService.java

/**
 * set a paris connect cokkie in the HttpServletResponse
 *
 * @param strPCUID/* w  w  w.  ja v a 2  s  .  c om*/
 *            the user PCUID
 * @param response
 *            The HTTP response
 */
public void setConnectionCookie(String strPCUID, HttpServletResponse response) {
    // set a connexion cookie to let the user access other PC Services
    // without sign in
    Cookie openamCookie = new Cookie(COOKIE_OPENAM_NAME, strPCUID);
    openamCookie.setDomain(COOKIE_OPENAM_DOMAIN);
    openamCookie.setSecure(COOKIE_OPENAM_SECURE);
    openamCookie.setMaxAge(COOKIE_OPENAM_MAX_AGE);
    openamCookie.setPath(COOKIE_OPENAM_PATH);

    response.addCookie(openamCookie);
}

From source file:io.mapzone.controller.vm.http.HttpResponseForwarder.java

/**
 * Copy cookie from the proxy to the servlet client. Replaces cookie path to
 * local path and renames cookie to avoid collisions.
 *///  w w  w . j  a v a 2  s .  c o  m
protected 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 (HttpCookie cookie : cookies) {
        // set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = requestForwarder.cookieNamePrefix.get() + 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);
    }
}