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

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

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

Cookie.getMaxAge介绍

[英]Gets the maximum age in seconds of this Cookie.

By default, -1 is returned, which indicates that the cookie will persist until browser shutdown.
[中]获取此Cookie的最大期限(以秒为单位)。
默认情况下,返回-1,这表示cookie将持续存在,直到浏览器关闭。

代码示例

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

/**
 * Assert a cookie's maxAge with a Hamcrest {@link Matcher}.
 */
public ResultMatcher maxAge(final String name, final Matcher<? super Integer> matcher) {
  return result -> {
    Cookie cookie = getCookie(result, name);
    assertThat("Response cookie '" + name + "' maxAge", cookie.getMaxAge(), matcher);
  };
}

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

/**
 * Assert a cookie's maxAge value.
 */
public ResultMatcher maxAge(final String name, final int maxAge) {
  return result -> {
    Cookie cookie = getCookie(result, name);
    assertEquals("Response cookie '" + name + "' maxAge", maxAge, cookie.getMaxAge());
  };
}

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

buf.append("; Domain=").append(cookie.getDomain());
int maxAge = cookie.getMaxAge();
if (maxAge >= 0) {
  buf.append("; Max-Age=").append(maxAge);

代码示例来源:origin: Dreampie/Resty

public boolean isPersistentCookie(String cookie) {
 Cookie c = getCookie(request.getCookies(), cookie);
 return c != null && c.getMaxAge() > 0;
}

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

private static com.gargoylesoftware.htmlunit.util.Cookie createCookie(javax.servlet.http.Cookie cookie) {
  Date expires = null;
  if (cookie.getMaxAge() > -1) {
    expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
  }
  BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
  result.setDomain(cookie.getDomain());
  result.setComment(cookie.getComment());
  result.setExpiryDate(expires);
  result.setPath(cookie.getPath());
  result.setSecure(cookie.getSecure());
  if (cookie.isHttpOnly()) {
    result.setAttribute("httponly", "true");
  }
  return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}

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

/**
 * Generate httponly cookie from HS2 cookie
 * @param cookie HS2 generated cookie
 * @return The httponly cookie
 */
private static String getHttpOnlyCookieHeader(Cookie cookie) {
 NewCookie newCookie = new NewCookie(cookie.getName(), cookie.getValue(),
  cookie.getPath(), cookie.getDomain(), cookie.getVersion(),
  cookie.getComment(), cookie.getMaxAge(), cookie.getSecure());
 return newCookie + "; HttpOnly";
}

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

private void verifyCookie(Cookie cookie, @Nullable String value, int expiry) {
 assertThat(cookie.getPath()).isEqualTo("/");
 assertThat(cookie.isHttpOnly()).isTrue();
 assertThat(cookie.getMaxAge()).isEqualTo(expiry);
 assertThat(cookie.getSecure()).isFalse();
 assertThat(cookie.getValue()).isEqualTo(value);
}

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

@Test
 public void delete() {
  when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});

  underTest.delete(request, response);

  verify(response).addCookie(cookieArgumentCaptor.capture());
  Cookie updatedCookie = cookieArgumentCaptor.getValue();
  assertThat(updatedCookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
  assertThat(updatedCookie.getValue()).isNull();
  assertThat(updatedCookie.getPath()).isEqualTo("/");
  assertThat(updatedCookie.getMaxAge()).isEqualTo(0);
 }
}

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

private void verifyCookie(Cookie cookie) {
 assertThat(cookie.getName()).isEqualTo("XSRF-TOKEN");
 assertThat(cookie.getValue()).isNotEmpty();
 assertThat(cookie.getPath()).isEqualTo("/");
 assertThat(cookie.isHttpOnly()).isFalse();
 assertThat(cookie.getMaxAge()).isEqualTo(TIMEOUT);
 assertThat(cookie.getSecure()).isFalse();
}

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

buf.append("; Domain=").append(cookie.getDomain());
int maxAge = cookie.getMaxAge();
if (maxAge >= 0) {
  buf.append("; Max-Age=").append(maxAge);

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

private void verifyCookie(Cookie cookie) {
  assertThat(cookie.getName()).isEqualTo("OAUTHSTATE");
  assertThat(cookie.getValue()).isNotEmpty();
  assertThat(cookie.getPath()).isEqualTo("/");
  assertThat(cookie.isHttpOnly()).isTrue();
  assertThat(cookie.getMaxAge()).isEqualTo(-1);
  assertThat(cookie.getSecure()).isFalse();
 }
}

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

@Test
public void verify_state_using_default_state_parameter() {
 String state = "state";
 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie("OAUTHSTATE", sha256Hex(state))});
 when(request.getParameter("state")).thenReturn(state);
 underTest.verifyState(request, response, identityProvider);
 verify(response).addCookie(cookieArgumentCaptor.capture());
 Cookie updatedCookie = cookieArgumentCaptor.getValue();
 assertThat(updatedCookie.getName()).isEqualTo("OAUTHSTATE");
 assertThat(updatedCookie.getValue()).isNull();
 assertThat(updatedCookie.getPath()).isEqualTo("/");
 assertThat(updatedCookie.getMaxAge()).isEqualTo(0);
}

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

