【问题标题】:Reading an email string from a cookie in java从java中的cookie读取电子邮件字符串
【发布时间】:2014-02-11 22:14:00
【问题描述】:

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

但是,当我尝试在 Java 的服务器端读取它时,我只得到foo

我需要在这里进行某种编码/解码吗?如果是这样,我该如何以一种可以被 JavaScript 和 Java 解码的方式来做到这一点?

提前致谢! -迈克尔

【问题讨论】:

    标签: java javascript email struts2 cookies


    【解决方案1】:

    来自javax.servlet.http.Cookie doco for setValue(String):

    在之后为 cookie 分配一个新值 cookie 已创建。如果你使用一个 二进制值,您可能想使用 BASE64 编码。

    使用版本 0 cookie,值不应包含 空格,括号,圆括号, 等号、逗号、双引号、 斜线、问号、at 符号、 冒号和分号。空值 可能并非所有人的行为方式都相同 浏览器。

    我猜你需要在输入(通过 JavaScript)和输出(通过 Java)的过程中对其进行 BASE64 编码

    【讨论】:

    • 请参阅下面关于等号的帖子...(cmets 的 300 字符限制是什么?)
    【解决方案2】:

    您需要转义 cookie 的值部分。

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

    【讨论】:

      【解决方案3】:

      我找到了两种解决方案。这是第一个。

      将填充添加回 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;
      

      【讨论】:

        【解决方案4】:

        第二种解决方案只是对 Base64 编码的字符串进行 URLEncode。我在这里使用公共编解码器进行编码。 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;
        

        【讨论】:

          猜你喜欢
          • 2023-03-31
          • 1970-01-01
          • 2011-08-20
          • 1970-01-01
          • 2017-10-10
          • 1970-01-01
          • 2013-07-14
          相关资源
          最近更新 更多