Commit 31c5d53d authored by sml's avatar sml

重复提交校验

parent d4b31294
package com.jz.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @ClassName: ResubmitCheck
* @Description: TODO(校验重复提交)
* @author ybz
* @date 2021年1月21日
*
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ResubmitCheck {
}
......@@ -28,5 +28,9 @@ public class CommConstant {
public static final String TRACE_ID="traceId";
/***************************************************/
//重复提交校验token
public static final String RE_SUBMIT_TOKEN = "resubmitToken";
/***************************************************/
}
package com.jz.common.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.annotation.ResubmitCheck;
import com.jz.common.bean.BaseResponse;
import com.jz.common.constant.CommConstant;
import com.jz.common.constant.StatuConstant;
/**
* @ClassName: LogInterceptor
* @Description: TODO(重复提交校验)
* @author ybz
* @date 2021年1月25日
*
*/
public class ResubmitCheckInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)handler;
ResubmitCheck resubmitCheck = handlerMethod.getMethod().getAnnotation(ResubmitCheck.class);
if (resubmitCheck!=null) {//判断是否需要进行重复提交校验
String resubmitToken = request.getHeader(CommConstant.RE_SUBMIT_TOKEN);
if (resubmitToken==null) {
resubmitToken = request.getParameter(CommConstant.RE_SUBMIT_TOKEN);
}
if (StringUtils.isEmpty(resubmitToken)) {//没有上传校验token
BaseResponse baseResponse = new BaseResponse();
baseResponse.setCode(StatuConstant.CODE_ERROR_PARAMETER);
baseResponse.setMessage("重复提交校验token不能为空!");
response.getWriter().write(JSONObject.toJSONString(baseResponse));
return false;
}
HttpSession session = request.getSession();
String resubmitTokenSession = (String) session.getAttribute(CommConstant.RE_SUBMIT_TOKEN);
if (!resubmitToken.equals(resubmitTokenSession)) {//判断是否重复提交
BaseResponse baseResponse = new BaseResponse();
baseResponse.setCode(StatuConstant.FAILURE_CODE);
baseResponse.setMessage("重复提交!");
response.getWriter().write(JSONObject.toJSONString(baseResponse));
return false;
}
//校验通过,置空token
session.setAttribute(CommConstant.RE_SUBMIT_TOKEN, null);
}
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 {
}
}
package com.jz.common.utils;
import java.util.UUID;
public class CommonUtils {
/**
* UUID随机数
* @return
*/
public static String generatePrimaryKeyId() {
String id = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
return id;
}
/**
* @Title: generateTraceId
* @Description: TODO(生成traceId)
* @param @return 参数
* @return String 返回类型
* @throws
*/
public static String generateTraceId() {
String traceId = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
return traceId;
}
/**
* @Title: generateUuid
* @Description: TODO(生成uuid)
* @param @return 参数
* @return String 返回类型
* @throws
*/
public static String generateUuid() {
String uuid = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
return uuid;
}
}
package com.jz.common.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.jz.common.constant.CommConstant;
/**
*
* @author hack2003
* @date 2016-8-11
* @version v1.0
*
*/
public class HttpRequestUtil {
/**
* 获取有nginx代理的真实地址IP
*
* @param request
* @return
* @throws Exception
*/
public static String getNginxRealIp(HttpServletRequest request) throws Exception {
String ip = request.getHeader("X-Forwarded-For");
if (!org.springframework.util.StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
ip = request.getHeader("X-Real-IP");// 需要在代理服务器上配置该属性
if (!org.springframework.util.StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
return request.getRemoteAddr();
}
/**
* 根据httpRequest获取url地址
*
* @param httpRequest
* @return
*/
public static String getRequestUrl(HttpServletRequest httpRequest) {
StringBuffer url = httpRequest.getRequestURL();
return url.toString();
}
/**
* @Title: getResubmitToken
* @Description: TODO(获取并设置重复提交判断token)
* @param @param request
* @param @return 参数
* @return String 返回类型
* @throws
*/
public static String getResubmitToken(HttpServletRequest request) {
HttpSession sesson = request.getSession();
String uuid = CommonUtils.generateUuid();
sesson.setAttribute(CommConstant.RE_SUBMIT_TOKEN, uuid);
return uuid;
}
}
......@@ -5,6 +5,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.jz.common.interceptor.LogInterceptor;
import com.jz.common.interceptor.ResubmitCheckInterceptor;
/**
* @ClassName: WebAppConfig
......@@ -22,6 +23,7 @@ public class WebAppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor());
registry.addInterceptor(new ResubmitCheckInterceptor());
}
}
......@@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.jz.common.bean.BaseBeanResponse;
import com.jz.common.bean.BaseResponse;
import com.jz.common.bean.PageInfoResponse;
import com.jz.common.constant.ResultCode;
import com.jz.common.constant.StatuConstant;
import com.jz.dmp.modules.controller.sys.bean.DmpMemberBatch;
......
package com.jz.dmp.modules.controller.sys;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jz.common.bean.BaseBeanResponse;
import com.jz.common.constant.StatuConstant;
import com.jz.dmp.modules.service.sys.SystemCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* 系统公共服务
* @author ybz
*
*/
@RestController
@RequestMapping("/sys/common")
@Api(tags = "系统公共服务")
public class SystemCommonController {
@Autowired
private SystemCommonService systemCommonService;
/**获取防止重复提交token
* @param dmpMemberRequest
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/getResubmitToken")
@ApiOperation(value = "获取防止重复提交token", notes = "获取防止重复提交token")
public BaseBeanResponse<String> getResubmitToken(HttpServletRequest httpRequest){
BaseBeanResponse<String> baseBeanResponse = new BaseBeanResponse<String>();
try {
baseBeanResponse = systemCommonService.getResubmitToken(httpRequest);
} catch (Exception e) {
baseBeanResponse.setMessage("获取失败");
baseBeanResponse.setCode(StatuConstant.FAILURE_CODE);
e.printStackTrace();
}
return baseBeanResponse;
}
}
package com.jz.dmp.modules.service.sys;
import javax.servlet.http.HttpServletRequest;
import com.jz.common.bean.BaseBeanResponse;
/**
* @ClassName: SystemCommonService
* @Description: TODO(系统公共服务service)
* @author ybz
* @date 2021年1月25日
*
*/
public interface SystemCommonService {
/**
* @Title: getResubmitToken
* @Description: TODO(获取重复提交校验token)
* @param @param httpRequest
* @param @return
* @param @throws Exception 参数
* @return BaseBeanResponse<String> 返回类型
* @throws
*/
public BaseBeanResponse<String> getResubmitToken(HttpServletRequest httpRequest)throws Exception;
}
package com.jz.dmp.modules.service.sys.impl;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.jz.common.bean.BaseBeanResponse;
import com.jz.common.constant.StatuConstant;
import com.jz.common.utils.HttpRequestUtil;
import com.jz.dmp.modules.service.sys.SystemCommonService;
/**
* @ClassName: SystemCommonServiceImpl
* @Description: TODO(公共服务实现类)
* @author ybz
* @date 2021年1月25日
*
*/
@Service
public class SystemCommonServiceImpl implements SystemCommonService {
/**
*获取重复提交校验token
*/
@Override
public BaseBeanResponse<String> getResubmitToken(HttpServletRequest httpRequest) throws Exception {
BaseBeanResponse<String> baseBeanResponse = new BaseBeanResponse<String>();
String resubmitToken = HttpRequestUtil.getResubmitToken(httpRequest);
baseBeanResponse.setCode(StatuConstant.SUCCESS_CODE);
baseBeanResponse.setMessage("获取成功");
baseBeanResponse.setData(resubmitToken);
return baseBeanResponse;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment