jcaptcha集群时验证码不能验证的问题

简介: 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
    
  }
}
目录
相关文章
|
2月前
|
JavaScript NoSQL Redis
Vue中实现修改邮箱、手机号等流程的大致过程、验证码由后端的redis生成验证(版本1.0)
这篇文章记录了在Vue中实现修改手机号和邮箱的大致流程,包括使用过滤器部分隐藏展示的手机号和邮箱,以及通过点击触发路由跳转的便捷方式。文章还描述了旧号码和新号码验证的界面实现,其中验证码由后端生成并通过弹窗展示给用户,未来可以接入真正的手机验证码接口。此外,还提供了修改邮箱的页面效果截图,并强调了学习是一个永无止境的过程。
Vue中实现修改邮箱、手机号等流程的大致过程、验证码由后端的redis生成验证(版本1.0)
|
9天前
|
存储 前端开发 Java
验证码案例 —— Kaptcha 插件介绍 后端生成验证码,前端展示并进行session验证(带完整前后端源码)
本文介绍了使用Kaptcha插件在SpringBoot项目中实现验证码的生成和验证,包括后端生成验证码、前端展示以及通过session进行验证码校验的完整前后端代码和配置过程。
11 0
验证码案例 —— Kaptcha 插件介绍 后端生成验证码,前端展示并进行session验证(带完整前后端源码)
|
5月前
|
JavaScript Java
java生成验证码并进行验证
java生成验证码并进行验证
|
2月前
|
存储 NoSQL Java
使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
该博客文章展示了如何在Linux虚拟机上使用Redis和Jedis客户端实现手机验证码的验证功能,包括验证码的生成、存储、验证以及限制每天发送次数的逻辑,并提供了测试结果截图。
使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
|
4月前
|
前端开发 JavaScript
阿里云验证码2.0 验证时报错 前端页面获取的验证参数有问题,动态JS加载失败,请问怎么解决啊?急,急,急。
用户反馈校验时遇到错误,日志显示验证码参数获取异常。采用无痕验证,失败后,返回`{captchaResult:false,bizResult:false}`,未触发滑块二次验证。
|
安全 开发工具 UED
告别验证码烦恼,轻松完成文字点选验证
文字点选验证码,作为一种创新的验证方式,正在逐渐取代传统的输入文字或数字的验证方式,为用户带来了更简单、直观的验证体验。它通过点击包含特定文字或物体的图片来完成验证,摆脱了繁琐的输入过程,让用户告别验证码的烦恼。
|
前端开发
图形验证码验证行式的笔记
最近在做关于验证码项目的时候,从交互的角度梳理了验证码的验证行式,今天总结一下验证码在产品中出现的验证行式。
图形验证码验证行式的笔记
|
安全 UED
行为验证码验证类型的讲解
最近在开发行为验证码,经常触及到关于验证类型的相关内容。但使用起来不太熟练,闲暇之余,总结一下我对行为验证码验证类型的理解。
行为验证码验证类型的讲解
KgCaptcha 图形验证码自定义验证行式
图形验证码是一种很常见的行为验证码,其中滑动拼图,用户只需要轻轻滑动滑块填充拼图,即可完成安全验证。通常包括嵌入式、触发式和弹出式三种形式。
KgCaptcha 图形验证码自定义验证行式
|
搜索推荐 前端开发 API
这个验证码合集,从图形到行为验证,你想要的都有-KgCaptcha
凯格行为验证码 - KgCaptcha,采用业界通用的API接口方式,对接轻松简单,即可享受带来的产品服务能力。自定义样式及风控等级,完全个性化的设置,与你的应用完美融合。
这个验证码合集,从图形到行为验证,你想要的都有-KgCaptcha