2008-11-07 37 views
2

我的应用程序需要将用户的电子邮件地址存储在cookie中,以便我可以预先填充登录表单(username == email address)。我在JavaScript中设置了cookie值。如果我从JavaScript读取它,我会得到[email protected]。如果我在Firefox的cookie查看器中查看它,我会得到[email protected]从java中的cookie中读取电子邮件字符串

当我尝试在Java服务器端读取它时,我只收到foo

我需要在这里做某种编码/解码吗?如果是这样,我该如何做到这一点,可以通过JavaScript和Java解码?

在此先感谢! -Michael

回答

0

您需要转义cookie的值部分。

document.cookie = name + "=" +escape(value) 
    + ((expires) ? ";expires=" + expires_date.toGMTString() : "") 
    + ((path) ? ";path=" + path : "") 
    + ((domain) ? ";domain=" + domain : "") 
    + ((secure) ? ";secure" : ""); 
4

javax.servlet.http.Cookie DOCO为的setValue(字符串):

将新值分配给被创建该cookie 后的cookie。如果使用二进制值 ,则可能需要使用 BASE64编码。

随着版本0 饼干,值不应包含 空格,方括号,括号, 等号,逗号,双引号, 斜杠,问号,的迹象, 冒号和分号。在所有 浏览器中,空值 可能不会表现相同的方式。

我猜你需要Base64编码,它(通过JavaScript)的方式和出路(通过Java)

+0

请参阅下文后约等号标志...(关于评论的300个字符限制是什么?) – bowmanmc 2008-11-07 19:39:26

0

我发现两个解决办法。这是第一个。

将填充添加回Base64编码的字符串。灵感这个从http://fi.am/entry/urlsafe-base64-encodingdecoding-in-two-lines/

来到在这个解决方案中,JavaScript的保持不变(Base64编码,一切)和服务器端的样子:

public class CookieDecoder { 

private static final Log log = LogFactory.getLog(CookieDecoder.class); 

/** 
* @param cookieValue The value of the cookie to decode 
* @return Returns the decoded string 
*/ 
public String decode(String cookieValue) { 
    if (cookieValue == null || "".equals(cookieValue)) { 
     return null; 
    } 
    if (!cookieValue.endsWith("=")) { 
     cookieValue = padString(cookieValue); 
    } 
    if (log.isDebugEnabled()) { 
     log.debug("Decoding string: " + cookieValue); 
    } 
    Base64 base64 = new Base64(); 
    byte[] encodedBytes = cookieValue.getBytes(); 
    byte[] decodedBytes = base64.decode(encodedBytes); 
    String result = new String(decodedBytes); 
    if (log.isDebugEnabled()) { 
     log.debug("Decoded string to: " + result); 
    } 
    return result; 
} 

private String padString(String value) { 
    int mod = value.length() % 4; 
    if (mod <= 0) { 
     return value; 
    } 
    int numEqs = 4 - mod; 
    if (log.isDebugEnabled()) { 
     log.debug("Padding value with " + numEqs + " = signs"); 
    } 
    for (int i = 0; i < numEqs; i++) { 
     value += "="; 
    } 
    return value; 
} 
} 

在JavaScript端,你只需要确保你base64编码的值:

var encodedValue = this.base64.encode(value); 
document.cookie = name + "=" + encodedValue + 
        "; expires=" + this.expires.toGMTString() + 
        "; path=" + this.path; 
0

第二个解决方案就是URLEncode Base64编码的字符串。我在这里使用公共编解码器来编码。 Java代码:

public class CookieDecoder { 

    private static final Log log = LogFactory.getLog(CookieDecoder.class); 

    /** 
    * @param cookieValue The value of the cookie to decode 
    * @return Returns the decoded string 
    */ 
    public String decode(String cookieValue) { 
     if (cookieValue == null || "".equals(cookieValue)) { 
      return null; 
     } 
     if (log.isDebugEnabled()) { 
      log.debug("Decoding string: " + cookieValue); 
     } 
     URLCodec urlCodec = new URLCodec(); 
     String b64Str; 
     try { 
      b64Str = urlCodec.decode(cookieValue); 
     } 
     catch (DecoderException e) { 
      log.error("Error decoding string: " + cookieValue); 
      return null; 
     } 
     Base64 base64 = new Base64(); 
     byte[] encodedBytes = b64Str.getBytes(); 
     byte[] decodedBytes = base64.decode(encodedBytes); 
     String result = new String(decodedBytes); 
     if (log.isDebugEnabled()) { 
      log.debug("Decoded string to: " + result); 
     } 
     return result; 
    } 
} 

但现在我必须把它的JavaScript端以及解码... 编码:

var encodedValue = this.base64.encode(value); 
document.cookie = name + "=" + escape(encodedValue) + 
        "; expires=" + this.expires.toGMTString() + 
        "; path=" + this.path; 

解码:

var nameEQ = name + "="; 
var ca = document.cookie.split(';'); 
for(var i = 0; i < ca.length; i++) { 
    var c = ca[i]; 
    while (c.charAt(0)==' ') { 
     c = c.substring(1,c.length); 
    } 
    if (c.indexOf(nameEQ) == 0) { 
     var encodedValue = c.substring(nameEQ.length,c.length); 
     return this.base64.decode(unescape(encodedValue)); 
    } 
} 
return null; 
相关问题