jcaptcha集羣時驗證碼不能驗證的問題

經過研讀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
		
	}
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章