赞
踩
使用redis+token实现幂等性操作,防止表单的重复提交和一些其他重复请求操作。有些接口需要保证操作的唯一性,比如:订单接口(重复点击下单,只会生成一个订单号);支付接口(重复支付也只会扣一次款);表单接口等等…
本文使用token机制来实现幂等性操作
这里以表单提交为例,大致的分析下流程:
【:tw-1f36c:情景:】用户提交了一个表单,但是网络比较慢,用户点了多次提交。此时第一个提交的内容token验证通过了,redis中token被销毁,剩下的请求虽然也携带了token,但是redis中已然没有了数据,剩下的请求验证全部不通过,请求被放弃…
操作演示:第一次正常提交,第二次模拟多次提交
1、跳转页面
@GetMapping("/diary/newDiary")
@ApiOperation("跳转到创建随笔页面")
//这是一个简单的跳转页面controller,在跳转时生成一个token到前段页面去
public ModelAndView toInsert(Model model){
String newDiaryToken = redisUtils.getToken();
model.addAttribute("newDiaryToken",newDiaryToken);
return new ModelAndView("diary/insert");
}
2、token生成工具
/** * @Description: 创建Token,怕麻烦的可以直接生成UUID作为toekn的key和value * @Date: 2020/1/8 0008 */ public String getToken(){ String token_value = UUID.randomUUID().toString().replace("-",""); int index = new Random().nextInt(5); String token_key = token_value.substring(0,index); try { redisTemplate.opsForValue().set(token_key,token_value); return token_key; }catch (Exception e){ e.printStackTrace(); return null; } }
3、表单页面加入隐藏项
<input type="hidden" th:value="${newDiaryToken}" name = "token">
4、拦截器拦截url请求,验证token
package com.braisedpanda.my.blog.web.framework.filter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; /** * @program: my-blog * @description: token拦截器 * * @create: 2020-01-08 10:08 **/ @Configuration public class TokenInterceptorConfig implements WebMvcConfigurer { @Bean TokenInterceptor tokenInterceptor(){ return new TokenInterceptor(); } public void addInterceptors(InterceptorRegistry registry) { //拦截指定URL registry.addInterceptor(tokenInterceptor()) .addPathPatterns("/admin/diary/insert/**"); } }
package com.braisedpanda.my.blog.web.framework.filter; import com.braisedpanda.my.blog.web.config.redis.RedisUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @program: my-blog * @description: * @create: 2020-01-08 10:13 **/ @Configuration @Slf4j public class TokenInterceptor implements HandlerInterceptor { @Autowired private RedisUtils redisUtils; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //验证token String token = request.getParameter("token"); String value = (String)redisUtils.get(token); if(!StringUtils.isEmpty(value) && value !=null && value.length()>0){ redisUtils.del(token); log.info("处理请求成功......."); return true; }else { log.error("请勿重复提交表单......."); response.sendRedirect("/toError"); return false; } } }
跳转到新建表单页面
表单页面
redis中有了token
提交表单成功
token被删除
多次请求被抛弃
错误页面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。