Interceptor Springmvc prevent form repeat commit "2"

Source: Internet
Author: User

"Reference Blog: http://my.oschina.net/mushui/blog/143397" principle: In the new page in the session to save token random code, when saved to verify, after the deletion, When you click Save again because the server side of the session no longer exists, all can not be verified through.

1. New Annotations:

/** * <p> * 防止重复提交注解,用于方法上<br/> * 在新建页面方法上,设置needSaveToken()为true,此时拦截器会在Session中保存一个token, * 同时需要在新建的页面中添加 * <input type="hidden" name="token" value="${token}"> * <br/> * 保存方法需要验证重复提交的,设置needRemoveToken为true * 此时会在拦截器中验证是否重复提交 * </p> * @author: chuanli * @date: 2013-6-27上午11:14:02 * */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface AvoidDuplicateSubmission {    boolean needSaveToken() default false;    boolean needRemoveToken() default false;}

2. New Interceptor

/** * <p> * Prevent duplicate submission filter * </p> * *@author: Chuanli *@date: 2013-6-27 11:19:05 * *PublicClass Avoidduplicatesubmissioninterceptor Extends Handlerinterceptoradapter{private staticFinal Logger LOG = Logger.getlogger (avoidduplicatesubmissioninterceptor.Class);@Overridepublic boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) throws Exception {use R user = Userutil.getuser ();if (User! =NULL) {Handlermethod Handlermethod = (handlermethod) handler; Method method = Handlermethod.getmethod (); Avoidduplicatesubmissionannotation = Method.getannotation (avoidduplicatesubmission.Class);if (Annotation! =NULL) {Boolean needsavesession =Annotation.needsavetoken ();if (needsavesession) {request.getsession (False). SetAttribute ("Token", Tokenprocessor.getinstance (). Generatetoken ()); } Boolean needremovesession =Annotation.needremovetoken ();if (needremovesession) {if (Isrepeatsubmit (Request)) {Log.warn ("Please don ' t repeat Submit,[user:" + user.getusername () +", url:" + request.getservletpath () +"]");ReturnFalse } request.getsession (False). RemoveAttribute ("token"); } } }Returntrue;} Private Boolean Isrepeatsubmit (HttpServletRequest request) {String servertoken = (string) request.getsession (  False). getattribute ("token"); if (Servertoken = = null) { return true;} String Clinettoken = Request.getparameter ("token"); if (Clinettoken = = null) { return true;} if (!servertoken.equals (Clinettoken)) { return true;} return false;}}                 

3. Configure in Spring

<beanclass="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" ><Property Name="Interceptors"> <List> <Bean Class="Com.sohu.tv.crm.aop.UserLogInterceptor"/> <Bean Class="Com.sohu.tv.crm.aop.AvoidDuplicateSubmissionInterceptor"/> </list> </Property></bean> 

4. Add annotations to the relevant methods:

@RequestMapping("/save") @AvoidDuplicateSubmission(needRemoveToken = true)    public synchronized ModelAndView save(ExecutionUnit unit, HttpServletRequest request, HttpServletResponse response)            throws Exception {@RequestMapping("/edit") @AvoidDuplicateSubmission(needSaveToken = true) public ModelAndView edit(Integer id, HttpServletRequest request) throws Exception {

5. Add <input type= "hidden" name= "token" value= "${token" in the new page >

Interceptor Springmvc prevent form repeat commit "2"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.