/** * 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