问题描述:在响应中添加cookie的过程中出现以下异常
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:182) org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:115) org.apache.catalina.connector.Response.generateCookieString(Response.java:1010) org.apache.catalina.connector.Response.addCookie(Response.java:962) org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:385) org.apache.jsp.deal_jsp._jspService(deal_jsp.java:126) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
代码如下所示:
Cookie cookie1 = new Cookie("username", m.replaceAll("").trim());cookie1.setPath("/");cookie1.setMaxAge(60 * 60 * 3);resp.addCookie(cookie1);req.getRequestDispatcher("/loginsuccess.jsp").forward(req, resp);
由于我是在尝试sql注入,所以输入的内容包含特殊字符,我以为是特殊字符惹的祸,然后我就用正则去除了特殊字符,但是依然报这个错误,在网上查找资料的过程中看到说是空格的影响,所以去除了空格。
Cookie cookie1 = new Cookie("username", m.replaceAll("").replace(" ",""));cookie1.setPath("/");cookie1.setMaxAge(60 * 60 * 3);resp.addCookie(cookie1);req.getRequestDispatcher("/loginsuccess.jsp").forward(req, resp);
然后,就没有报错啦~
参考链接: