流量削峰
验证码和问答题

POST seckill/captchas.jpg?skuId=10001
/*** 生成图片验证码*/@RequestMapping(value="/seckill/captchas.jpg", method=RequestMethod.POST})@ResponseBodypublic SeckillResponse<String> genCaptchas(String skuId, HttpServletRequest request, HttpServletResponse response) {//从cookie中取出userString user = getUserFromCookie(request);//根据skuId和user生成图片BufferedImage img=createCaptchas(user, skuId);try {OutputStream out=response.getOutputStream();ImageIO.write(img, "JPEG", out);out.flush();out.close();return null;} catch (IOException e) {e.printStackTrace();return SeckillResponse.error(ErrorMsg.SECKILL_FAIL);}}/*** 生成验证码图片方法*/public BufferedImage createCaptchas(String user, String skuId) {int width=90;int height=40;BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics graph=img.getGraphics();graph.setColor(new Color(0xDCDCDC));graph.fillRect(0, 0, width, height);Random random=new Random();//生成验证码String formula=createFormula(random);graph.setColor(new Color(0,100,0));graph.setFont(new Font("Candara",Font.BOLD,24));//将验证码写在图片上graph.drawString(formula, 8, 24);graph.dispose();//计算验证码的值int vCode=calc(formula);//将计算结果保存到redis上面去,过期时间1分钟cacheMgr.set("CAPTCHA_"+user+"_"+skuId, vCode, 60000);return img;}private String createFormula(Random random) {private static char[]ops=new char[] {'+','-','*'};//生成10以内的随机数int num1=random.nextInt(10);int num2=random.nextInt(10);int num3=random.nextInt(10);char oper1=ops[random.nextInt(3)];char oper2=ops[random.nextInt(3)];String exp=""+num1+oper1+num2+oper2+num3;return exp;}private static int calc(String formula) {try {ScriptEngineManager manager=new ScriptEngineManager();ScriptEngine engine=manager.getEngineByName("JavaScript");return (Integer) engine.eval(formula);}catch(Exception e){e.printStackTrace();return 0;}}
signature=base64(timestamp,md5(timestamp,vCode,skuId,user,randomSalt)
POST /seckill/settlement.html?skuId=10001&signature=ad6543audhhw13dg×tamp=1345611143&newCode=54
消息队列


文章转载自Alleria Windrunner,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




