当前位置: 首页>>代码示例>>Java>>正文


Java Cookie类代码示例

本文整理汇总了Java中javax.servlet.http.Cookie的典型用法代码示例。如果您正苦于以下问题:Java Cookie类的具体用法?Java Cookie怎么用?Java Cookie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Cookie类属于javax.servlet.http包,在下文中一共展示了Cookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doFilterInternal

import javax.servlet.http.Cookie; //导入依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    // Spring put the CSRF token in session attribute "_csrf"
    CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf");

    // Send the cookie only if the token has changed
    String actualToken = request.getHeader("X-CSRF-TOKEN");
    if (actualToken == null || !actualToken.equals(csrfToken.getToken())) {
        // Session cookie that will be used by AngularJS
        String pCookieName = "CSRF-TOKEN";
        Cookie cookie = new Cookie(pCookieName, csrfToken.getToken());
        cookie.setMaxAge(-1);
        cookie.setHttpOnly(false);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
    filterChain.doFilter(request, response);
}
 
开发者ID:GastonMauroDiaz,项目名称:buenojo,代码行数:19,代码来源:CsrfCookieGeneratorFilter.java

示例2: getCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
public Map<String,Cookie> getCookie() {
    if (this.cookie == null) {
        this.cookie = new ScopeMap<Cookie>() {
            @Override
            protected Enumeration<String> getAttributeNames() {
                Cookie[] c = ((HttpServletRequest) page.getRequest())
                        .getCookies();
                if (c != null) {
                    Vector<String> v = new Vector<String>();
                    for (int i = 0; i < c.length; i++) {
                        v.add(c[i].getName());
                    }
                    return v.elements();
                }
                return null;
            }

            @Override
            protected Cookie getAttribute(String name) {
                Cookie[] c = ((HttpServletRequest) page.getRequest())
                        .getCookies();
                if (c != null) {
                    for (int i = 0; i < c.length; i++) {
                        if (name.equals(c[i].getName())) {
                            return c[i];
                        }
                    }
                }
                return null;
            }

        };
    }
    return this.cookie;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:36,代码来源:ImplicitObjectELResolver.java

示例3: deleteCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
        Cookie cookie, String domain, String path) {
    if (cookie != null) {
        if(StringUtils.isNotBlank(domain)){
            cookie.setDomain(domain);
        }
        cookie.setPath(path);
        cookie.setValue("");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:13,代码来源:CookieUtils.java

示例4: getCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
/**
 * 从 Cookie 中获取数据
 */
public static String getCookie(HttpServletRequest request, String name) {
    String value = "";
    try {
        Cookie[] cookieArray = request.getCookies();
        if (cookieArray != null) {
            for (Cookie cookie : cookieArray) {
                if (StringUtil.isNotEmpty(name) && name.equals(cookie.getName())) {
                    value = CodecUtil.decodeURL(cookie.getValue());
                    break;
                }
            }
        }
    } catch (Exception e) {
        logger.error("获取 Cookie 出错!");
        throw new RuntimeException(e);
    }
    return value;
}
 
开发者ID:smxc,项目名称:garlicts,代码行数:22,代码来源:WebUtil.java

示例5: getGidCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
private static String getGidCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        Cookie[] var2 = cookies;
        int var3 = cookies.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            Cookie cookie = var2[var4];
            if (WXZID_COOKIE_NAME.equals(cookie.getName()) && cookie.getValue() != null) {
                try {
                    return UrlUtils.decode(cookie.getValue());
                } catch (Exception var7) {
                    LOGGER.error("wxzid [" + cookie.getValue() + "] deserialize failed,", var7);
                    return null;
                }
            }
        }
    }
    return null;
}
 
开发者ID:wxz1211,项目名称:dooo,代码行数:21,代码来源:UserAuth.java

示例6: getToken

import javax.servlet.http.Cookie; //导入依赖的package包/类
public String getToken( HttpServletRequest request ) {
    /**
     *  Getting the token from Cookie store
     */
    Cookie authCookie = getCookieValueByName( request, AUTH_COOKIE );
    if ( authCookie != null ) {
        return authCookie.getValue();
    }
    /**
     *  Getting the token from Authentication header
     *  e.g Bearer your_token
     */
    String authHeader = request.getHeader(AUTH_HEADER);
    if ( authHeader != null && authHeader.startsWith("Bearer ")) {
        return authHeader.substring(7);
    }

    return null;
}
 
开发者ID:bfwg,项目名称:angular-spring-starter,代码行数:20,代码来源:TokenHelper.java

示例7: containsKey

import javax.servlet.http.Cookie; //导入依赖的package包/类
@Override
public boolean containsKey(final Object key)
{
  final Cookie[] cookies = _httpServletRequest.getCookies();
  if (cookies == null)
  {
    return false;
  }
  for (Cookie element : cookies)
  {
    if (element.getName().equals(key))
    {
      return true;
    }
  }

  return false;
}
 
开发者ID:apache,项目名称:myfaces-trinidad,代码行数:19,代码来源:ServletCookieMap.java

示例8: testGetValueCookiesDate

