springboot redis token_基于redis的API接口幂等设计

  • 在互联网API接口中,由于网络超时、手动刷新等经常导致客户端重复提交数据到服务端,这就要求在设计API接口时做好幂等控制。尤其是在面向微服务架构的系统中,系统间的调用非常频繁,如果不做好幂等性设置,轻则会导致脏数据入库,重则导致资损。
  • 本例基于Redis实现一个幂等控制框架。主要思路是在调用接口时传入全局唯一的token字段,标识一个请求是否是重复请求。
  • 总体思路

1)在调用接口之前先调用获取token的接口生成对应的令牌(token),并存放在redis当中。

2)在调用接口的时候,将第一步得到的token放入请求头中。

3)解析请求头,如果能获取到该令牌,就放行,执行既定的业务逻辑,并从redis中删除该token。

4)如果获取不到该令牌,就返回错误信息(例如:请勿重复提交)

<
package 
package com.zpc.redis.annotation;

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

/**
 * 标识一个接口是否需要为request自动添加token字段
 */
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtApiToken {

}
package com.zpc.redis.aop;

import com.zpc.redis.annotation.ExtApiIdempotent;
import com.zpc.redis.annotation.ExtApiToken;
import com.zpc.redis.service.RedisTokenService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * AOP切面
 * 完成2个功能:
 * 1)判断接口方法是否有ExtApiToken注解,如果有自动在HttpServletRequest中添加token字段值
 * 2)判断接口方法是否有ExtApiIdempotent注解,如果有则校验token
 */
@Component
@Aspect
public class ExtApiIdempotentAop {

    @Autowired
    private RedisTokenService tokenService;

    @Pointcut("execution(public * com.zpc.redis.controller.*.*(..))")
    public void myAop() {

    }

    @Before("myAop()")
    public void before(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        ExtApiToken annotation = signature.getMethod().getAnnotation(ExtApiToken.class);
        if (annotation != null) {
            getRequest().setAttribute("token", tokenService.getToken());
        }
    }

    //环绕通知
    @Around("myAop()")
    public Object doBefore(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //判断方法上是否有ExtApiIdempotent注解
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        ExtApiIdempotent declaredAnnotation = signature.getMethod().getDeclaredAnnotation(ExtApiIdempotent.class);
        if (declaredAnnotation != null) {
            String type = declaredAnnotation.type();
            String token = null;
            HttpServletRequest request = getRequest();
            if ("head".equals(type)) {
                token = request.getHeader("token");
            } else {
                token = request.getParameter("token");
            }

            if (StringUtils.isEmpty(token)) {
                return "请求参数错误!";
            }

            boolean tokenOk = tokenService.findToken(token);
            if (!tokenOk) {
                getResponse("请勿重复提交!");
                return null;
            }
        }
        //放行
        return proceedingJoinPoint.proceed();
    }

    public HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request;
    }

    public void getResponse(String msg) throws IOException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write(msg);
        writer.close();
    }
}
package com.zpc.redis.bean;

public class User {
    private String name;
    private Integer age;
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", sex='" + sex + ''' +
                '}';
    }
}
package com.zpc.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Service
public class RedisService {

    @Autowired
    private ShardedJedisPool shardedJedisPool;

    private <T> T execute(Function<T, ShardedJedis> fun) {
        ShardedJedis shardedJedis = null;
        try {
            // 从连接池中获取到jedis分片对象
            shardedJedis = shardedJedisPool.getResource();
            return fun.callback(shardedJedis);
        } finally {
            if (null != shardedJedis) {
                // 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
                shardedJedis.close();
            }
        }
    }

    /**
     * 执行set操作
     *
     * @param key
     * @param value
     * @return
     */
    public String set(final String key, final String value) {
        return this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callback(ShardedJedis e) {
                return e.set(key, value);
            }
        });
    }

    /**
     * 执行get操作
     *
     * @param key
     * @return
     */
    public String get(final String key) {
        return this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callback(ShardedJedis e) {
                return e.get(key);
            }
        });
    }

    /**
     * 执行删除操作
     *
     * @param key
     * @return
     */
    public Long del(final String key) {
        return this.execute(new Function<Long, ShardedJedis>() {
            @Override
            public Long callback(ShardedJedis e) {
                return e.del(key);
            }
        });
    }

    /**
     * 设置生存时间,单位为:秒
     *
     * @param key
     * @param seconds
     * @return
     */
    public Long expire(final String key, final Integer seconds) {
        return this.execute(new Function<Long, ShardedJedis>() {
            @Override
            public Long callback(ShardedJedis e) {
                return e.expire(key, seconds);
            }
        });
    }

    /**
     * 执行set操作并且设置生存时间,单位为:秒
     *
     * @param key
     * @param value
     * @return
     */
    public String set(final String key, final String value, final Integer seconds) {
        return this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callback(ShardedJedis e) {
                String str = e.set(key, value);
                e.expire(key, seconds);
                return str;
            }
        });
    }
}
package com.zpc.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * 生成token并且放到redis中
 */
@Service
public class RedisTokenService {

    private static final Integer TOKEN_TIMEOUT = 600;

    @Autowired
    RedisService redisService;

    public String getToken() {
        String token = "token" + UUID.randomUUID();
        redisService.set(token, token, TOKEN_TIMEOUT);
        return token;
    }

    public boolean findToken(String tokenKey){
        String token = redisService.get(tokenKey);
        if(StringUtils.isEmpty(token)){
            return false;
        }
        redisService.del(tokenKey);
        return true;
    }
}
package com.zpc.redis.controller;

import com.zpc.redis.annotation.ExtApiIdempotent;
import com.zpc.redis.bean.User;
import com.zpc.redis.service.RedisTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController {

    @Autowired
    private RedisTokenService tokenService;

    @RequestMapping(value = "addUser", produces = "application/json;charset=utf-8")
    public String addUser(@RequestBody User user, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return "请求参数错误!";
        }
        boolean tokenOk = tokenService.findToken(token);
        if (!tokenOk) {
            return "请勿重复提交!";
        }
        //执行正常的业务逻辑
        System.out.println("user info:" + user);
        return "添加成功!";
    }

    @RequestMapping(value = "getToken")
    public String getToken() {
        return tokenService.getToken();
    }

    @ExtApiIdempotent(type = "head")
    @RequestMapping(value = "addUser2", produces = "application/json;charset=utf-8")
    public String addUser2(@RequestBody User user) {
        //执行正常的业务逻辑
        System.out.println("user info:" + user);
        return "添加成功!!";
    }
}

获取token:

v2-bdad80bd4aa09a0254207a4ffb8e96d4_b.jpg

使用postman或者其他接口测试工具发起post请求,注意添加请求头:

v2-889a6eaa3d74d7a78f171992e85bbfc6_b.jpg

v2-9da02a4e529af12f8097909ed29833cf_b.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值