防止请求2次调用

定义一个注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)//运行时有效
@Target(ElementType.METHOD)//作用于方法
public @interface RedisLook {
}

写一个aop 环绕增强  拿到当前登录人唯一身份 存入redis   执行完删除

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 说明 赛事下权限 切面类
 *
 * @author 马志宏
 * @date 2020/4/24 16:57
 */
@Aspect
@Component
public class AnnotationAspect {

    @Autowired
    private BladeRedisCache redisCache;

    @Around(value = "@annotation(around)")
    @ResponseBody
    public Object redisLook(ProceedingJoinPoint point, RedisLook around) {
        TourAlbumDownloadDTO tourAlbumDownloadDTO = (TourAlbumDownloadDTO) point.getArgs()[0];
        JSONObject o = redisCache.get(tourAlbumDownloadDTO.getDownloadOpenid());
        if (Objects.isNull(o)){
            redisCache.set(tourAlbumDownloadDTO.getDownloadOpenid(), JSONObject.toJSON(tourAlbumDownloadDTO));
        }else {
            if (o.equals(JSONObject.toJSON(tourAlbumDownloadDTO))){
                return R.fail("不可重复提交!");
            }
        }
        try {
            return    point.proceed();
        } catch (Throwable throwable) {
            throw new RuntimeException("服务器开小差了,请稍后再试");
        } finally {
            redisCache.del(tourAlbumDownloadDTO.getDownloadOpenid());
        }
    }
}

注解使用

    /**
     * @author MZH
     * @since 2020-05-12
     */
    @PostMapping("/buy")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "购买", notes = "传入tourAlbumDownload")
    @RedisLook
    public  R<Map<String,Object>>  buy(@Valid @RequestBody TourAlbumDownloadDTO tourAlbumDownloadDTO) {
        return tourAlbumOrderService.saveAndUpdateTourAlbumDownload(tourAlbumDownloadDTO);
    }

 

posted @ 2020-06-10 13:51  ±小马哥  阅读(323)  评论(0编辑  收藏  举报