java吧 关注:1,257,949贴子:12,752,577
  • 2回复贴,共1

老哥们问个问题

只看楼主收藏回复

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SameUrlData {
}
以上是一个自定义注解
public class SameUrlDataInterceptor 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();
SameUrlData annotation = method.getAnnotation(SameUrlData.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=JsonMapper.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;
}
}
}


IP属地:江西1楼2018-08-27 00:24回复
    public boolean repeatDataValidator(HttpServletRequest httpServletRequest)
    {
    String params=JsonMapper.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;
    }
    }
    }
    就是这段中的hashmap不是多线程不安全的嘛,这样写高并发不会用问题吗


    IP属地:江西2楼2018-08-27 00:26
    回复
      广告
      立即查看
      有点乱


      IP属地:江西来自Android客户端3楼2018-08-27 00:27
      回复