首页 > 其他分享 >Spring MVC防重复提交

Spring MVC防重复提交

时间:2023-03-20 18:35:04浏览次数:34  
标签:return UUID Spring request token MVC 提交 false


如何在Spring MVC里面解决此问题(其它框架也一样,逻辑一样,思想一样,和具体框架没什么关系)。要解决重复提交,有很多办法,比如说在提交完成后redirect一下,也可以用本文提到的使用token的方法(我不使用redirect是因为那样解决不了ajax提交数据或者移动应用提交数据,另一个原因是现在比较通行的方法是使用token,像python里的django框架也是使用token来解决)。

使用token的逻辑是,给所有的url加一个拦截器,在拦截器里面用java的UUID生成一个随机的UUID并把这个UUID放到session里面,然后在浏览器做数据提交的时候将此UUID提交到服务器。服务器在接收到此UUID后,检查一下该UUID是否已经被提交,如果已经被提交,则不让逻辑继续执行下去…

好的,来点实际代码,也许实际代码才是最好的老师:

注解Token代码:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Token {

boolean save() default false;

boolean remove() default false;
}




拦截器TokenInterceptor代码:



public class TokenInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null) {
boolean needSaveSession = annotation.save();
if (needSaveSession) {
request.getSession(false).setAttribute("token", UUID.randomUUID().toString());
}
boolean needRemoveSession = annotation.remove();
if (needRemoveSession) {
if (isRepeatSubmit(request)) {
return false;
}
request.getSession(false).removeAttribute("token");
}
}
return true;
} else {
return super.preHandle(request, response, handler);
}
}

private boolean isRepeatSubmit(HttpServletRequest request) {
String serverToken = (String) request.getSession(false).getAttribute("token");
if (serverToken == null) {
return true;
}
String clinetToken = request.getParameter("token");
if (clinetToken == null) {
return true;
}
if (!serverToken.equals(clinetToken)) {
return true;
}
return false;
}
}




然后在Spring MVC的配置文件里加入:[color=red]注意,要在*-servlet。xml里面配置[/color]



<!-- 拦截器配置 -->
<mvc:interceptors>
<!-- 配置Shiro拦截器,实现注册用户的注入 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.storezhang.video.shiro.ShiroInterceptor"/>
</mvc:interceptor>
<!-- 配置Token拦截器,防止用户重复提交数据 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.storezhang.web.spring.TokenInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>


相关代码已经注释,相信你能看懂。




关于这个方法的用法是:在需要生成token的controller上增加@Token(save=true),而在需要检查重复提交的controller上添加@Token(remove=true)就可以了。


另外,你需要在view里在form里增加下面代码:



<input type="hidden" name="token" value="${token}" />

标签:return,UUID,Spring,request,token,MVC,提交,false
From: https://blog.51cto.com/u_3871599/6138471

相关文章

  • Springboot项目密码加密器jasypt
    最新版依赖<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version></dependenc......
  • spring工具类
    spring工具类获取bean编写packagecom.cloudiip.security.utils;importorg.springframework.stereotype.Component;importorg.springframework.context.Applicatio......
  • org.springframework.core.metrics.ApplicationStartup
    日志Exceptioninthread"main"java.lang.NoClassDefFoundError:org/springframework/core/metrics/ApplicationStartup atorg.springframework.boot.SpringApplicat......
  • Spring@Transactional事务失效的场景
    ①未启用事务管理功能②事务方法所在类未被加载成Bean③事务方法不是public类型④事务方法被final修饰⑤事务方法被同类的方法调用⑥多线程调用⑦手动trycatch了异......
  • SpringBoot
    一、SpringBoot的入门SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程spring优化SpringBoot配置繁琐==>自......
  • git提交代码时报SSL certificate problem_ unable to get local issuer certificate
    问题描述在提交代码到git仓库的时候报这个错误SSLcertificateproblem:unabletogetlocalissuercertificate原因这个问题是由于没有配置信任的服务器HTTPS验证。......
  • SpringBoot 2.6.0 以上 @Autowired 没有解决循环依赖
    使用@Autowired,仍在提示循环依赖,提示:Asalastresort,itmaybepossibletobreakthecycleautomaticallybysettingspring.main.allow-circular-referencesto......
  • 跟老杜手撕Spring6教程(十)set注入专题-1
    set注入专题本篇文章是set注入专题,上篇说了构造注入​​https://blog.51cto.com/u_15485663/6132498​​配合视频教程观看,更易理解吸收,动力节点老杜的Spring6教程采用难度逐......
  • idea.2022.3.x社区版插件“intellij-spring-assistant”
    idea.2022.3.x社区版插件“intellij-spring-assistant”https://blog.csdn.net/ErickPang/article/details/128794674?spm=1001.2101.3001.6650.2&utm_medium=distribute.......
  • Asp.Net MVC学习笔记2
       Areas区域。随着项目的不断扩大,Controllers控制器也在不断变多,这样即时使用文件夹分隔也不易于项目维护,和阅读。Asp.NetMVC提供了Areas的功能可以把项目中的没一......