package connect_tx_sdk.utils;
import org.apache.log4j.Category;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
public class CookieUtils {
private static final Category log = Category.getInstance(CookieUtils.class);
// the key which represents the list of cookies to add for this request (in case of redirect)
public static final String COOKIES_TO_SEND = "atlassian.core.web.cookies.unsent";
// Character used to separate username and password in persistent cookies.
// 0x13 == "Device Control 3" non-printing ASCII char. Unlikely to appear in a username
private static final char DELIMITER = 0x13;
//"Tweakable" parameters for the cookie encoding. NOTE: changing these
//and recompiling this class will essentially invalidate old cookies.
private final static int ENCODE_XORMASK = 0x5A;
private final static char ENCODE_CHAR_OFFSET1 = 'C';
private final static char ENCODE_CHAR_OFFSET2 = 'i';
/**
* Invalidate the specified cookie and delete it from the response object.
*
* @param response The HttpServletResponse object, known as "response" in a JSP page.
* @param cookieName The name of the cookie you want to delete.
* @param path of the path the cookie you want to delete.
*/
public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String path)
{
log.debug("CookieUtils.invalidateCookie " + cookieName + " for path " + path);
setCookie(request, response, cookieName, null, 0, path);
}
/**
* Invalidate the specified cookie and delete it from the response object. Deletes only cookies mapped
* against the root "/" path. Otherwise use
* {@link #invalidateCookie(HttpServletRequest, HttpServletResponse, String, String)}
*
* @param response The HttpServletResponse object, known as "response" in a JSP page.
* @param cookieName The name of the cookie you want to delete.
* @see #invalidateCookie(HttpServletRequest, HttpServletResponse, String, String)
*/
public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName)
{
invalidateCookie(request, response, cookieName, "/");
}
/**
* Returns the specified Cookie object, or null if the cookie does not exist.
*
* @param request The HttpServletRequest object, known as "request" in a
* JSP page.
* @param name the name of the cookie.
* @return the Cookie object if it exists, otherwise null.
*/
public static Cookie getCookie(HttpServletRequest request, String name)
{
Cookie cookies[] = request.getCookies();
if (cookies == null || name == null || name.length() == 0)
{
return null;
}
//Otherwise, we have to do a linear scan for the cookie.
for (int i = 0; i < cookies.length; i++)
{
if (cookies[i].getName().equals(name))
{
return cookies[i];
}
}
return null;
}
/**
* Sets a cookie
*
* This will also put the cookie in a list of cookies to send with this request's response
* (so that in case of a redirect occurring down the chain, the first filter
* will always try to set this cookie again)
*/
public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int maxAge, String path)
{
log.debug("CookieUtils.setCookie " + name + ":" + value);
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
response.addCookie(cookie);
return cookie;
}
/**
* Returns the value of the specified cookie as a String. If the cookie
* does not exist, the method returns null.
*
* @param request the HttpServletRequest object, known as "request" in a
* JSP page.
* @param name the name of the cookie
* @return the value of the cookie, or null if the cookie does not exist.
*/
public static String getCookieValue(HttpServletRequest request, String name)
{
Cookie cookie = getCookie(request, name);
if (cookie != null)
{
return cookie.getValue();
}
return null;
}
/**
* Builds a cookie string containing a username and password.<p>
*
* Note: with open source this is not really secure, but it prevents users
* from snooping the cookie file of others and by changing the XOR mask and
* character offsets, you can easily tweak results.
*
* @param username The username.
* @param password The password.
* @return String encoding the input parameters, an empty string if one of
* the arguments equals <code>null</code>.
*/
public static String encodePasswordCookie(String username, String password)
{
return encodePasswordCookie(username, password, new String(new char[]{DELIMITER, ENCODE_CHAR_OFFSET1, ENCODE_CHAR_OFFSET2}));
}
/**
* Builds a cookie string containing a username and password, using offsets to customise the encoding.<p>
*
* Note: with open source this is not really secure, but it prevents users
* from snooping the cookie file of others and by changing the XOR mask and
* character offsets, you can easily tweak results.
*
* @param username The username.
* @param password The password.
* @param encoding A String used to customise cookie encoding (only the first 3 characters are used)
* @return String encoding the input parameters, an empty string if one of
* the arguments equals <code>null</code>.
*/
public static String encodePasswordCookie(String username, String password, String encoding)
{
StringBuffer buf = new StringBuffer();
if (username != null && password != null)
{
char offset1 = (encoding != null && encoding.length() > 1) ? encoding.charAt(1) : ENCODE_CHAR_OFFSET1;
char offset2 = (encoding != null && encoding.length() > 2) ? encoding.charAt(2) : ENCODE_CHAR_OFFSET2;
byte[] bytes = (username + DELIMITER + password).getBytes();
int b;
for (int n = 0; n < bytes.length; n++)
{
b = bytes[n] ^ (ENCODE_XORMASK + n);
buf.append((char) (offset1 + (b & 0x0F)));
buf.append((char) (offset2 + ((b >> 4) & 0x0F)));
}
}
return buf.toString();
}
/**
* Decodes a cookie string containing a username and password.
* @param cookieVal The cookie value.
* @return String[] containing the username at index 0 and the password at
* index 1, or <code>{ null, null }</code> if cookieVal equals
* <code>null</code> or the empty string.
*/
public static String[] decodePasswordCookie(String cookieVal)
{
return decodePasswordCookie(cookieVal, new String(new char[]{DELIMITER, ENCODE_CHAR_OFFSET1, ENCODE_CHAR_OFFSET2}));
}
/**
* Decodes a cookie string containing a username and password.
* @param cookieVal The cookie value.
* @param encoding A String used to customise cookie encoding (only the first 3 characters are used) - should be the same string you used to encode the cookie!
* @return String[] containing the username at index 0 and the password at
* index 1, or <code>{ null, null }</code> if cookie
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
qq_openid.zip (53个子文件)
qq
resources
connect.properties 486B
.svn
all-wcprops 212B
prop-base
props
tmp
prop-base
props
text-base
text-base
connect.properties.svn-base 486B
entries 385B
src
connect_tx_sdk
config
QQConfig.java 829B
service
TempToken.java 2KB
ShareToken.java 3KB
UserToken.java 3KB
AccessToken.java 2KB
utils
CookieUtils.java 9KB
SecretUtils.java 2KB
HttpClientUtils.java 5KB
ConnectUtils.java 5KB
log4j.properties 662B
log4j.production.properties 283B
connect.properties 116B
WebRoot
img
qq_login.png 3KB
.svn
all-wcprops 202B
prop-base
qq_login.png.svn-base 53B
props
tmp
prop-base
props
text-base
text-base
qq_login.png.svn-base 3KB
entries 390B
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 386B
classes
connect_tx_sdk
config
QQConfig.class 1KB
service
AccessToken.class 3KB
UserToken.class 3KB
TempToken.class 2KB
ShareToken.class 4KB
utils
CookieUtils.class 5KB
HttpClientUtils$1.class 1KB
HttpClientUtils.class 4KB
HttpClientUtils$GzipDecompressingEntity.class 1KB
ConnectUtils.class 7KB
SecretUtils.class 3KB
HttpClientUtils$2.class 2KB
log4j.properties 662B
log4j.production.properties 283B
connect.properties 116B
lib
httpcore-4.0-beta3.jar 168KB
commons-codec-1.3.jar 46KB
commons-collections-3.2.1.jar 562KB
commons-logging-1.1.1.jar 59KB
commons-beanutils-1.8.0.jar 226KB
commons-lang-2.4.jar 256KB
httpclient-4.0-beta2.jar 270KB
log4j-1.2.15.jar 383KB
commons-io-1.4.jar 106KB
js
connect.js 372B
.svn
all-wcprops 196B
prop-base
props
tmp
prop-base
props
text-base
text-base
connect.js.svn-base 372B
entries 377B
login.jsp 2KB
index.jsp 1KB
共 53 条
- 1
jifei3000
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页