token-防止表单重复提交

1 篇文章 0 订阅
在网络不好的情况下用户会出现多次点击提交表单的操作,以前的解决办法都是在前台用js进行处理,可后来发现还是有重复提交的数据,顾又增加了一层token的校验。
我这个token是基于springmvc结构实现的。

jsp表单提交代码如下

<table width="100%" align="center" class="ui-widget ui-widget-content" >
        <form action="/report/saleFoodDetailReport/saveSSS.htm" method="post">
            <input type="hidden" name="token" value="${token}">
            <input type="submit" name="" value="SSSSSS"/>
        </form>
    </table>

controller代码

@RequestMapping("/saveSSS.htm")
    @IAvoidDuplicateSubmission(needRemoveToken=true)
    public void saveSSS(HttpServletRequest request,HttpServletResponse response){
    }

类IAvoidDuplicateSubmission

@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IAvoidDuplicateSubmission {
    boolean needSaveToken() default false;
    boolean needRemoveToken() default false;
}

累AvoidDuplicateSubmissionInterceptor


import com.wanda.commn.service.IAvoidDuplicateSubmission;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;


/**
 * Created by PC06QLEQ on 2016/5/31.
 */
public class AvoidDuplicateSubmissionInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        System.out.println(handler.getClass());

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        IAvoidDuplicateSubmission annotation = method.getAnnotation(IAvoidDuplicateSubmission.class);
        if (annotation != null) {
            boolean needSaveSession = annotation.needSaveToken();
            if (needSaveSession) {
                String sValue=TokenProcessor.getInstance().generateToken();
                request.getSession(false).setAttribute("token", sValue);
                request.getSession(false).setAttribute(sValue, sValue);
                System.out.println("sValue::::"+sValue);
            }

            boolean needRemoveSession = annotation.needRemoveToken();
            if (needRemoveSession) {
                String reValue=isRepeatSubmit(request);
                if ("0".equals(reValue)) {
                    return false;
                }
                else{
                    request.getSession(false).removeAttribute(reValue);
                }
            }
        }
        return true;
    }

    private String isRepeatSubmit(HttpServletRequest request) {
        String serverToken = (String) request.getSession(false).getAttribute(request.getParameter("token"));
        System.out.println("serverToken::::"+serverToken);
        if (serverToken == null) {
            return "0";
        }
        String clinetToken = request.getParameter("token");
        System.out.println("clinetToken::::"+clinetToken);
        if (clinetToken == null) {
            return "0";
        }
        if (!serverToken.equals(clinetToken)) {
            return "0";
        }
        return serverToken;
    }

}

配置文件servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <!-- 声明纳入spring mvc web上下文的转向 -->
    <context:component-scan base-package="com.wanda.*.controller"></context:component-scan>

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="com.wanda.framework.resolver.DragonHandlerMethodArgumentResolver"></bean>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>


    <mvc:interceptors>  
        <bean class="com.wanda.login.controller.SuInterceptor"/>  
    </mvc:interceptors>  



    <!-- 声明视图协商解析器 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/views/" />
                    <property name="suffix" value=".jsp" />
                </bean>
            </list>
        </property>
    </bean>

</beans>

以上就是我实现的功能,希望对大家有一定的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值