javax.servlet.http.Cookie.setSecure()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(224)

本文整理了Java中javax.servlet.http.Cookie.setSecure()方法的一些代码示例,展示了Cookie.setSecure()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cookie.setSecure()方法的具体详情如下:
包路径:javax.servlet.http.Cookie
类名称:Cookie
方法名:setSecure

Cookie.setSecure介绍

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

The default value is false.
[中]向浏览器指示是否应仅使用安全协议(如HTTPS或SSL)发送cookie。
默认值为false

代码示例

代码示例来源:origin: ZHENFENG13/My-Blog

/**
 * 设置cookie
 *
 * @param name
 * @param value
 * @param maxAge
 * @param response
 */
private void cookie(String name, String value, int maxAge, HttpServletResponse response) {
  Cookie cookie = new Cookie(name, value);
  cookie.setMaxAge(maxAge);
  cookie.setSecure(false);
  response.addCookie(cookie);
}

代码示例来源:origin: perwendel/spark

/**
 * Adds cookie to the response. Can be invoked multiple times to insert more than one cookie.
 *
 * @param domain   domain of the cookie
 * @param path     path of the cookie
 * @param name     name of the cookie
 * @param value    value of the cookie
 * @param maxAge   max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
 * @param secured  if true : cookie will be secured
 * @param httpOnly if true: cookie will be marked as http only
 */
