下面列出了java.net.HttpCookie#getName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public String resetCookies(List<String> cookieStrings) {
String result = null;
for (String cookieString : cookieStrings) {
List<HttpCookie> parse = HttpCookie.parse(cookieString);
for (HttpCookie httpCookie : parse) {
if(StringUtils.isNoneBlank(httpCookie.getValue())) {
String cookie = httpCookie.getName()+"="+httpCookie.getValue();
if(result==null) {
result = cookie;
} else {
result = result + " ; " + cookie;
}
}
}
}
return result;
}
private Cookie[] createCookies() {
List<String> strCookies = httpHeaders.get(HttpHeaders.COOKIE);
if (strCookies == null) {
return new Cookie[] {};
}
List<Cookie> result = new ArrayList<>();
for (String strCookie : strCookies) {
List<HttpCookie> httpCookies = HttpCookie.parse(strCookie);
for (HttpCookie httpCookie : httpCookies) {
Cookie cookie = new Cookie(httpCookie.getName(), httpCookie.getValue());
result.add(cookie);
}
}
return result.toArray(new Cookie[result.size()]);
}
/**
* Authentication with google account
* @param cookies cookies list keys from google auth
* @param login username associated to zds login
* @param id user id on ZdS associated to login
*/
public void authToGoogle(List<HttpCookie> cookies, String login, String id) {
if(login != null && id != null) {
this.login = login;
this.idUser = id;
log.info("L'identifiant de l'utilisateur " + this.login + " est : " + idUser);
cookieStore = new BasicCookieStore();
for(HttpCookie cookie:cookies) {
BasicClientCookie c = new BasicClientCookie(cookie.getName(), cookie.getValue());
c.setDomain(cookie.getDomain());
c.setPath(cookie.getPath());
c.setSecure(cookie.getSecure());
c.setVersion(cookie.getVersion());
c.setComment(cookie.getComment());
cookieStore.addCookie(c);
}
context.setCookieStore(cookieStore);
this.authenticated = true;
}
else {
log.debug("Le login de l'utilisateur n'a pas pu être trouvé");
}
}
/**
* Copy cookie from the proxy to the servlet client.
* Replaces cookie path to local path and renames cookie to avoid collisions.
*/
private 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 (int i = 0, l = cookies.size(); i < l; i++) {
HttpCookie cookie = cookies.get(i);
//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);
}
}
public static List<TransportableHttpCookie> from(URL url, List<HttpCookieWithId> list) {
List<TransportableHttpCookie> result = new ArrayList<>(list.size());
for (HttpCookieWithId hcwi : list) {
HttpCookie cookie = hcwi.httpCookie;
TransportableHttpCookie thc = new TransportableHttpCookie();
thc.name = cookie.getName();
thc.value = cookie.getValue();
thc.comment = cookie.getComment();
thc.commentURL = cookie.getCommentURL();
thc.discard = cookie.getDiscard();
thc.domain = cookie.getDomain();
thc.maxAge = cookie.getMaxAge();
thc.path = cookie.getPath();
thc.portList = cookie.getPortlist();
thc.secure = cookie.getSecure();
thc.version = cookie.getVersion();
thc.url = url.toString();
result.add(thc);
}
return result;
}
/**
* 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 = getServletContext().getServletContextName();
if (path == null) {
path = "";
}
path += servletRequest.getServletPath();
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);
}
}
/**
* Cookie building database entities.
*
* @param uri cookie corresponding uri.
* @param cookie cookie.
*/
public CookieEntity(URI uri, HttpCookie cookie) {
this.uri = uri == null ? null : uri.toString();
this.name = cookie.getName();
this.value = cookie.getValue();
this.comment = cookie.getComment();
this.commentURL = cookie.getCommentURL();
this.discard = cookie.getDiscard();
this.domain = cookie.getDomain();
long maxAge = cookie.getMaxAge();
if (maxAge != -1 && maxAge > 0) {
this.expiry = (maxAge * 1000L) + System.currentTimeMillis();
if (this.expiry < 0L) // 溢出
this.expiry = HeaderUtils.getMaxExpiryMillis();
} else
this.expiry = -1L;
this.path = cookie.getPath();
if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
this.path = path.substring(0, path.length() - 1);
}
this.portList = cookie.getPortlist();
this.secure = cookie.getSecure();
this.version = cookie.getVersion();
}
@Override
public boolean remove(URI uri, HttpCookie httpCookie) {
checkInitialization();
mLock.lock();
try {
if (httpCookie == null || !isEnable()) return true;
if (mCookieStoreListener != null) mCookieStoreListener.onRemoveCookie(uri, httpCookie);
Where where = new Where(CookieSQLHelper.NAME, Options.EQUAL, httpCookie.getName());
String domain = httpCookie.getDomain();
if (!TextUtils.isEmpty(domain)) where.and(CookieSQLHelper.DOMAIN, Options.EQUAL, domain);
String path = httpCookie.getPath();
if (!TextUtils.isEmpty(path)) {
if (path.length() > 1 && path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
where.and(CookieSQLHelper.PATH, Options.EQUAL, path);
}
return mCookieEntityDao.delete(where.toString());
} finally {
mLock.unlock();
}
}
public CookieAdapter(HttpCookie cookie) {
this.name = cookie.getName();
this.value = cookie.getValue();
this.domain = cookie.getDomain();
this.path = cookie.getPath();
this.secure = cookie.getSecure();
}
/**
* Compute the value of the "Set-Cookie" header to represent the given cookie,
* with an optional {@link SameSite} attribute and extension.
*
* @param cookie the cookie
* @param sameSite attribute for the cookie (given separately as {@link HttpCookie} does not currently
* support it
* @param extension cookie extension
* @return the value of the "Set-Cookie" header
*/
public static String setCookieHeaderValue(HttpCookie cookie,
@Nullable SameSite sameSite,
@Nullable Object extension) {
StringBuilder builder = new StringBuilder(cookie.getName());
builder.append("=\"").append(cookie.getValue()).append('"');
List<String> attributes = attributesForSetCookie(cookie, sameSite, extension);
for (String attribute : attributes) {
builder.append("; ").append(attribute);
}
return builder.toString();
}
@Override
public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response)
{
boolean authGood = false;
List<HttpCookie> cookies = request.getCookies();
for( HttpCookie cookie : cookies )
{
String name = cookie.getName();
if( name!= null && name.equals( "authToken" ))
{
String value = cookie.getValue();
try
{
if( value != null && AuthToken.validateToken(configProps, value))
{
authGood = true;
break;
}
}
catch( Exception e )
{
logger.error(" Exception validating authToken:", e );
authGood = false;
break;
}
}
else
{
continue;
}
}
return new KafkaMessageSenderSocket( configProps, authGood );
}
public String getCookieToken(URI uri, HttpCookie cookie) {
return cookie.getName() + cookie.getDomain();
}
protected String getCookieToken(URI uri, HttpCookie cookie)
{
return cookie.getName() + cookie.getDomain();
}
protected String getCookieToken(URI uri, HttpCookie cookie) {
return cookie.getName() + cookie.getDomain();
}
public UriKey(final URI uri, final HttpCookie cookie) {
super(cookie.getName());
this.host = forStorage(uri.getHost());
}
public DomainKey(final HttpCookie cookie) {
super(cookie.getName());
this.domain = cookie.getDomain();
this.path = cookie.getPath();
}
protected String getCookieToken(URI uri, HttpCookie cookie) {
return cookie.getName() + cookie.getDomain();
}