自定义拦截器实现防止重复提交_第1页
自定义拦截器实现防止重复提交_第2页
自定义拦截器实现防止重复提交_第3页
自定义拦截器实现防止重复提交_第4页
自定义拦截器实现防止重复提交_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、由于struts2标签的性能不好,项目组决定不使用,但是如果用struts2自带的拦截器防止重复提交又必须struts标签,所以只好自定拦器实现,具体步骤如下:新建拦截器类:public class TokenAtionInterceptor extends AbstractInterceptor public String intercept(ActionInvocation invocation) throws Exception Map session = invocation.getInvocationContext().getSession();HttpServletRequest

2、request = ServletActionContext.getRequest();String strGUID = RandomGUIDUtil.newGuid();/生成令牌String strRequestToken = (String)session.get(request_token);/取出会话中的令牌String strToken = request.getParameter(token);/页面中的令牌if(strRequestToken != null & !strRequestToken.equals(strToken)/重复提交,重置令牌session.put(req

3、uest_token, strGUID);request.setAttribute(token, strGUID);return invalidToken;session.put(request_token, strGUID);request.setAttribute(token, strGUID);return invocation.invoke();/否则正常运行建一个生成令牌的工具类:public class RandomGUIDUtil extends Object /* 日志管理类对象 */ private Logger logger = Logger.getLogger(this.

4、getClass().getName(); public String valueBeforeMD5 = ; public String valueAfterMD5 = ; private static Random myRand; private static SecureRandom mySecureRand; private static String s_id; /* * 静态块初始化 */ static mySecureRand = new SecureRandom(); long secureInitializer = mySecureRand.nextLong(); myRand

5、 = new Random(secureInitializer); try s_id = InetAddress.getLocalHost().toString(); catch (UnknownHostException e) System.out.println(构造带指定详细消息和嵌入异常!); public RandomGUIDUtil() throws Exception getRandomGUID(false); public RandomGUIDUtil(boolean secure) throws Exception getRandomGUID(secure); /* * 随机

6、生成GUID */ private void getRandomGUID(boolean secure) throws Exception MessageDigest md5 = null; StringBuffer sbValueBeforeMD5 = new StringBuffer(); try md5 = MessageDigest.getInstance(MD5); catch (NoSuchAlgorithmException e) logger.error(初始化MD5出错!); throw new NoSuchAlgorithmException(初始化MD5出错!); try

7、 long time = System.currentTimeMillis(); long rand = 0; if (secure) rand = mySecureRand.nextLong(); else rand = myRand.nextLong(); sbValueBeforeMD5.append(s_id); sbValueBeforeMD5.append(:); sbValueBeforeMD5.append(Long.toString(time); sbValueBeforeMD5.append(:); sbValueBeforeMD5.append(Long.toString

8、(rand); valueBeforeMD5 = sbValueBeforeMD5.toString(); md5.update(valueBeforeMD5.getBytes(); byte array = md5.digest(); StringBuffer sb = new StringBuffer(); for (int j = 0; j array.length; +j) int b = arrayj & 0xFF; if (b 0x10) sb.append(0); sb.append(Integer.toHexString(b); valueAfterMD5 = sb.toStr

9、ing(); catch (Exception e) logger.error(获得MD5加密码出错!); throw new Exception(初始化MD5出错!); /* * 生成一个GUID串 * return GUID * throws Exception */ public static String newGuid() throws Exception RandomGUIDUtil rdmGUID = new RandomGUIDUtil(); return rdmGUID.toString(); /* * 生成以主机串号、显卡串号、主板号,以保证唯一性的32位编码 */ pub

10、lic String toString() String raw = valueAfterMD5.toUpperCase(); StringBuffer sb = new StringBuffer(); sb.append(raw.substring(0, 8); sb.append(raw.substring(8, 12); sb.append(raw.substring(12, 16); sb.append(raw.substring(16, 20); sb.append(raw.substring(20); return sb.toString(); 在struts配置文件中对应的包下定义拦截器,注意顺序,package里元素必须按照一定的顺序排列。这个顺序 就是 result-types interceptors default-interceptor-ref default-action-ref default-class

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论