import javax.servlet.http.Cookie; //导入依赖的package包/类
@Test
public void testGetValueCookiesDate() throws Exception {
  Date date = new Date();
  String strDate = ISO8601Utils.format(date);
  Cookie[] cookies = new Cookie[] {new Cookie("c1", strDate)};
  new Expectations() {
    {
      request.getCookies();
      result = cookies;
    }
  };

  CookieProcessor processor = createProcessor("c1", Date.class);
  Object value = processor.getValue(request);
  Assert.assertEquals(strDate, ISO8601Utils.format((Date) value));
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:17,代码来源:TestCookieProcessor.java

示例9: getCookies

import javax.servlet.http.Cookie; //导入依赖的package包/类
@Override
public Cookie[] getCookies() {

    if (request == null) {
        throw new IllegalStateException(
                        sm.getString("requestFacade.nullRequest"));
    }

    Cookie[] ret = null;

    /*
     * Clone the returned array only if there is a security manager
     * in place, so that performance won't suffer in the non-secure case
     */
    if (SecurityUtil.isPackageProtectionEnabled()){
        ret = AccessController.doPrivileged(
            new GetCookiesPrivilegedAction());
        if (ret != null) {
            ret = ret.clone();
        }
    } else {
        ret = request.getCookies();
    }

    return ret;
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:27,代码来源:RequestFacade.java

示例10: copyProxyCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
/**
 * Copy cookie from the proxy to the servlet client. Replaces cookie path to local path and renames cookie to avoid
 * collisions.
 */
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 = 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);
    }
}
 
开发者ID:bluecreator,项目名称:http-agent,代码行数:23,代码来源:AgentServlet.java

示例11: getRememberedDisplayGroupID

import javax.servlet.http.Cookie; //导入依赖的package包/类
/**
   */
  private int getRememberedDisplayGroupID(final HttpServletRequest request) {
    // try to get the display group from session
    final Integer displayGroupID = (Integer) request.getSession().getAttribute(WebUIConstants.SESSION_ATTR_SELECTED_DISPLAY_GROUP_ID);
    if (displayGroupID != null) {
      return displayGroupID.intValue();
    }

    // try to get the display group from the cookie
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
      for (int i = 0; i < cookies.length; i++) {
        final Cookie c = cookies[i];
//        if (log.isDebugEnabled()) log.debug("c.getName(): " + c.getName());
//        if (log.isDebugEnabled()) log.debug("c.getValue(): " + c.getValue());
        if (c.getName().equals(WebUIConstants.COOKIE_DISPLAY_GROUP_ID)) {
          if (StringUtils.isValidInteger(c.getValue())) {
            return getValidDisplayGroupID(Integer.parseInt(c.getValue()));
          } else {
            return DisplayGroup.DISPLAY_GROUP_ID_ALL;
          }
        }
      }
    }

    if (displayGroupID != null) {
      return getValidDisplayGroupID(displayGroupID.intValue());
    }
    return DisplayGroup.DISPLAY_GROUP_ID_ALL;
  }
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:32,代码来源:BuildsStatusesPage.java

示例12: verifyTgtToSetRemovingOldTgt

import javax.servlet.http.Cookie; //导入依赖的package包/类
@Test
public void verifyTgtToSetRemovingOldTgt() throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockHttpServletRequest request = new MockHttpServletRequest();

    final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class);
    when(tgt.getId()).thenReturn("test");

    request.setCookies(new Cookie("TGT", "test5"));
    WebUtils.putTicketGrantingTicketInScopes(this.context, tgt);
    this.context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));

    assertEquals("success", this.action.execute(this.context).getId());
    request.setCookies(response.getCookies());
    assertEquals(tgt.getId(), this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request));
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:17,代码来源:SendTicketGrantingTicketActionTests.java

示例13: addCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
/**
 * Add the specified Cookie to those that will be included with
 * this Response.
 *
 * @param cookie Cookie to be added
 */
@Override
public void addCookie(final Cookie cookie) {

    // Ignore any call from an included servlet
    if (included || isCommitted()) {
        return;
    }

    final StringBuffer sb = generateCookieString(cookie);
    //if we reached here, no exception, cookie is valid
    // the header name is Set-Cookie for both "old" and v.1 ( RFC2109 )
    // RFC2965 is not supported by browsers and the Servlet spec
    // asks for 2109.
    addHeader("Set-Cookie", sb.toString());
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:22,代码来源:Response.java

示例14: getPlayerIdFromCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
/**
 * Reads the player ID from the cookie in the HTTP request.
 *
 * @param request  The HTTP request.
 * @param username The name of the current user.
 * @return The player ID embedded in the cookie, or <code>null</code> if cookie is not present.
 */
private String getPlayerIdFromCookie(HttpServletRequest request, String username) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }
    String cookieName = COOKIE_NAME + "-" + StringUtil.utf8HexEncode(username);
    for (Cookie cookie : cookies) {
        if (cookieName.equals(cookie.getName())) {
            return StringUtils.trimToNull(cookie.getValue());
        }
    }
    return null;
}
 
开发者ID:airsonic,项目名称:airsonic,代码行数:21,代码来源:PlayerService.java

示例15: addCookie

import javax.servlet.http.Cookie; //导入依赖的package包/类
/**添加cookie*/
public List<Cookie> addCookie(User user) {
	Cookie cookieU = new Cookie(Constants.COOKIE_USERNAME, user.getUsername());
	Cookie cookieP = new Cookie(Constants.COOKIE_PASSWORD, user.getPassword());
	cookieU.setMaxAge(60 * 60 * 24 * 14);
	cookieP.setMaxAge(60 * 60 * 24 * 14);
	cookieU.setPath("/");
	cookieP.setPath("/");
	List<Cookie> list = new ArrayList<Cookie>();
	list.add(cookieP);
	list.add(cookieU);
	return list;
}
 
开发者ID:zhangjikai,项目名称:sdudoc,代码行数:14,代码来源:CookieUtil.java


注:本文中的javax.servlet.http.Cookie类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。