public void cookie(String domain, String path, String name, String value, int maxAge, boolean secured, boolean httpOnly) {
  Cookie cookie = new Cookie(name, value);
  cookie.setPath(path);
  cookie.setDomain(domain);
  cookie.setMaxAge(maxAge);
  cookie.setSecure(secured);
  cookie.setHttpOnly(httpOnly);
  response.addCookie(cookie);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Remove the cookie that this generator describes from the response.
 * Will generate a cookie with empty value and max age 0.
 * <p>Delegates to {@link #createCookie} for cookie creation.
 * @param response the HTTP response to remove the cookie from
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 */
public void removeCookie(HttpServletResponse response) {
  Assert.notNull(response, "HttpServletResponse must not be null");
  Cookie cookie = createCookie("");
  cookie.setMaxAge(0);
  if (isCookieSecure()) {
    cookie.setSecure(true);
  }
  if (isCookieHttpOnly()) {
    cookie.setHttpOnly(true);
  }
  response.addCookie(cookie);
  if (logger.isTraceEnabled()) {
    logger.trace("Removed cookie '" + getCookieName() + "'");
  }
}

代码示例来源:origin: pippo-java/pippo

private Cookie createSessionIdCookie(HttpServletRequest request, String sessionId) {
  Cookie cookie = new Cookie(SESSION_ID_COOKIE_NAME, sessionId);
  cookie.setHttpOnly(true);
  cookie.setSecure(request.isSecure());
  cookie.setPath(request.getContextPath() + "/");
  // TODO setDomain
  return cookie;
}

代码示例来源:origin: com.google.inject.extensions/guice-servlet

public ImmutableCookie(Cookie original) {
 super(original.getName(), original.getValue());
 super.setMaxAge(original.getMaxAge());
 super.setPath(original.getPath());
 super.setComment(original.getComment());
 super.setSecure(original.getSecure());
 super.setValue(original.getValue());
 super.setVersion(original.getVersion());
 if (original.getDomain() != null) {
  super.setDomain(original.getDomain());
 }
}

代码示例来源:origin: pippo-java/pippo

/**
 * Adds a cookie to the response.
 *
 * @param name
 * @param value
 * @return the response
 */
public Response cookie(String name, String value) {
  Cookie cookie = new Cookie(name, value);
  cookie.setSecure(true);
  addCookie(cookie);
  return this;
}

代码示例来源:origin: apache/nifi

private void removeOidcRequestCookie(final HttpServletResponse httpServletResponse) {
  final Cookie cookie = new Cookie(OIDC_REQUEST_IDENTIFIER, null);
  cookie.setPath("/");
  cookie.setHttpOnly(true);
  cookie.setMaxAge(0);
  cookie.setSecure(true);
  httpServletResponse.addCookie(cookie);
}

代码示例来源:origin: pippo-java/pippo

/**
 * Generates a language cookie with a very long max age (ten years).
 *
 * @param language
 * @return The cookie
 */
private Cookie generateLanguageCookie(String language) {
  Cookie cookie = new Cookie(applicationCookiePrefix + "_LANG", language);
  cookie.setSecure(true);
  cookie.setMaxAge(TEN_YEARS);
  return cookie;
}

代码示例来源:origin: org.jboss.resteasy/skeleton-key-as7

protected void setCookie(String name, String value, String domain, String path, boolean secure)
{
 Cookie cookie = new Cookie(name, value);
 if (domain != null) cookie.setDomain(domain);
 if (path != null) cookie.setPath(path);
 if (secure) cookie.setSecure(true);
 response.addCookie(cookie);
}

代码示例来源:origin: org.sonatype.sisu.inject/guice-servlet

public ImmutableCookie(Cookie original) {
 super(original.getName(), original.getValue());
 super.setMaxAge(original.getMaxAge());
 super.setPath(original.getPath());
 super.setComment(original.getComment());
 super.setSecure(original.getSecure());
 super.setValue(original.getValue());
 super.setVersion(original.getVersion());
 if (original.getDomain() != null) {
  super.setDomain(original.getDomain());
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Add a cookie with the given value to the response,
 * using the cookie descriptor settings of this generator.
 * <p>Delegates to {@link #createCookie} for cookie creation.
 * @param response the HTTP response to add the cookie to
 * @param cookieValue the value of the cookie to add
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 * @see #setCookieMaxAge
 */
public void addCookie(HttpServletResponse response, String cookieValue) {
  Assert.notNull(response, "HttpServletResponse must not be null");
  Cookie cookie = createCookie(cookieValue);
  Integer maxAge = getCookieMaxAge();
  if (maxAge != null) {
    cookie.setMaxAge(maxAge);
  }
  if (isCookieSecure()) {
    cookie.setSecure(true);
  }
  if (isCookieHttpOnly()) {
    cookie.setHttpOnly(true);
  }
  response.addCookie(cookie);
  if (logger.isTraceEnabled()) {
    logger.trace("Added cookie [" + getCookieName() + "=" + cookieValue + "]");
  }
}

代码示例来源:origin: apache/hive

/**
 * Generate a server side cookie given the cookie value as the input.
 * @param str Input string token.
 * @return The generated cookie.
 */
private Cookie createCookie(String str) {
 if (LOG.isDebugEnabled()) {
  LOG.debug("Cookie name = " + AUTH_COOKIE + " value = " + str);
 }
 Cookie cookie = new Cookie(AUTH_COOKIE, str);
 cookie.setMaxAge(cookieMaxAge);
 if (cookieDomain != null) {
  cookie.setDomain(cookieDomain);
 }
 if (cookiePath != null) {
  cookie.setPath(cookiePath);
 }
 cookie.setSecure(isCookieSecure);
 return cookie;
}

代码示例来源:origin: nice-swa/my-site

/**
 * 设置cookie
 *
 * @param name
 * @param value
 * @param maxAge
 * @param response
 */
private void cookie(String name, String value, int maxAge, HttpServletResponse response) {
  Cookie cookie = new Cookie(name, value);
  cookie.setMaxAge(maxAge);
  cookie.setSecure(false);
  response.addCookie(cookie);
}

代码示例来源:origin: org.nuxeo.ecm.platform/nuxeo-platform-web-common

public static Cookie createCookie(HttpServletRequest request, String name, String value) {
    Cookie cookie = new Cookie(name, value);
    cookie.setPath(request.getContextPath());
    if (!Framework.isDevModeSet()) {
      cookie.setSecure(true);
    }
    return cookie;
  }
}

代码示例来源:origin: com.jwebmp.inject.extensions/guice-servlet

public ImmutableCookie(Cookie original) {
 super(original.getName(), original.getValue());
 super.setMaxAge(original.getMaxAge());
 super.setPath(original.getPath());
 super.setComment(original.getComment());
 super.setSecure(original.getSecure());
 super.setValue(original.getValue());
 super.setVersion(original.getVersion());
 if (original.getDomain() != null) {
  super.setDomain(original.getDomain());
 }
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Remove the cookie that this generator describes from the response.
 * Will generate a cookie with empty value and max age 0.
 * <p>Delegates to {@link #createCookie} for cookie creation.
 * @param response the HTTP response to remove the cookie from
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 */
public void removeCookie(HttpServletResponse response) {
  Assert.notNull(response, "HttpServletResponse must not be null");
  Cookie cookie = createCookie("");
  cookie.setMaxAge(0);
  if (isCookieSecure()) {
    cookie.setSecure(true);
  }
  if (isCookieHttpOnly()) {
    cookie.setHttpOnly(true);
  }
  response.addCookie(cookie);
  if (logger.isTraceEnabled()) {
    logger.trace("Removed cookie '" + getCookieName() + "'");
  }
}

代码示例来源:origin: SonarSource/sonarqube

public Cookie build() {
 Cookie cookie = new Cookie(requireNonNull(name), value);
 cookie.setPath(getContextPath(request));
 cookie.setSecure(isHttps(request));
 cookie.setHttpOnly(httpOnly);
 cookie.setMaxAge(expiry);
 return cookie;
}

代码示例来源:origin: pippo-java/pippo

/**
 * Removes the specified cookie by name.
 *
 * @param name
 * @return the response
 */
public Response removeCookie(String name) {
  Cookie cookie = new Cookie(name, "");
  cookie.setSecure(true);
  cookie.setMaxAge(0);
  addCookie(cookie);
  return this;
}

代码示例来源:origin: org.wso2.carbon.extension.identity.authenticator/org.wso2.carbon.extension.identity.sso.cas

public Cookie storeTicketGrantingCookie(String sessionId, IdentityRequest req) {
    Cookie ticketGrantingCookie = getTicketGrantingCookie(req);
    if (ticketGrantingCookie == null) {
      ticketGrantingCookie = new Cookie(SSOLoginProcessor.CAS_COOKIE_NAME, sessionId);
    }
    ticketGrantingCookie.setPath(CASConfiguration.getBasePath());
    ticketGrantingCookie.setSecure(true);
    return ticketGrantingCookie;
  }
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Add a cookie with the given value to the response,
 * using the cookie descriptor settings of this generator.
 * <p>Delegates to {@link #createCookie} for cookie creation.
 * @param response the HTTP response to add the cookie to
 * @param cookieValue the value of the cookie to add
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 * @see #setCookieMaxAge
 */
public void addCookie(HttpServletResponse response, String cookieValue) {
  Assert.notNull(response, "HttpServletResponse must not be null");
  Cookie cookie = createCookie(cookieValue);
  Integer maxAge = getCookieMaxAge();
  if (maxAge != null) {
    cookie.setMaxAge(maxAge);
  }
  if (isCookieSecure()) {
    cookie.setSecure(true);
  }
  if (isCookieHttpOnly()) {
    cookie.setHttpOnly(true);
  }
  response.addCookie(cookie);
  if (logger.isTraceEnabled()) {
    logger.trace("Added cookie [" + getCookieName() + "=" + cookieValue + "]");
  }
}

相关文章