经过研读jcaptcha验证码实现的过程,生成的验证码存放在CaptchaStore中store中,store属于内部变量,当集群时,进行验证时,由A计算机到B计算机进行验证,B计算机CaptchaStore中store中得不到当前验证码,无法进行验证。所以想了想只有通过session来存取当前的验证码变量来实现。
重写CaptchaStore
package com.dzf.core.security.jcaptcha;
import java.util.Collection;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import com.octo.captcha.Captcha;
import com.octo.captcha.service.CaptchaServiceException;
public interface CaptchaStore{
public abstract boolean hasCaptcha(String paramString);
/** @deprecated */
public abstract void storeCaptcha(String paramString, Captcha paramCaptcha)
throws CaptchaServiceException;
public abstract void storeCaptcha(String paramString, Captcha paramCaptcha, Locale paramLocale)
throws CaptchaServiceException;
public abstract boolean removeCaptcha(String paramString);
public abstract Captcha getCaptcha(String paramString)
throws CaptchaServiceException;
public abstract Locale getLocale(String paramString)
throws CaptchaServiceException;
public abstract int getSize();
public abstract Collection getKeys();
public abstract void empty();
public abstract void initAndStart();
public abstract void cleanAndShutdown();
public abstract void setRequest(HttpServletRequest request);
public abstract HttpServletRequest getRequest();
}
/*
* JCaptcha, the open source java framework for captcha definition and integration
* Copyright (c) 2007 jcaptcha.net. All Rights Reserved.
* See the LICENSE.txt file distributed with this package.
*/
package com.dzf.core.security.jcaptcha;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.octo.captcha.Captcha;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.captchastore.CaptchaAndLocale;
/**
* Simple store based on a HashMap
*/
public class SessionCaptchaStore implements CaptchaStore {
HttpServletRequest request;
HttpSession store;
List<String> keySet;
public static String SESSIONCAPTCHA="session_captcha";
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public SessionCaptchaStore() {
this.keySet = new ArrayList<String>();
}
/**
* Check if a captcha is stored for this id
*
* @return true if a captcha for this id is stored, false otherwise
*/
public boolean hasCaptcha(String id) {
return request.getSession().getAttribute(SESSIONCAPTCHA+id)!=null;
}
/**
* Store the captcha with the provided id as key. The key is assumed to be unique, so if the same key is used twice
* to store a captcha, the store will return an exception
*
* @param id the key
* @param captcha the captcha
*
* @throws CaptchaServiceException if the captcha already exists, or if an error occurs during storing routine.
*/
public void storeCaptcha(String id, Captcha captcha) throws CaptchaServiceException {
keySet.add(SESSIONCAPTCHA+id);
request.getSession().setAttribute(SESSIONCAPTCHA+id, new CaptchaAndLocale(captcha));
}
/**
* Store the captcha with the provided id as key. The key is assumed to be unique, so if the same key is used twice
* to store a captcha, the store will return an exception
*
* @param id the key
* @param captcha the captcha
* @param locale the locale used that triggers the captcha generation
* @throws com.octo.captcha.service.CaptchaServiceException
* if the captcha already exists, or if an error occurs during storing routine.
*/
public void storeCaptcha(String id, Captcha captcha, Locale locale) throws CaptchaServiceException {
keySet.add(SESSIONCAPTCHA+id);
request.getSession().setAttribute(SESSIONCAPTCHA+id, new CaptchaAndLocale(captcha,locale));
}
/**
* Retrieve the captcha for this key from the store.
*
* @return the captcha for this id
*
* @throws CaptchaServiceException if a captcha for this key is not found or if an error occurs during retrieving
* routine.
*/
public Captcha getCaptcha(String id) throws CaptchaServiceException {
Object captchaAndLocale = request.getSession().getAttribute(SESSIONCAPTCHA+id);
return captchaAndLocale!=null?((CaptchaAndLocale) captchaAndLocale).getCaptcha():null;
}
/**
* Retrieve the locale for this key from the store.
*
* @return the locale for this id, null if not found
* @throws com.octo.captcha.service.CaptchaServiceException
* if an error occurs during retrieving routine.
*/
public Locale getLocale(String id) throws CaptchaServiceException {
Object captchaAndLocale = request.getSession().getAttribute(SESSIONCAPTCHA+id);
return captchaAndLocale!=null?((CaptchaAndLocale) captchaAndLocale).getLocale():null;
}
/**
* Remove the captcha with the provided id as key.
*
* @param id the key
*
* @return true if found, false otherwise
*
* @throws CaptchaServiceException if an error occurs during remove routine
*/
public boolean removeCaptcha(String id) {
if (request.getSession().getAttribute(SESSIONCAPTCHA+id) != null) {
keySet.remove(SESSIONCAPTCHA+id);
request.getSession().removeAttribute(SESSIONCAPTCHA+id);
return true;
}
return false;
}
/**
* get the size of this store
*/
public int getSize() {
return keySet.size();
}
/**
* Return all the contained keys
*/
public Collection getKeys() {
return keySet;
}
/**
* Empty the store
*/
public void empty() {
for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
String key = iterator.next();
keySet.remove(key);
request.getSession().removeAttribute(key);
}
}
public void cleanAndShutdown() {
// TODO Auto-generated method stub
}
public void initAndStart() {
// TODO Auto-generated method stub
}
}