java - 在servlet中添加cookie報(bào)錯(cuò)
問題描述
1.在添加cookie的時(shí)候報(bào)錯(cuò):
An invalid character [13] was present in the Cookie value
在網(wǎng)上查了一些報(bào)錯(cuò),大部分都是[32]、[44],據(jù)說是因?yàn)閏ookie里面添加了“,”或者空格導(dǎo)致的。
登陸處理的代碼是這樣的:
//登錄處理 @RequestMapping(value = '/login/validate', method = RequestMethod.POST) public void Validate(@RequestParam('username') String username, @RequestParam('password') String password, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {String md5 = MD5Util.stringToMD5(password);if (userService.verification(username, md5)) { User user = userService.selectByUsername(username); Long id = user.getId(); Long createDate = new Date().getTime(); String str = id + '=' + createDate; //加密 byte[] result = DESUtil.desCrypto(str, '12345678'); //把加密的字節(jié)數(shù)組轉(zhuǎn)換成16進(jìn)制// String results = TypeUtil.bytesToHexString(result); String results = Base64.encodeBase64String(result); Cookie cookie = new Cookie('token', results); cookie.setMaxAge(60 * 60 * 24 * 7);//7天 cookie.setPath('/'); System.out.println('新生成cookie和其MaxAge:' + cookie.getName() + '-->' + cookie.getMaxAge()); httpServletResponse.addCookie(cookie); HttpSession session = httpServletRequest.getSession(); session.setAttribute('user', user); for (Cookie c : httpServletRequest.getCookies()) {System.out.println('cookes添加到response后重新獲取cookies和其MaxAge:' + c.getName() + '-->' + c.getMaxAge()); } try {httpServletResponse.sendRedirect('/index.html');//httpServletRequest.getRequestDispatcher('/index.html').forward(httpServletRequest, httpServletResponse); } catch (Exception e) {e.printStackTrace(); }} else { try {httpServletResponse.sendRedirect('no.html'); } catch (IOException e) {e.printStackTrace(); }} }
報(bào)錯(cuò)的地方發(fā)生在addCookie這里。
問題解答
回答1:經(jīng)過嘗試,把原來的代碼2注釋,1 放開就可以了,哪位大佬可以解釋一下啊
1. String results = TypeUtil.bytesToHexString(result);2. //String results = Base64.encodeBase64String(result);
相關(guān)文章:
1. javascript - sublime快鍵鍵問題2. javascript - immutable配合react提升性能?3. css - 寫頁面遇到個(gè)布局問題,求大佬們幫解答,在線等,急!~4. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問5. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?6. 實(shí)現(xiàn)bing搜索工具urlAPI提交7. 配置Apache時(shí),添加對PHP的支持時(shí)語法錯(cuò)誤8. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化9. javascript - 移動端上不能實(shí)現(xiàn)拖拽布局嗎?10. phpstudy8.1支持win11系統(tǒng)嗎?

網(wǎng)公網(wǎng)安備