提交 使用aop解决登录接口幂等性问题

master
冯凯 2023-10-03 22:40:37 +08:00
parent 543e41ba1e
commit 40ae59240b
4 changed files with 74 additions and 6 deletions

View File

@ -68,6 +68,10 @@
<artifactId>mybatis-spring-boot-starter</artifactId> <artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version> <version>2.3.1</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,19 @@
package com.durant.annotion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author
* @version 1.0
* @description: TODO
* @date 2023/10/3 22:02
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Independent {
}

View File

@ -0,0 +1,45 @@
package com.durant.aspect;
import com.durant.annotion.Independent;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author
* @version 1.0
* @description: TODO
* @date 2023/10/3 22:05
*/
@Aspect
@Component
public class IndependentAspect {
private Object result=null;
private Map<String,Object> resultMap=new ConcurrentHashMap<>();
@Around("@annotation(independent)")
public Object independent(ProceedingJoinPoint joinPoint, Independent independent) throws Throwable {
String id = generateId(joinPoint.getArgs());
if (resultMap.containsKey(id)){
System.out.println("不要重复提交请求");
return resultMap.get(id);
}
result= joinPoint.proceed();
resultMap.put(id,result);
return result;
}
private String generateId(Object[] args){
StringBuilder stringBuilder = new StringBuilder();
for (Object arg : args) {
stringBuilder.append(arg.hashCode());
}
return stringBuilder.toString();
}
}

View File

@ -1,13 +1,11 @@
package com.durant.controller; package com.durant.controller;
import com.durant.Result; import com.durant.Result;
import com.durant.annotion.Independent;
import com.durant.dto.request.LoginReq; import com.durant.dto.request.LoginReq;
import com.durant.service.LoginService; import com.durant.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/** /**
* @author * @author
@ -16,16 +14,18 @@ import org.springframework.web.bind.annotation.RestController;
* @date 2023/10/3 14:08 * @date 2023/10/3 14:08
*/ */
@RestController @RestController
@CrossOrigin(origins = "http://localhost:9528")
public class LoginController { public class LoginController {
@Autowired @Autowired
private LoginService loginService; private LoginService loginService;
@Independent
@PostMapping("/user/login") @PostMapping("/user/login")
public Result<LoginReq> userLogin(@RequestBody LoginReq loginReq){ public Result<LoginReq> userLogin(@RequestBody LoginReq loginReq){
Result<LoginReq> loginReqResult = loginService.userLogin(loginReq); Result<LoginReq> loginReqResult = loginService.userLogin(loginReq);
if (loginReqResult!=null){ if (loginReqResult.getData()!=null){
return Result.success(loginReq); return Result.success(loginReqResult.getData());
} }
return Result.error(); return Result.error();