package org.changneng.framework.frameworkbusiness.repeatCommit; import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.changneng.framework.frameworkcore.utils.JacksonUtils; import org.changneng.framework.frameworkcore.utils.ResponseJson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.http.HttpStatus; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class RepeatCommitInterceptAdapterToken implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); CheckRepeatToken isCheck = method.getAnnotation(CheckRepeatToken. class ); String paramRequest = request.getParameter( "token" ); try { if (isCheck != null ){ //判断token参数是否为空 if (paramRequest == null || "" .equals(paramRequest)){ System.out.println( "参数为空" ); response.setHeader( "Content-Type" , "application/json;charset=UTF-8" ); try { response.getWriter().write(JacksonUtils.toJsonString( new ResponseJson().failure(HttpStatus.BAD_REQUEST.toString(), "007" , "不能重复提交" , "不能重复提交" , null ))); response.getWriter().close(); } catch (IOException e) { e.printStackTrace(); } return false ; } String uuId = (String) request.getSession().getAttribute( "tokenReport" ); if (uuId!= null ){ synchronized ( this ) { String tempUuId = (String) request.getSession().getAttribute( "tokenReport" ); if (paramRequest.equals(tempUuId)&&! "" .equals(tempUuId)){ System.out.println( "第一次提交" ); request.getSession().removeAttribute( "tokenReport" ); return true ; } else { System.out.println( "重复提交了" ); response.setHeader( "Content-Type" , "application/json;charset=UTF-8" ); try { response.getWriter().write(JacksonUtils.toJsonString( new ResponseJson().failure(HttpStatus.BAD_REQUEST.toString(), "007" , "不能重复提交" , "不能重复提交" , null ))); response.getWriter().close(); } catch (IOException e) { e.printStackTrace(); } return false ; } } } else { System.out.println( "重复提交了" ); response.setHeader( "Content-Type" , "application/json;charset=UTF-8" ); try { response.getWriter().write(JacksonUtils.toJsonString( new ResponseJson().failure(HttpStatus.BAD_REQUEST.toString(), "007" , "不能重复提交" , "" , null ))); response.getWriter().close(); } catch (IOException e) { e.printStackTrace(); } return false ; } } } catch (Exception ex){ ex.printStackTrace(); } return true ; } /*else { return super.preHandle(request, response, handler); }*/ return true ; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } } // 在跳转页面的controller方法中添加 @PutSessionValue ,在新增保存的方法上添加 @CheckRepeatToken 注解即可 |