图形验证码 隐藏秒杀地址

@RequestMapping(value="/verifyCode", method=RequestMethod.GET)
@ResponseBody
public Result<String> getMiaoshaVerifyCod(HttpServletResponse response,MiaoshaUser user,
      @RequestParam("goodsId")long goodsId) {
   if(user == null) {
      return Result.error(CodeMsg.SESSION_ERROR);
   }
   try {
      BufferedImage image  = miaoshaService.createVerifyCode(user, goodsId);
      OutputStream out = response.getOutputStream();
      ImageIO.write(image, "JPEG", out);
      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);
   }
   // generate a random code
   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);
   //输出图片 
   return image;
}
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;
   }
}

private static char[] ops = new char[] {'+', '-', '*'};
/**
 * + - * 
 * */
private String generateVerifyCode(Random rdm) {
   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;
}
@RequestMapping(value="/path", method=RequestMethod.GET)
@ResponseBody
public Result<String> getMiaoshaPath(HttpServletRequest request, MiaoshaUser user,
      @RequestParam("goodsId")long goodsId,
      @RequestParam(value="verifyCode", defaultValue="0")int verifyCode
      ) {
   if(user == null) {
      return Result.error(CodeMsg.SESSION_ERROR);
   }
   boolean check = miaoshaService.checkVerifyCode(user, goodsId, verifyCode);
   if(!check) {
      return Result.error(CodeMsg.REQUEST_ILLEGAL);
   }
   String path  =miaoshaService.createMiaoshaPath(user, goodsId);
   return Result.success(path);
}

 

public boolean checkVerifyCode(MiaoshaUser user, long goodsId, int verifyCode) {
   if(user == null || goodsId <=0) {
      return false;
   }
   Integer codeOld = redisService.get(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId, Integer.class);
   if(codeOld == null || codeOld - verifyCode != 0 ) {
      return false;
   }
   redisService.delete(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId);
   return true;
}
public String createMiaoshaPath(MiaoshaUser user, long goodsId) {
   if(user == null || goodsId <=0) {
      return null;
   }
   String str = MD5Util.md5(UUIDUtil.uuid()+"123456");
       redisService.set(MiaoshaKey.getMiaoshaPath, ""+user.getId() + "_"+ goodsId, str);
   return str;
}
@RequestMapping(value="/{path}/do_miaosha", method=RequestMethod.POST)
@ResponseBody
public Result<Integer> miaosha(Model model,MiaoshaUser user,
      @RequestParam("goodsId")long goodsId,
      @PathVariable("path") String path) {
   model.addAttribute("user", user);
   if(user == null) {
      return Result.error(CodeMsg.SESSION_ERROR);
   }
   //验证path
   boolean check = miaoshaService.checkPath(user, goodsId, path);
   if(!check){
      return Result.error(CodeMsg.REQUEST_ILLEGAL);
   }

}

public boolean checkPath(MiaoshaUser user, long goodsId, String path) {
   if(user == null || path == null) {
      return false;
   }
   String pathOld = redisService.get(MiaoshaKey.getMiaoshaPath, ""+user.getId() + "_"+ goodsId, String.class);
   return path.equals(pathOld);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值