@Test
public void verify_state() {
 String state = "state";
 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie("OAUTHSTATE", sha256Hex(state))});
 when(request.getParameter("aStateParameter")).thenReturn(state);
 underTest.verifyState(request, response, identityProvider, "aStateParameter");
 verify(response).addCookie(cookieArgumentCaptor.capture());
 Cookie updatedCookie = cookieArgumentCaptor.getValue();
 assertThat(updatedCookie.getName()).isEqualTo("OAUTHSTATE");
 assertThat(updatedCookie.getValue()).isNull();
 assertThat(updatedCookie.getPath()).isEqualTo("/");
 assertThat(updatedCookie.getMaxAge()).isEqualTo(0);
}

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

@Test
public void remove_state() {
 underTest.removeState(request, response);
 verify(response).addCookie(cookieArgumentCaptor.capture());
 Cookie cookie = cookieArgumentCaptor.getValue();
 assertThat(cookie.getValue()).isNull();
 assertThat(cookie.getMaxAge()).isEqualTo(0);
}

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

@Test
public void init_create_cookie() {
 when(request.getParameter("return_to")).thenReturn("/settings");
 underTest.init(request, response);
 verify(response).addCookie(cookieArgumentCaptor.capture());
 Cookie cookie = cookieArgumentCaptor.getValue();
 assertThat(cookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
 assertThat(cookie.getValue()).isNotEmpty();
 assertThat(cookie.getPath()).isEqualTo("/");
 assertThat(cookie.isHttpOnly()).isTrue();
 assertThat(cookie.getMaxAge()).isEqualTo(300);
 assertThat(cookie.getSecure()).isFalse();
}

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

@Test
public void create_cookie_when_web_context() {
 when(request.getContextPath()).thenReturn("/sonarqube");
 Cookie cookie = newCookieBuilder(request).setName("name").setValue("value").setHttpOnly(true).setExpiry(10).build();
 assertThat(cookie.getName()).isEqualTo("name");
 assertThat(cookie.getValue()).isEqualTo("value");
 assertThat(cookie.isHttpOnly()).isTrue();
 assertThat(cookie.getMaxAge()).isEqualTo(10);
 assertThat(cookie.getSecure()).isFalse();
 assertThat(cookie.getPath()).isEqualTo("/sonarqube");
}

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

@Test
public void create_cookie() {
 Cookie cookie = newCookieBuilder(request).setName("name").setValue("value").setHttpOnly(true).setExpiry(10).build();
 assertThat(cookie.getName()).isEqualTo("name");
 assertThat(cookie.getValue()).isEqualTo("value");
 assertThat(cookie.isHttpOnly()).isTrue();
 assertThat(cookie.getMaxAge()).isEqualTo(10);
 assertThat(cookie.getSecure()).isFalse();
 assertThat(cookie.getPath()).isEqualTo("/");
}

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

/**
 * Print the supplied cookies in a human-readable form, assuming the
 * {@link Cookie} implementation does not provide its own {@code toString()}.
 * @since 4.2
 */
private void printCookies(Cookie[] cookies) {
  String[] cookieStrings = new String[cookies.length];
  for (int i = 0; i < cookies.length; i++) {
    Cookie cookie = cookies[i];
    cookieStrings[i] = new ToStringCreator(cookie)
      .append("name", cookie.getName())
      .append("value", cookie.getValue())
      .append("comment", cookie.getComment())
      .append("domain", cookie.getDomain())
      .append("maxAge", cookie.getMaxAge())
      .append("path", cookie.getPath())
      .append("secure", cookie.getSecure())
      .append("version", cookie.getVersion())
      .append("httpOnly", cookie.isHttpOnly())
      .toString();
  }
  this.printer.printValue("Cookies", cookieStrings);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void getCookie_returnsCookieMaxAgeEqualToSessionTimeout() throws Exception {
  Cookie cookie = factory.getCookie(uaaPrincipal);
  assertEquals(sessionTimeout, cookie.getMaxAge());
}

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

@Test
public void testCustomCookie() {
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  CookieLocaleResolver resolver = new CookieLocaleResolver();
  resolver.setCookieName("LanguageKoek");
  resolver.setCookieDomain(".springframework.org");
  resolver.setCookiePath("/mypath");
  resolver.setCookieMaxAge(10000);
  resolver.setCookieSecure(true);
  resolver.setLocale(request, response, new Locale("nl", ""));
  Cookie cookie = response.getCookie("LanguageKoek");
  assertNotNull(cookie);
  assertEquals("LanguageKoek", cookie.getName());
  assertEquals(".springframework.org", cookie.getDomain());
  assertEquals("/mypath", cookie.getPath());
  assertEquals(10000, cookie.getMaxAge());
  assertTrue(cookie.getSecure());
  request = new MockHttpServletRequest();
  request.setCookies(cookie);
  resolver = new CookieLocaleResolver();
  resolver.setCookieName("LanguageKoek");
  Locale loc = resolver.resolveLocale(request);
  assertEquals("nl", loc.getLanguage());
}

相关文章