第一招:利用response向浏览器输出图片:
//获取验证码 在<img />标签内的src属性设为请求路径/verifyCode?goodsId=xxx&token=xxxxx;之后接口返回图片流即可
@RequestMapping(value="/verifyCode", method=RequestMethod.GET)
@ResponseBody
public Result<String> getMiaoshaVerifyCod(HttpServletResponse response, MiaoshaUser user,
@RequestParam("goodsId")long goodsId) {
......try {
BufferedImage image = createVerifyCode(user, goodsId);
OutputStream out = response.getOutputStream();
ImageIO.write(image, "JPEG", out);//向浏览器输出格式为jpeg的图片
//告诉浏览器不要缓存
//response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");response.setIntHeader("Expires",-1);
out.flush();
out.close();
return null;//没有什么影响
}catch(Exception e) {
e.printStackTrace();
return Result.error(CodeMsg.MIAOSHA_FAIL);
}
......
}
//创建验证码:
public BufferedImage createVerifyCode(MiaoshaUser user, long goodsId) {
/* if(user == null || goodsId <=0) {//身份校验
return null;
}*/
int width = 80;
int height = 32;
//create the image
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// set the background color
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
// draw the border
g.setColor(Color.black);
g.drawRect(0, 0, width - 1, height - 1);
// create a random instance to generate the codes
Random rdm = new Random();
// make some confusion
for (int i = 0; i < 50; i++) {
int x = rdm.nextInt(width);
int y = rdm.nextInt(height);
g.drawOval(x, y, 0, 0);
}
// 生成验证码表达式(比如2+3*2)之类的
String verifyCode = generateVerifyCode(rdm);
g.setColor(new Color(0, 100, 0));
g.setFont(new Font("Candara", Font.BOLD, 24));
g.drawString(verifyCode, 8, 24);
g.dispose(); //把验证码的正确结果存到redis中,用于比对下次用户请求中的携带的验证码,看是否一致
int rnd = calc(verifyCode);
redisService.set(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId, rnd);//每个用户对应的验证码不同,过期时间这里设置为了60s
//输出图片
return image;
}
//生成运算表达式
private String generateVerifyCode(Random rdm) {
//运算符数组
char[] ops = new char[]{'+','-','*'};
//三个个位数
int num1 = rdm.nextInt(10);
int num2 = rdm.nextInt(10);
int num3 = rdm.nextInt(10);
//两个运算符
char op1 = ops[rdm.nextInt(3)];
char op2 = ops[rdm.nextInt(3)];
//拼接表达式
String exp = ""+ num1 + op1 + num2 + op2 + num3;
return exp;
}
//根据给定表达式,计算结果,参考js的eval函数
private static int calc(String exp) {
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
return (Integer)engine.eval(exp);
}catch(Exception e) {
e.printStackTrace();
return 0;
}
}
最终显示:
第二招:利用response刷新页面:
response.setContentType("text/html;charset=utf-8");
response.setHeader("refresh","3;url=/xxxx.jsp");//设置停留三秒(停留页面显示内容由response.getWriter控制),然后跳转到/xxx.jsp
response.getWriter().write("登陆成功,3秒后自动跳转到首页,如果没有请点<a href=''>超链接</a>");