修复bug,解决重复提交问题
parent
cd92339c6c
commit
e07b6ec554
|
@ -69,7 +69,6 @@ public class TokenFilter implements Filter {
|
|||
Filter.super.destroy();
|
||||
}
|
||||
|
||||
|
||||
//相应方法封装
|
||||
private void resp(ServletResponse servletResponse, String msg) throws IOException {
|
||||
HttpServletResponse response=(HttpServletResponse) servletResponse;
|
||||
|
@ -78,5 +77,6 @@ public class TokenFilter implements Filter {
|
|||
response.setContentType("application/json");
|
||||
outputStream.write(msg.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,23 @@
|
|||
package com.bwie.aop;
|
||||
|
||||
import com.bwie.utils.HttpServletRequestReader;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Log4j2
|
||||
@Aspect
|
||||
@Component
|
||||
|
@ -17,13 +28,53 @@ public class RepeatSubmit {
|
|||
@Pointcut("@annotation(com.bwie.aop.RepeatEnum)")
|
||||
private void pointcut(){}
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
private final Set<String> set=new HashSet<>();
|
||||
|
||||
private static final String apiTemplate="%s:%s:%s";
|
||||
|
||||
@Autowired
|
||||
private HttpServletResponse response;
|
||||
|
||||
|
||||
@Before("pointcut()")
|
||||
public void bef() {
|
||||
log.info("切面 -前");
|
||||
String token = request.getHeader("token");
|
||||
String body = HttpServletRequestReader.ReadAsChars(request);
|
||||
String format = String.format(apiTemplate, token, request.getRequestURI(), body);
|
||||
threadLocal.set(format);
|
||||
log.info("提交唯一标识:{}", format);
|
||||
//用户+请求+内容
|
||||
if (set.contains(format)) {
|
||||
resp(response,"重复提交");
|
||||
}
|
||||
set.add(format);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@After("pointcut()")
|
||||
public void aft(){
|
||||
log.info("切面 -后");
|
||||
set.remove(threadLocal.get());
|
||||
threadLocal.remove();
|
||||
}
|
||||
|
||||
|
||||
//相应方法封装
|
||||
private void resp(ServletResponse servletResponse, String msg){
|
||||
try {
|
||||
HttpServletResponse response=(HttpServletResponse) servletResponse;
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
response.setStatus(401);
|
||||
response.setContentType("application/json");
|
||||
outputStream.write(msg.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,31 +14,21 @@ import java.util.Set;
|
|||
@RequestMapping("/test")
|
||||
public class TestController {
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
private final Set<String> set=new HashSet<>();
|
||||
|
||||
private static final String apiTemplate="%s:%s:%s";
|
||||
|
||||
@RepeatEnum
|
||||
@PostMapping
|
||||
public String post(@RequestHeader("token") String token, @RequestBody String str){
|
||||
String format = String.format(apiTemplate, token, request.getRequestURI(), str);
|
||||
log.info("提交唯一标识:{}",format);
|
||||
//用户+请求+内容
|
||||
try {
|
||||
if (set.contains(format)){
|
||||
return "重复提交";
|
||||
}
|
||||
set.add(format);
|
||||
public String post( @RequestBody String str){
|
||||
|
||||
log.info("接收到请求:{}",str);
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
return "请求成功";
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
set.remove(format);
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "请求成功";
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,3 +1,3 @@
|
|||
artifactId=demo11
|
||||
groupId=com.bwie
|
||||
version=1.0-SNAPSHOT
|
||||
version=3.6.0
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
com\bwie\aop\RepeatSubmit.class
|
||||
com\bwie\config\WebMvcConfig.class
|
||||
com\bwie\Filter\XssFilter.class
|
||||
com\bwie\controller\LoginController.class
|
||||
com\bwie\domian\UserInfo.class
|
||||
com\bwie\Filter\TokenFilter.class
|
||||
com\bwie\controller\TestController.class
|
||||
com\bwie\domian\req\LoginReq.class
|
||||
com\bwie\config\XssHttpServletRequestWrapper.class
|
||||
com\bwie\aop\RepeatEnum.class
|
||||
com\bwie\domian\UserInfo$UserInfoBuilder.class
|
||||
com\bwie\config\LoginUserMap.class
|
||||
com\bwie\utils\HttpServletRequestReader.class
|
||||
com\bwie\App.class
|
||||
com\bwie\aop\RepeatSubmit.class
|
||||
com\bwie\config\WebMvcConfig.class
|
||||
com\bwie\config\XssHttpServletRequestWrapper$1.class
|
||||
com\bwie\domian\req\LoginReq.class
|
||||
com\bwie\domian\UserInfo$UserInfoBuilder.class
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
D:\Project\demo11\src\main\java\com\bwie\config\LoginUserMap.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\domian\req\LoginReq.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\config\WebMvcConfig.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\Filter\TokenFilter.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\aop\RepeatSubmit.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\aop\RepeatEnum.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\config\XssHttpServletRequestWrapper.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\domian\UserInfo.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\App.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\domian\req\LoginReq.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\config\WebMvcConfig.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\aop\RepeatEnum.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\Filter\XssFilter.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\controller\LoginController.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\controller\TestController.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\App.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\utils\HttpServletRequestReader.java
|
||||
|
|
Loading…
Reference in New Issue