SpringMVC防止重复提交
问题描述:
现在的网站在注册步骤中,由于后台要处理大量信息,造成响应变慢(测试机器性能差也是造成变慢的一个因素),在前端页面提交信息之前,等待后端响应,此时如果用户
再点一次提交按钮,后台会保存多份用户信息。为解决此问题,借鉴了struts2的token思路,在springmvc下实现token。
实现思路:
在springmvc配置文件中加入拦截器的配置,拦截两类请求,一类是到页面的,一类是提交表单的。当转到页面的请求到来时,生成token的名字和token值,一份放到redis缓存中,一份放传给页面表单的隐藏域。(注:这里之所以使用redis缓存,是因为tomcat服务器是集群部署的,要保证token的存储介质是全局线程安全的,而redis是单线程的)
当表单请求提交时,拦截器得到参数中的tokenName和token,然后到缓存中去取token值,如果能匹配上,请求就通过,不能匹配上就不通过。这里的tokenName生成时也是随机的,每次请求都不一样。而从缓存中取token值时,会立即将其删除(删与读是原子的,无线程安全问题)。
实现方式:
TokenInterceptor.java
package com.xxx.www.common.interceptor; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.xxx.cache.redis.IRedisCacheClient;
import com.xxx.common.utility.JsonUtil;
import com.xxx.www.common.utils.TokenHelper; /**
*
* @see TokenHelper
*/
public class TokenInterceptor extends HandlerInterceptorAdapter
{
private static Logger log = Logger.getLogger(TokenInterceptor.class);
private static Map<String , String> viewUrls = new HashMap<String , String>();
private static Map<String , String> actionUrls = new HashMap<String , String>();
private Object clock = new Object();
@Autowired
private IRedisCacheClient redisCacheClient;
static
{
viewUrls.put("/user/regc/brandregnamecard/", "GET");
viewUrls.put("/user/regc/regnamecard/", "GET");
actionUrls.put("/user/regc/brandregnamecard/", "POST");
actionUrls.put("/user/regc/regnamecard/", "POST");
}
{
TokenHelper.setRedisCacheClient(redisCacheClient);
}
/**
* 拦截方法,添加or验证token
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
String url = request.getRequestURI();
String method = request.getMethod();
if(viewUrls.keySet().contains(url) && ((viewUrls.get(url)) == null || viewUrls.get(url).equals(method)))
{
TokenHelper.setToken(request);
return true;
}
else if(actionUrls.keySet().contains(url) && ((actionUrls.get(url)) == null || actionUrls.get(url).equals(method)))
{
log.debug("Intercepting invocation to check for valid transaction token.");
return handleToken(request, response, handler);
}
return true;
}
protected boolean handleToken(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
synchronized(clock)
{
if(!TokenHelper.validToken(request))
{
System.out.println("未通过验证...");
return handleInvalidToken(request, response, handler);
}
}
System.out.println("通过验证...");
return handleValidToken(request, response, handler);
} /**
* 当出现一个非法令牌时调用
*/
protected boolean handleInvalidToken(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
Map<String , Object> data = new HashMap<String , Object>();
data.put("flag", 0);
data.put("msg", "请不要频繁操作!");
writeMessageUtf8(response, data);
return false;
}
/**
* 当发现一个合法令牌时调用.
*/
protected boolean handleValidToken(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
return true;
} private void writeMessageUtf8(HttpServletResponse response, Map<String , Object> json) throws IOException
{
try
{
response.setCharacterEncoding("UTF-8");
response.getWriter().print(JsonUtil.toJson(json));
}
finally
{
response.getWriter().close();
}
} }
TokenHelper.java
package com.xxx.www.common.utils;
import java.math.BigInteger;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.xxx.cache.redis.IRedisCacheClient; /**
* TokenHelper
*
*/
public class TokenHelper
{
/**
* 保存token值的默认命名空间
*/
public static final String TOKEN_NAMESPACE = "xxx.tokens";
/**
* 持有token名称的字段名
*/
public static final String TOKEN_NAME_FIELD = "xxx.token.name";
private static final Logger LOG = Logger.getLogger(TokenHelper.class);
private static final Random RANDOM = new Random();
private static IRedisCacheClient redisCacheClient;// 缓存调用,代替session,支持分布式
public static void setRedisCacheClient(IRedisCacheClient redisCacheClient)
{
TokenHelper.redisCacheClient = redisCacheClient;
}
/**
* 使用随机字串作为token名字保存token
*
* @param request
* @return token
*/
public static String setToken(HttpServletRequest request)
{
return setToken(request, generateGUID());
}
/**
* 使用给定的字串作为token名字保存token
*
* @param request
* @param tokenName
* @return token
*/
private static String setToken(HttpServletRequest request, String tokenName)
{
String token = generateGUID();
setCacheToken(request, tokenName, token);
return token;
}
/**
* 保存一个给定名字和值的token
*
* @param request
* @param tokenName
* @param token
*/
private static void setCacheToken(HttpServletRequest request, String tokenName, String token)
{
try
{
String tokenName0 = buildTokenCacheAttributeName(tokenName);
redisCacheClient.listLpush(tokenName0, token);
request.setAttribute(TOKEN_NAME_FIELD, tokenName);
request.setAttribute(tokenName, token);
}
catch(IllegalStateException e)
{
String msg = "Error creating HttpSession due response is commited to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: " + e.getMessage();
LOG.error(msg, e);
throw new IllegalArgumentException(msg);
}
}
/**
* 构建一个基于token名字的带有命名空间为前缀的token名字
*
* @param tokenName
* @return the name space prefixed session token name
*/
public static String buildTokenCacheAttributeName(String tokenName)
{
return TOKEN_NAMESPACE + "." + tokenName;
} /**
* 从请求域中获取给定token名字的token值
*
* @param tokenName
* @return the token String or null, if the token could not be found
*/
public static String getToken(HttpServletRequest request, String tokenName)
{
if(tokenName == null)
{
return null;
}
Map params = request.getParameterMap();
String[] tokens = (String[]) (String[]) params.get(tokenName);
String token;
if((tokens == null) || (tokens.length < 1))
{
LOG.warn("Could not find token mapped to token name " + tokenName);
return null;
} token = tokens[0];
return token;
}
/**
* 从请求参数中获取token名字
*
* @return the token name found in the params, or null if it could not be found
*/
public static String getTokenName(HttpServletRequest request)
{
Map params = request.getParameterMap();
if(!params.containsKey(TOKEN_NAME_FIELD))
{
LOG.warn("Could not find token name in params.");
return null;
}
String[] tokenNames = (String[]) params.get(TOKEN_NAME_FIELD);
String tokenName;
if((tokenNames == null) || (tokenNames.length < 1))
{
LOG.warn("Got a null or empty token name.");
return null;
}
tokenName = tokenNames[0];
return tokenName;
}
/**
* 验证当前请求参数中的token是否合法,如果合法的token出现就会删除它,它不会再次成功合法的token
*
* @return 验证结果
*/
public static boolean validToken(HttpServletRequest request)
{
String tokenName = getTokenName(request);
if(tokenName == null)
{
LOG.debug("no token name found -> Invalid token ");
return false;
}
String token = getToken(request, tokenName);
if(token == null)
{
if(LOG.isDebugEnabled())
{
LOG.debug("no token found for token name " + tokenName + " -> Invalid token ");
}
return false;
}
String tokenCacheName = buildTokenCacheAttributeName(tokenName);
String cacheToken = redisCacheClient.listLpop(tokenCacheName);
if(!token.equals(cacheToken))
{
LOG.warn("xxx.internal.invalid.token Form token " + token + " does not match the session token " + cacheToken + ".");
return false;
}
// remove the token so it won't be used again
return true;
}
public static String generateGUID()
{
return new BigInteger(165, RANDOM).toString(36).toUpperCase();
} }
spring-mvc.xml
<!-- token拦截器-->
<bean id="tokenInterceptor" class="com.xxx.www.common.interceptor.TokenInterceptor"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="tokenInterceptor"/>
</list>
</property>
</bean>
input.jsp 在form中加如下内容:
<input type="hidden" name="<%=request.getAttribute("xxx.token.name") %>" value="<%=token %>"/>
<input type="hidden" name="xxx.token.name" value="<%=request.getAttribute("xxx.token.name") %>"/>
当前这里也可以用类似于struts2的自定义标签来做。
SpringMVC防止重复提交的更多相关文章
- springmvc防止重复提交拦截器
一.拦截器实现,ResubmitInterceptorHandler.java import org.apache.commons.lang3.StringUtils; import org.spri ...
- 161116、springmvc自己实现防止表单重复提交(基于注解)
原理:在去某个页面直接生成一个随机数(这里使用的是UUID)并放入session中,用户提交表单时将这个随机数传入服务端与session中的值进行比较,如果不不存在或不相等,则认为是重复提交:如果相等 ...
- SpringMVC后台token防重复提交解决方案
本文介绍如何使用token来防止前端重复提交的问题. 目录 1.思路 2.拦截器源码实现 3.注解源码 4.拦截器的配置 5.使用指南 6.结语 思路 1.添加拦截器,拦截需要防重复提交的请求 2.通 ...
- 拦截器springmvc防止表单重复提交【1】
[参考博客:http://www.cnblogs.com/hdwpdx/archive/2016/03/29/5333943.html] springmvc 用拦截器+token防止重复提交 首先,防 ...
- springmvc防止表单重复提交demo
原理:在去某个页面直接生成一个随机数(这里使用的是UUID)并放入session中,用户提交表单时将这个随机数传入服务端与session中的值进行比较,如果不不存在或不相等,则认为是重复提交:如果相等 ...
- 重新理解了重定向,利用重定向可以防止用户重复提交表单(兼谈springmvc重定向操作)
自己用springmvc框架有一段时间了,但是都还一直分不清楚什么时候应该用转发,什么时候应该用重定向.可能用转发的情形太多了,以致于自己都忘记了还有重定向. 当用户提交post请求之后,刷新页面就会 ...
- SpringMVC表当重复提交
最近公司上线,有同志进行攻击,表当防重复提交也没有弄,交给我 ,本人以前也没弄过,知道大概的思路,但是那样实在是太麻烦了,虽然后面试过使用过滤器加拦截器实现,不过还是有点小麻烦. 后来在网上搜索后发现 ...
- SpringMVC防止表单重复提交
最近公司上线,有同志进行攻击,表当防重复提交也没有弄,交给我 ,本人以前也没弄过,知道大概的思路,但是那样实在是太麻烦了,虽然后面试过使用过滤器加拦截器实现,不过还是有点小麻烦. 后来在网上搜索后发现 ...
- 拦截器springmvc防止表单重复提交【3】3秒后自动跳回首页【重点明白如何跳转到各自需要的页面没有实现 但是有思路】
[1]定义异常类 [重点]:异常类有个多参数的构造函数public CmsException(String s, String... args),可以用来接受多个参数:如(“异常信息”,“几秒跳转”, ...
随机推荐
- idea设置方法注释
1. File-->Settings-->Editor-->Live Templates 1.1 新建Template Group: 命名为 methodNote 1.2 在meth ...
- Eureka【启用https】
上一篇主要说的是开启http basic认证,从安全角度来讲,基于base64编码,容易被抓包后破解,在公网中很不安全,本文详谈如何在eureka server和eureka client中开启htt ...
- xpath的一些常用使用
xml文档<html> <head> <title>My page</title> </head> <body> <h2& ...
- Linux 创建用户 用户组 用户权限
首先 你要有个root账号 然后才能做下面几条操作: useradd username 创建用户usernamepasswd user_pwd 给已创建的用户username设置密码 关于us ...
- Java多线程(十):BlockingQueue实现生产者消费者模型
BlockingQueue BlockingQueue.解决了多线程中,如何高效安全"传输"数据的问题.程序员无需关心什么时候阻塞线程,什么时候唤醒线程,该唤醒哪个线程. 方法介绍 ...
- 【原创】大叔经验分享(81)marathon上app无法重启
通过api调用marathon重启app后出现deployment,但是app不会重启,配置如下: "constraints": [ [ "hostname", ...
- win10下 安装迅雷精简版
下载链接:https://files-cdn.cnblogs.com/files/del88/ThunderMini_1.5.3.288.zip 他妈的 今天安装迅雷精简版 在win10上 竟然报错, ...
- 15 Django之Celery发送邮件
异步任务--celery发送邮件 安装两个python包: pip install celery==3.1.25 pip install django-celery==3.2.1 pip instal ...
- css3之媒体查询
<html> <head> <meta charset="utf-8"> <style> body{ background-colo ...
- Java秒杀实战 (四)JMeter压测
转自:https://blog.csdn.net/qq_41305266/article/details/81071278. 一.JMeter入门 下载链接 http://jmeter.apache. ...