前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springMVC 防重校验(拦截器)[通俗易懂]

springMVC 防重校验(拦截器)[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-03 14:36:55
2760
发布2022-08-03 14:36:55
举报

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
<mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="com.bitspace.bourse.core.interceptor.NoRepeatInterceptor"></bean>
</mvc:interceptor>
代码语言:javascript
复制
import com.alibaba.fastjson.JSON;
import com.bitspace.bourse.core.annotation.NoRepeatData;
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;
import java.util.HashMap;
import java.util.Map;

public class NoRepeatInterceptor extends HandlerInterceptorAdapter {
  
  

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  
  
        if (handler instanceof HandlerMethod) {
  
  
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            NoRepeatData annotation = method.getAnnotation(NoRepeatData.class);
            if (annotation != null) {
  
  
                if (repeatDataValidator(request)) {//如果重复相同数据
                    return false;
                } else {
  
  
                    return true;
                }
            }
            return true;
        } else {
  
  
            return super.preHandle(request, response, handler);
        }
    }

    public boolean repeatDataValidator(HttpServletRequest httpServletRequest) {
  
  
        String params = JSON.toJSONString(httpServletRequest.getParameterMap());
        String url = httpServletRequest.getRequestURI();
        Map<String, String> map = new HashMap<String, String>();
        map.put(url, params);
        String nowUrlParams = map.toString();

        Object preUrlParams = httpServletRequest.getSession().getAttribute("repeatData");
        if (preUrlParams == null) {//如果上一个数据为null,表示还没有访问页面
            httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
            return false;
        } else {//否则,已经访问过页面
            if (preUrlParams.toString().equals(nowUrlParams)) {//如果上次url+数据和本次url+数据相同,则表示城府添加数据
                return true;
            } else {//如果上次 url+数据 和本次url加数据不同,则不是重复提交
                httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
                return false;
            }

        }

    }

}
代码语言:javascript
复制
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 NoRepeatData {
  
  
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/107362.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年4月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档