• spring 注解重复(防重复请求)


    1、配置拦截器

    spring-mvc.xml

    <mvc:interceptors>
    <mvc:interceptor>
    <mvc:mapping path="/**/*"/>
    <mvc:exclude-mapping path="/css/**"/>
    <mvc:exclude-mapping path="/images/**"/>
    <mvc:exclude-mapping path="/js/**"/>
    <mvc:exclude-mapping path="/upload/**"/>
    <bean class="com.tp.soft.common.interceptor.AvoidDuplicateSubmissionInterceptor" />
    </mvc:interceptor>
    </mvc:interceptors>

    2、写注解接口

    Token.java

    package com.tp.soft.common.interceptor;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention (RetentionPolicy.RUNTIME)
    public @interface Token {
         boolean save() default false ;
         boolean remove() default false ;
    }

    3、写拦截器

    AvoidDuplicateSubmissionInterceptor.java

    package com.tp.soft.common.interceptor;
    
    import java.lang.reflect.Method;
    import java.util.UUID;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.method.HandlerMethod;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class AvoidDuplicateSubmissionInterceptor 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 ;
        }
    
    }

    4、在需要生成token的方法上写上@Token(save=true) 在重复请求的方法上写上@Token(remove=true)

    5、在页面上如果是form表单写上

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

    如果是异步的则直接用${token}传入参数

  • 相关阅读:
    Java系列教程-Spring 教程目录
    python中如何给散点图上面的特定点做标记
    OpenCV:Python下OpenCV安装和入门最强详细攻略
    如何实现一个 windows 桌面动态壁纸
    学习C/C++的简单方法
    手眼标定
    python合并多个txt文件
    python qq发消息
    python获取当前天气情况
    程序代写、毕业设计
  • 原文地址:https://www.cnblogs.com/tplovejava/p/5473159.html
Copyright © 2020-2023  润新知