自定義攔截器實現(xiàn)防止重復(fù)提交_第1頁
自定義攔截器實現(xiàn)防止重復(fù)提交_第2頁
自定義攔截器實現(xiàn)防止重復(fù)提交_第3頁
自定義攔截器實現(xiàn)防止重復(fù)提交_第4頁
自定義攔截器實現(xiàn)防止重復(fù)提交_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、由于struts2標(biāo)簽的性能不好,項目組決定不使用,但是如果用struts2自帶的攔截器防止重復(fù)提交又必須struts標(biāo)簽,所以只好自定攔器實現(xiàn),具體步驟如下:新建攔截器類:public class TokenAtionInterceptor extends AbstractInterceptor public String intercept(ActionInvocation invocation) throws Exception Map<String, Object> session = invocation.getInvocationContext().getSession

2、();HttpServletRequest request = ServletActionContext.getRequest();String strGUID = RandomGUIDUtil.newGuid();/生成令牌String strRequestToken = (String)session.get("request_token");/取出會話中的令牌String strToken = request.getParameter("token");/頁面中的令牌if(strRequestToken != null && !st

3、rRequestToken.equals(strToken)/重復(fù)提交,重置令牌session.put("request_token", strGUID);request.setAttribute("token", strGUID);return "invalidToken"session.put("request_token", strGUID);request.setAttribute("token", strGUID);return invocation.invoke();/否則正常運行建

4、一個生成令牌的工具類:public class RandomGUIDUtil extends Object /* 日志管理類對象 */ private Logger logger = Logger.getLogger(this.getClass().getName(); public String valueBeforeMD5 = "" public String valueAfterMD5 = "" private static Random myRand; private static SecureRandom mySecureRand; priva

5、te static String s_id; /* * 靜態(tài)塊初始化 */ static mySecureRand = new SecureRandom(); long secureInitializer = mySecureRand.nextLong(); myRand = new Random(secureInitializer); try s_id = InetAddress.getLocalHost().toString(); catch (UnknownHostException e) System.out.println("構(gòu)造帶指定詳細(xì)消息和嵌入異常!");

6、public RandomGUIDUtil() throws Exception getRandomGUID(false); public RandomGUIDUtil(boolean secure) throws Exception getRandomGUID(secure); /* * 隨機(jī)生成GUID */ private void getRandomGUID(boolean secure) throws Exception MessageDigest md5 = null; StringBuffer sbValueBeforeMD5 = new StringBuffer(); try

7、md5 = MessageDigest.getInstance("MD5"); catch (NoSuchAlgorithmException e) logger.error("初始化MD5出錯!"); throw new NoSuchAlgorithmException("初始化MD5出錯!"); try long time = System.currentTimeMillis(); long rand = 0; if (secure) rand = mySecureRand.nextLong(); else rand = myRa

8、nd.nextLong(); sbValueBeforeMD5.append(s_id); sbValueBeforeMD5.append(":"); sbValueBeforeMD5.append(Long.toString(time); sbValueBeforeMD5.append(":"); sbValueBeforeMD5.append(Long.toString(rand); valueBeforeMD5 = sbValueBeforeMD5.toString(); md5.update(valueBeforeMD5.getBytes();

9、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.toString(); catch (Exception e) logger.error("獲得MD5加密碼出錯!"); th

10、row new Exception("初始化MD5出錯!"); /* * 生成一個GUID串 * return GUID * throws Exception */ public static String newGuid() throws Exception RandomGUIDUtil rdmGUID = new RandomGUIDUtil(); return rdmGUID.toString(); /* * 生成以主機(jī)串號、顯卡串號、主板號,以保證唯一性的32位編碼 */ public String toString() String raw = valueAfte

11、rMD5.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配置文件中對應(yīng)的包下定義攔截器,注意順序,package里元素必須按照一定的順序排列。這個順序 就是 resu

12、lt-types interceptors default-interceptor-ref default-action-ref default-class-ref global-results global-exception-mappings action*(就是所有的action放到最后)<action>配置中加上名為” invalidToken”的result,就是攔截器中,重復(fù)提交跳轉(zhuǎn)的路徑如:<package name="template" extends="struts-default" namespace="/

13、"><interceptors><interceptor name="invocationToken" class="com.tydic.ppm.util.TokenAtionInterceptor"></interceptor></interceptors><global-results><result name="error">/error.jsp</result><result name="openError&qu

14、ot;>/openError.jsp</result></global-results><action name="addTemplate" class="templateConfigAction"> <interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="invocationToken"></interceptor-ref><result name="input">/content/templateManage/configTemplate/templateBaseInfo.jsp</result><result name="success">/content/templateManage/configTemplate/templateBaseInfo.jsp</result><result name="invalidToken">/content/templateManag

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論