赞
踩
(1)秒杀接口地址隐藏。防止有人恶意秒杀
(2)数学公式验证码。也能防止恶意秒杀。并且能够减轻秒杀系统的瞬时流量,减轻并发量。
(3)接口限流防刷。限制一个用户1分钟之内只能访问某个接口10次。
每次点击秒杀按钮,才会生成秒杀地址,秒杀地址不是写死的,是从服务端获取,动态拼接而成的地址。(HTTP协议是明文传输,前端是防不住恶意用户的攻击,所以安全校验要放在服务端,从而禁止掉这些恶意攻击。)
在进行秒杀之前,去后端获取一个动态的秒杀地址path(服务端生成随机数作为path),在然后将这个随机数返回给前端,前端用这个path拼接在新的请求url(url : “/seckill/” + path + “/kill”)上作为参数,再去发请求到后台开始我们的秒杀。
<button class="btn btn-primary" type="button" id="buyButton" onclick="getMiaoshaPath()">立即秒杀</button>
发起请求/seckill/getPath去后端获取一个动态的秒杀地址path,然后前端在用这个path拼接在url(url : “/seckill/” + path + “/kill”)上作为参数,再去发请求到后台开始我们的秒杀。
- //获取秒杀地址
- function getMiaoshaPath() {
- var goodsId = $("#goodsId").val();
- $.ajax({
- url : "/miaosha/getPath",
- type : "GET",
- data : {
- goodsId : goodsId,
- //vertifyCode:$("#vertifyCode").val()
- },
- success : function(data) {
- if (data.code == 0) {
- //获取秒杀地址
- var path = data.data;
- //拿到path之后,才去做我的秒杀逻辑,并且在方法传入秒杀地址
- doMiaosha(path);
- } else {
- layer.msg(data.msg);
- }
- },
- error : function() {
- layer.msg("请求有误!");
- }
- });
- }
- function doMiaosha(path) {
- //alert(path);
- $.ajax({
- url : "/miaosha/" + path + "/do_miaosha",
- type : "POST",
- data : {
- goodsId : $("#goodsId").val()
- },
- success : function(data) {
- if (data.code == 0) {
- getMiaoshaResult($("#goodsId").val());
- } else {
- layer.msg(data.msg);
- }
- },
- error : function() {
- layer.msg("请求有误!");
- }
- });
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- /**
- * 获取秒杀的path,并且验证验证码的值是否正确
- */
- @RequestMapping(value ="/getPath")
- @ResponseBody
- public Result<String> getMiaoshaPath(HttpServletRequest request,Model model,MiaoshaUser user,
- @RequestParam("goodsId") Long goodsId,
- @RequestParam(value="vertifyCode",defaultValue="0") int vertifyCode) {
- model.addAttribute("user", user);
- //如果用户为空,则返回至登录页面
- if(user==null){
- return Result.error(CodeMsg.SESSION_ERROR);
- }
- //生成一个随机串
- String path=miaoshaService.createMiaoshaPath(user,goodsId);
- return Result.success(path);
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
注意:写入缓存,是后端接收到这个请求秒杀地址path参数,并且与缓存中的存的path比较,如果一致,进行秒杀逻辑,否则,非法请求。
- /**
- * 生成一个秒杀path,写入缓存,并且,返回至前台
- */
- public String createMiaoshaPath(MiaoshaUser user, Long goodsId) {
- String str=MD5Util.md5(UUIDUtil.uuid()+"123456");
- //将随机串保存在客户端,并且返回至客户端。
- //String path=""+user.getId()+"_"+goodsId;
- redisService.set(MiaoshaKey.getMiaoshaPath, ""+user.getId()+"_"+goodsId, str);
- return str;
- }
加上了秒杀接口地址隐藏之后可以防止恶意用户登陆之后,通过不断调用秒杀地址接口,骚扰服务器,所以使用动态获取秒杀地址,只有真正点击秒杀按钮,才会根据用户id和商品goodsId生成对应的秒杀接口地址。
但是,这种情况仍然不能解决利用机器人频繁点击按钮的操作,为了降低点击按钮的次数,以及高并发下,防止多个用户在同一时间内,并发出大量请求,加入数学公式图形验证码以及接口防刷等优化技术。
思路:点击秒杀之前,先输入验证码,分散用户的请求
生成验证码,并将验证码计算的结果保存到redis中
- @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);
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- 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;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在获取秒杀路径path的时候进行,验证码校验,每次验证正确之后需要从redis中删除该验证码。
- @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);
- }
-
- //每次验证过后,需要从redis删除验证码
- 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;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
如何统计1分钟内某个用户只能访问10次,采用定时器的方式消耗太大了,需要为每个用户创建一个定时器。我们可以直接用redis来存储某个用户的访问次数(user , times),并且redis的过期时间设置为60s,这样就能统计出一分钟内某个用户的访问次数了,一分钟结束后下一分钟,这个key就被清除了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。