使用patchca后cookie失效的解决办法

Keywords:patchca,cookie失效

 

使用patchca生成验证码后,设置cookie值会失效,源代码如下:

String randomCode = EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
Cookie cookie = ValidateCodeCookieUtil.addValidateCode2Cookie(request, response, randomCode);

 

查看patchca源代码,发现是在EncoderHelper.getChallangeAndWriteImage(cs, "png", os);过程对

response outputstream进行了读写,因此无法再次写入cookie。

 

 

改成如下方式,问题解决:

Captcha captcha = cs.getCaptcha();
String randomCode = captcha.getChallenge();
Cookie cookie = ValidateCodeCookieUtil.addValidateCode2Cookie(request, response, randomCode);
response.addCookie(cookie);
           
BufferedImage image = captcha.getImage();
ImageIO.write(image, "png", os);
os.flush();           

你可能感兴趣的:(OS)