Commit 17a9e657 by 段启岩

开启全局阻止重复提交

parent b6c9cce7
......@@ -31,6 +31,8 @@ public class BeyondCloudsProperties {
*/
private Boolean debug;
private Boolean globalPreventDuplicate = false;
/**
* 认证配置
*/
......
package cn.meteor.beyondclouds.core.aop;
import cn.meteor.beyondclouds.common.enums.ErrorCode;
import cn.meteor.beyondclouds.config.properties.BeyondCloudsProperties;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.authentication.Subject;
import cn.meteor.beyondclouds.core.exception.ServiceException;
......@@ -11,8 +12,10 @@ import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.cors.CorsUtils;
......@@ -27,15 +30,69 @@ import java.util.concurrent.locks.Lock;
@Slf4j
public class PreventDuplicateSubmission {
private BeyondCloudsProperties beyondCloudsProperties;
private RedisLockFactory redisLockFactory;
@Autowired
public PreventDuplicateSubmission(RedisLockFactory redisLockFactory) {
public PreventDuplicateSubmission(RedisLockFactory redisLockFactory, BeyondCloudsProperties beyondCloudsProperties) {
this.redisLockFactory = redisLockFactory;
this.beyondCloudsProperties = beyondCloudsProperties;
}
@Around(value = "@annotation(cn.meteor.beyondclouds.core.annotation.PreventDuplicate)")
@Around(value = "pointCutPreventDuplicateAnnotation()")
public Object processTx(ProceedingJoinPoint jp) throws Throwable {
if (!beyondCloudsProperties.getGlobalPreventDuplicate()) {
log.info("global prevent duplicate closed.");
return process(jp);
} else {
return jp.proceed();
}
}
@Around(value = "pointCutRequestMapping() || pointCutGetMapping() || pointCutPutMapping() || pointCutPostMapping() || pointCutDeleteMapping()")
public Object processGlobalPreventDu(ProceedingJoinPoint jp) throws Throwable {
if (beyondCloudsProperties.getGlobalPreventDuplicate()) {
log.info("global prevent duplicate opened.");
return process(jp);
} else {
return jp.proceed();
}
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void pointCutRequestMapping() {
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
public void pointCutGetMapping() {
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.PutMapping)")
public void pointCutPutMapping() {
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void pointCutDeleteMapping() {
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
public void pointCutPostMapping() {
}
@Pointcut("@annotation(cn.meteor.beyondclouds.core.annotation.PreventDuplicate)")
public void pointCutPreventDuplicateAnnotation() {
}
private Object process(ProceedingJoinPoint jp) throws Throwable {
String token = jp.getSignature().toString();
token = SubjectUtils.getSubject().getIdentification() + ":" + token;
Lock lock = redisLockFactory.createLock(Md5Utils.encode(token));
......
......@@ -25,4 +25,9 @@ public class SysConstants {
* 存储在HttpServletRequest里面的认证错误的信息
*/
public static final String AUTHORIZATION_ERROR_CODE = "AUTHORIZATION_ERROR_CODE";
/**
*
*/
public static final String THREAD_LOCAL_KEY_PREVENT_DUPLICATE_REDIS_LOCK = "THREAD_LOCAL_KEY_PREVENT_DUPLICATE_REDIS_LOCK";
}
package cn.meteor.beyondclouds.core.lock;
import cn.meteor.beyondclouds.common.helper.IRedisHelper;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Arrays;
......
......@@ -87,6 +87,7 @@ logging:
# 云里云外
beyondclouds:
debug: true
global-prevent-duplicate: true
auth:
qq:
client-id: 101846021
......
......@@ -87,6 +87,7 @@ logging:
# 云里云外
beyondclouds:
debug: true
global-prevent-duplicate: true
auth:
qq:
client-id: 101846021
......
......@@ -85,6 +85,7 @@ logging:
# 云里云外
beyondclouds:
debug: true
global-prevent-duplicate: true
auth:
qq:
client-id: 101846021
......
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