SPRINGMVC anti-duplication commit interception

Source: Internet
Author: User
Tags uuid

/** * created with intellij idea. * user: lijian * date:  14-7-11 * Time:  morning 10:38 * description : Prevent duplicate submission of data       http://hi.baidu.com/uvmfawxtxzbknqe/item/3c87d4220acba32442634acf * to change this  template use File | Settings | File Templates. */public  Class tokeninterceptor extends handlerinterceptoradapter {    private  static logger logger = loggerfactory.getlogger (Tokeninterceptor.class);      @Override     public boolean prehandle (httpservletrequest  Request, httpservletresponse response, object handler)  throws exception {         if  (Handler instanceof handlermethod)  {             handlermethod handlermethod =  (HandlerMethod)  handler ;            method method =  Handlermethod.getmethod ();            token  Annotation = method.getannotation (Token.class);             if  (annotation != null)  {                 boolean needSaveSession =  Annotation.save ();                 if  (needsavesession)  {                     request.getsession (False). SetAttribute ("token",  Uuid.randomuuid (). toString ());                 }                 boolean needRemoveSession =  Annotation.remove ();                 if  (needremovesession)  {                     if  (Isrepeatsubmit (request))  {                          return false;                     }                     request.getsession (False). RemoveAttribute (" Token ");                 }             }             return true;        } else {             return super.prehandle (Request, response,  handler);        }    }     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;    }}

Add a custom note @token (save=true) on the Request Path form page to add a unique value generated by the UUID//page Add <input type= "hidden" name= "Token" value= "${token}"/ >//need to add @token (remove=true) to the controller submitted at the time of submission

/** * Created with IntelliJ idea. * User:lijian * date:14-7-11 * Time: Morning 10:35 * To change this template use File | Settings | File Templates.     */@Target (Elementtype.method) @Retention (retentionpolicy.runtime) public @interface Token {/** * generates a unique UUID token value    * * @return * * Boolean save () default false; /** * Check token value, remove token value by check * * @return */boolean remove () default false;}

<!--Interceptor Configuration--><mvc:interceptors> <!--Configure token blocker to prevent users from repeating data--<mvc:interceptor> < ; mvc:mapping path= "/**"/> <bean class= "Com.xxxx.xxx.xxx.TokenInterceptor"/> </mvc:interceptor>< ;/mvc:interceptors>

Files placed in the configuration file

SPRINGMVC anti-duplication commit interception

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.