Problems caused by cookies containing Chinese

On Friday, after the project test was completed and there was no problem, it went online. After going online, it was found that some accounts could not log in

The reason is that the cookie used to record and track users contains cookies. An exception occurred while reading and writing.

The exception looks like this:

java. lang.IllegalArgumentException: Control character in cookie value or attribute.@ H_ 301_ 7@ at org. apache. tomcat. util. http. CookieSupport. isHttpSeparator(CookieSupport.java:193)@H_ 301_ 7@ at org. apache. tomcat. util. http. CookieSupport. isHttpToken(CookieSupport.java:217)@H_ 301_ 7@ at org. apache. tomcat. util. http. ServerCookie. appendCookieValue(ServerCookie.java:186)@H_ 301_ 7@ at org. apache. catalina. connector.. Response. generateCookieString(Response.java:1032)@H_ 301_ 7@ at org. apache. catalina. connector.. Response. addCookie(Response.java:974)@H_ 301_ 7@ at org. apache. catalina. connector.. ResponseFacade. addCookie(ResponseFacade.java:381)@H_ 301_ 7@ at com. vcfilm. interceptor. service. AutologonService. setCookie(AutologonService.java:168)@H_ 301_ 7@ at com. vcfilm. interceptor. service. AutologonService. savelogonInfo(AutologonService.java:129)@H_ 301_ 7@ at com. vcfilm. interceptor. service. AutologonService. savelogonInfo(AutologonService.java:139)@H_ 301_ 7@ at com. vcfilm. wechat. actioncommon. BaseAction. SaveSession(BaseAction.java:45)@H_ 301_ 7@ at com. vcfilm. wechat. actioncommon. BaseAction. SetMember(BaseAction.java:191)@H_ 301_ 7@ at com. vcfilm. wechat. member. MemberAction. logincheck(MemberAction.java:364)@H_ 301_ 7@ at sun. reflect. NativeMethodAccessorImpl. invoke0(Native Method)@H_ 301_ 7@ at sun. reflect. NativeMethodAccessorImpl. invoke(NativeMethodAccessorImpl.java:57)@H_ 301_ 7@ at sun. reflect. DelegatingMethodAccessorImpl. invoke(DelegatingMethodAccessorImpl.java:43)@H_ 301_ 7@ at java. lang.reflect. Method. invoke(Method.java:601)

Chinese requires urlencoder encode. utf-8

Please refer to this article http://blog.csdn.net/newyear1988/article/details/7817066

After adding it, it is found that there is still a problem. When writing the cookie, it is encrypted with DES. It is found that when taking the cookie, getval() is used to decrypt the exception, so the value obtained by getval() is directly returned in the exception code block.

    /** * 从cookie中取值
     * */public String getCookieVal(HttpServletRequest request,String key){
        Cookie[] cookies= request.getCookies();if(null != cookies && cookies.length > 0){for(Cookie c:cookies){if(c.getName().equalsIgnoreCase(key)){null != c){try{return URLDecoder.decode(c.getValue(),"utf-8");
                        }catch(Exception e){
                            e.printStackTrace();return c.getValue();
                        }
                    }
                }
            }
        }return "";
    }     * 保存值到cookie
     * public void setCookie(String key,String val,int maxAge){{
            val = URLEncoder.encode(val,1)">);
        }(Exception e){
            e.printStackTrace();
        }
        Cookie cookie = new Cookie(key,val);if(maxAge > 0){
            cookie.setMaxAge(maxAge);
        }
        cookie.setPath("/");
        ServletActionContext.getResponse().addCookie(cookie);
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>