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
    
  }
}
目录
相关文章
|
24天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
16天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
20天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2577 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
18天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
3天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
2天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
163 2
|
20天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1576 16
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
22天前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
973 14
|
3天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
219 2
|
17天前
|
人工智能 开发框架 Java
重磅发布!AI 驱动的 Java 开发框架:Spring AI Alibaba
随着生成式 AI 的快速发展,基于 AI 开发框架构建 AI 应用的诉求迅速增长,涌现出了包括 LangChain、LlamaIndex 等开发框架,但大部分框架只提供了 Python 语言的实现。但这些开发框架对于国内习惯了 Spring 开发范式的 Java 开发者而言,并非十分友好和丝滑。因此,我们基于 Spring AI 发布并快速演进 Spring AI Alibaba,通过提供一种方便的 API 抽象,帮助 Java 开发者简化 AI 应用的开发。同时,提供了完整的开源配套,包括可观测、网关、消息队列、配置中心等。
734 9