Jcaptcha集群环境验证失败解决办法

问题描述:
线上集群环境,前端可能从A服务器取得验证码,而验证是到B服务器
默认的hashmap store是保存在单个Jvm内存中的,这样验证就会有问题
 

解决办法:

重写CaptchaStore,把信息存在缓存中(memcached或redis)

1.重写 MyCaptchaStore 实现 CaptchaStore

public class MyCaptchaStore implements CaptchaStore {

    @Autowired
	private MemcachedService memcachedService;

    @Override
    public boolean hasCaptcha(String id) {
        CaptchaAndLocale captcha = (CaptchaAndLocale) memcachedService.get(id);
        return captcha == null ? false : true;
    }

    @Override
    public void storeCaptcha(String id, Captcha captcha) throws CaptchaServiceException {
        try {
        	memcachedService.add(id,new CaptchaAndLocale(captcha));
        } catch (Exception e) {
            throw new CaptchaServiceException(e);
        }
    }

  
    @Override
    public void storeCaptcha(String id, Captcha captcha, Locale locale) throws CaptchaServiceException {
        try {
        	memcachedService.add(id,new CaptchaAndLocale(captcha,locale));
        } catch (Exception e) {
            throw new CaptchaServiceException(e);
        }

    }

  

    @Override
    public boolean removeCaptcha(String id) {
    	memcachedService.remove(id);
        return true;
    }

    @Override
    public Captcha getCaptcha(String id) throws CaptchaServiceException {
        CaptchaAndLocale captchaAndLocale = (CaptchaAndLocale) memcachedService.get(id);
        return captchaAndLocale != null ? (captchaAndLocale.getCaptcha()) : null;
    }

  

    @Override
    public Locale getLocale(String id) throws CaptchaServiceException {
        CaptchaAndLocale captchaAndLocale = (CaptchaAndLocale) memcachedService.get(id);
        return captchaAndLocale != null ? (captchaAndLocale.getLocale()) : null;
    }

  
    @Override
    public int getSize() {
        return 0;
    }

  
    @Override
    public Collection getKeys() {
        return null;
    }

  
    @Override
    public void empty() {
    	
    }

    @Override
    public void initAndStart() {

    }


    @Override
    public void cleanAndShutdown() {

    }

}

2.配置spring.xml,添加

<!-- 验证码配置 -->
	<bean id="captchaService"
		class="com.octo.captcha.service.multitype.GenericManageableCaptchaService">
		 <constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">	
	        <ref bean="myCaptchaStore"/>	
	    </constructor-arg>
		
		<constructor-arg index="1" ref="imageEngine" />
		<constructor-arg type="int" index="2" value="180" />
		<constructor-arg type="int" index="3" value="100000" />
		<constructor-arg type="int" index="4" value="75000" />
	</bean>


    <bean id="myCaptchaStore" class="com.xxxx.internal.MyCaptchaStore"/>

参考文章:http://zhanshenny.iteye.com/blog/2098779

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值