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
		
	}
}
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值