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

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

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

Cookie.getPath介绍

[英]Returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server.
[中]返回浏览器将此cookie返回到的服务器上的路径。服务器上的所有子路径都可以看到cookie。

代码示例

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

private String getCookieHeader(Cookie cookie) {
  StringBuilder buf = new StringBuilder();
  buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
  if (StringUtils.hasText(cookie.getPath())) {
    buf.append("; Path=").append(cookie.getPath());

代码示例来源: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: spring-projects/spring-framework

@Test
public void testSetAndResolveLocale() {
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpServletResponse response = new MockHttpServletResponse();
  CookieLocaleResolver resolver = new CookieLocaleResolver();
  resolver.setLocale(request, response, new Locale("nl", ""));
  Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
  assertNotNull(cookie);
  assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, cookie.getName());
  assertEquals(null, cookie.getDomain());
  assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_PATH, cookie.getPath());
  assertFalse(cookie.getSecure());
  request = new MockHttpServletRequest();
  request.setCookies(cookie);
  resolver = new CookieLocaleResolver();
  Locale loc = resolver.resolveLocale(request);
  assertEquals("nl", loc.getLanguage());
}

代码示例来源:origin: jooby-project/jooby

@Override
public List<Cookie> cookies() {
 javax.servlet.http.Cookie[] cookies = req.getCookies();
 if (cookies == null) {
  return ImmutableList.of();
 }
 return Arrays.stream(cookies)
   .map(c -> {
    Cookie.Definition cookie = new Cookie.Definition(c.getName(), c.getValue());
    Optional.ofNullable(c.getComment()).ifPresent(cookie::comment);
    Optional.ofNullable(c.getDomain()).ifPresent(cookie::domain);
    Optional.ofNullable(c.getPath()).ifPresent(cookie::path);
    return cookie.toCookie();
   })
   .collect(Collectors.toList());
}

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

String cookieValue = accountOptionCookie.getValue();
assertEquals(true, accountOptionCookie.isHttpOnly());
assertEquals(365*24*60*60, accountOptionCookie.getMaxAge());
assertEquals("/login", accountOptionCookie.getPath());
Assert.assertEquals(secure, accountOptionCookie.getSecure());
assertThat(currentUserCookie.getValue(), containsString("user-id"));

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

@Test
public void getNullCookie() {
  Cookie cookie = factory.getNullCookie();
  assertEquals("Current-User", cookie.getName());
  assertFalse(cookie.isHttpOnly());
  assertEquals(0, cookie.getMaxAge());
  assertEquals("/", cookie.getPath());
}

代码示例来源: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: cloudfoundry/uaa

@Test
public void testSave_and_Load_Token() throws Exception {
  for (String contextPath : Arrays.asList("", "/uaa")) {
    String expectedCookiePath = contextPath + "/";
    CookieBasedCsrfTokenRepository repo = new CookieBasedCsrfTokenRepository();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setPathInfo("/login/somepath");
    request.setContextPath(contextPath);
    CsrfToken token = repo.generateToken(request);
    assertTrue("The token is at least 22 characters long.", token.getToken().length() >= 22);
    repo.saveToken(token, request, response);
    Cookie cookie = response.getCookie(token.getParameterName());
    assertNotNull(cookie);
    assertEquals(token.getToken(), cookie.getValue());
    assertEquals(true, cookie.isHttpOnly());
    assertEquals(repo.getCookieMaxAge(), cookie.getMaxAge());
    assertNotNull(cookie.getPath());
    assertEquals(expectedCookiePath, cookie.getPath());
    request.setCookies(cookie);
    CsrfToken saved = repo.loadToken(request);
    assertEquals(token.getToken(), saved.getToken());
    assertEquals(token.getHeaderName(), saved.getHeaderName());
    assertEquals(token.getParameterName(), saved.getParameterName());
  }
}

代码示例来源: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());
}

代码示例来源: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: webx/citrus

