创建AutoIdempotent 拦截注解,后面使用在方法直接注解即可
@Target(ElementType.METHOD) //应用在方法级别上
@Retention(RetentionPolicy.RUNTIME)//运行时
public @interface AutoIdempotent {
}
创建拦截器
public class AutoIdempotentInterceptor implements HandlerInterceptor{
@Autowired
private TokenServiceImpl tokenService; //token生成类
/**
* 预处理
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
//被ApiIdempotment标记的扫描
AutoIdempotent methodAnnotation = method.getAnnotation(AutoIdempotent.class);
if (methodAnnotation != null) {
try {
return tokenService.checkToken(request);// 幂等性校验, 校验通过则放行, 校验失败则抛出异常, 并通过统一异常处理返回友好提示
}catch (Exception ex){
response.setContentType("text/html;charset=UTF-8");
Map<String,Object&