/* * Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences. * * This file is part of Duckling project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package cn.vlabs.umt.services.captcha; import com.octo.captcha.engine.CaptchaEngine; import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.service.captchastore.CaptchaStore; import com.octo.captcha.service.multitype.GenericManageableCaptchaService; public class CustomGenericManageableCaptchaService extends GenericManageableCaptchaService { /** * The constructor method of class CustomManageableCaptchaService.java . * * @param captchaEngine * @param minGuarantedStorageDelayInSeconds * @param maxCaptchaStoreSize * @param captchaStoreLoadBeforeGarbageCollection */ public CustomGenericManageableCaptchaService( CaptchaEngine captchaEngine, int minGuarantedStorageDelayInSeconds, int maxCaptchaStoreSize, int captchaStoreLoadBeforeGarbageCollection) { super(captchaEngine, minGuarantedStorageDelayInSeconds, maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection); } /** * The constructor method of class CustomManageableCaptchaService.java . * * @param captchaStore * @param captchaEngine * @param minGuarantedStorageDelayInSeconds * @param maxCaptchaStoreSize * @param captchaStoreLoadBeforeGarbageCollection */ public CustomGenericManageableCaptchaService(CaptchaStore captchaStore, CaptchaEngine captchaEngine, int minGuarantedStorageDelayInSeconds, int maxCaptchaStoreSize, int captchaStoreLoadBeforeGarbageCollection) { super(captchaStore, captchaEngine, minGuarantedStorageDelayInSeconds, maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection); } /** * 修改验证码校验逻辑,默认的是执行了该方法后,就把sessionid从store当中移除<br/> * 然而在ajax校验的时候,如果第一次验证失败,第二次还得重新刷新验证码,这种逻辑不合理<br/> * 现在修改逻辑,只有校验通过以后,才移除sessionid。 Method Name:validateResponseForID . * * @param ID * @param response * @return * @throws CaptchaServiceException * the return type:Boolean */ @Override public Boolean validateResponseForID(String ID, Object response) throws CaptchaServiceException { if (!this.store.hasCaptcha(ID)) { throw new CaptchaServiceException( "Invalid ID, could not validate unexisting or already validated captcha"); } Boolean valid = this.store.getCaptcha(ID) .validateResponse(response); // 源码的这一句是没被注释的,这里我们注释掉,在下面暴露一个方法给我们自己来移除sessionId // this.store.removeCaptcha(ID); return valid; } /** * 移除session绑定的验证码信息. Method Name:removeCaptcha . * * @param sessionId * the return type:void */ public void removeCaptcha(String sessionId) { if (sessionId != null && this.store.hasCaptcha(sessionId)) { this.store.removeCaptcha(sessionId); } } }