/** 复制一个cookie,修改cookie的名称。 */
public CookieSupport(Cookie cookie, String name) {
  super(assertNotNull(getCookieName(cookie, name), "cookieName"), cookie.getValue());
  setVersion(cookie.getVersion());
  setMaxAge(cookie.getMaxAge());
  setSecure(cookie.getSecure());
  String comment = cookie.getComment();
  if (!isEmpty(comment)) {
    setComment(comment);
  }
  String domain = cookie.getDomain();
  if (!isEmpty(domain)) {
    setDomain(domain);
  }
  String path = cookie.getPath();
  if (!isEmpty(path)) {
    setPath(path);
  }
  if (cookie instanceof CookieSupport) {
    setHttpOnly(((CookieSupport) cookie).getHttpOnly());
  } else if (getHttpOnlyMethod != null) {
    try {
      setHttpOnly((Boolean) getHttpOnlyMethod.invoke(cookie, EMPTY_OBJECT_ARRAY));
    } catch (InvocationTargetException e) {
      log.warn("Invocation of Cookie.isHttpOnly() failed", e.getTargetException());
    }
  }
}

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

private void addCookie(Cookie cookie) {
  checkCommitted();
  if (StringUtils.isNullOrEmpty(cookie.getPath())) {
    cookie.setPath(StringUtils.addStart(contextPath, "/"));
  }
  getCookieMap().put(cookie.getName(), cookie);
}

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

private String getCookieHeader(Cookie cookie) {
  StringBuilder buf = new StringBuilder();
  buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
  if (StringUtils.hasText(cookie.getPath())) {
    buf.append("; Path=").append(cookie.getPath());

代码示例来源:origin: webx/citrus

/** 复制一个cookie,修改cookie的名称。 */
public CookieSupport(Cookie cookie, String name) {
  super(assertNotNull(getCookieName(cookie, name), "cookieName"), cookie.getValue());
  setVersion(cookie.getVersion());
  setMaxAge(cookie.getMaxAge());
  setSecure(cookie.getSecure());
  String comment = cookie.getComment();
  if (!isEmpty(comment)) {
    setComment(comment);
  }
  String domain = cookie.getDomain();
  if (!isEmpty(domain)) {
    setDomain(domain);
  }
  String path = cookie.getPath();
  if (!isEmpty(path)) {
    setPath(path);
  }
  if (cookie instanceof CookieSupport) {
    setHttpOnly(((CookieSupport) cookie).getHttpOnly());
  } else if (getHttpOnlyMethod != null) {
    try {
      setHttpOnly((Boolean) getHttpOnlyMethod.invoke(cookie, EMPTY_OBJECT_ARRAY));
    } catch (InvocationTargetException e) {
      log.warn("Invocation of Cookie.isHttpOnly() failed", e.getTargetException());
    }
  }
}

代码示例来源:origin: org.apache.wicket/wicket-core

protected Key(Cookie cookie)
{
  Args.notNull(cookie, "cookie");
  name = cookie.getName();
  path = cookie.getPath();
  domain = cookie.getDomain();
}

代码示例来源: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: com.atlassian.jira/jira-core

private static String formatCookieValue(Cookie cookie)
{
  return new StringBuilder().append(cookie.getValue())
      .append(" path:").append(cookie.getPath())
      .append(" domain:").append(cookie.getDomain())
      .append(" version:").append(cookie.getVersion())
      .append(" maxAge:").append(cookie.getMaxAge())
      .toString();
}

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

protected Key(Cookie cookie)
{
  Args.notNull(cookie, "cookie");
  name = cookie.getName();
  path = cookie.getPath();
  domain = cookie.getDomain();
}

代码示例来源: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: org.jasig.portal/uPortal-web

@Override
public void updateFromCookie(Cookie cookie) {
  this.setComment(cookie.getComment());
  this.setDomain(cookie.getDomain());
  this.setExpires(DateTime.now().plusSeconds(cookie.getMaxAge()));
  this.setPath(cookie.getPath());
  this.setSecure(cookie.getSecure());
  this.setValue(cookie.getValue());
}
/** @return the comment */

相关文章