在shopxx看到JCaptcha验证码的应用,感觉效果很好,就模仿着应用到一个步项目上,结果在验证的时候一直不成功,代码如下: public String userLogin() throws Exception {

String captchaID = getRequest().getSession().getId();
System.out.println("captchaID==="+captchaID);
String challengeResponse = StringUtils.upperCase(getRequest().getParameter(JCaptchaEngine.CAPTCHA_INPUT_NAME));
System.out.println("challengeResponse is =="+challengeResponse);boolean b = false;  b = captchaService.validateResponseForID(captchaID, challengeResponse);
  
System.out.println("b is .... "+b);if(b == true){
System.out.println("captchaID == challengeResponse");
}else{
System.out.println("challengeResponse != captchaID");
}
System.out.println("没过验证码");
if (captchaService.validateResponseForID(captchaID, challengeResponse) != true) {
System.out.println("正在验证中");
loginMessage = "验证码输入错误!";
return SUCCESS;
}
System.out.println("过验证码");
User u = this.userService.getUserByusername(this.getUsername());
if (u != null) {
if (!(DigestUtils.md5Hex(this.getPassword())).equals(u
.getPassword())) {
loginMessage = "密码不正确";
return SUCCESS;
}
// 必须实现SessionAware接口才能生效
session.put(Constant.USER_SESSION_KEY, u);
loginMessage = "";
return SUCCESS;
} else {
loginMessage = "用户名不正确";
return SUCCESS;
}
}Console打印如下(先输入一次正确的验证码,后来又输入一次错误的):captchaID===86C7CC75394980E6CE55CA29476497E8
challengeResponse is ==PSYK
b is .... true
captchaID == challengeResponse
没过验证码
===========================================(下面是错误的,就这两行)
captchaID===86C7CC75394980E6CE55CA29476497E8
challengeResponse is ==FFF
请大家帮忙看下,因为我是第一次用JCaptcha,配置方面也不知道有没有遗漏,请知道的大哥说下使用JCaptcha需要在哪几个文件配置(我已经在applicationContext.xml和web.xml配置)。 或者说下我发出的那个action方法有没有不对的地方,谢谢了。。

解决方案 »

  1.   

    没弄明白你的问题在哪里System.out.println("没过验证码");
    这句总会执行的还有JCaptcha的码是一次性的,验证完后就会失效,不管是否通过还有你这程序写得
    if(b == true){//怎么都爱这么写的
    .....
    }
      

  2.   


    我那是测试,因为老是返回不了我想要的内容,我想看下代码到底是执行到哪一行才出问题的。 后来发现是captchaService.validateResponseForID(captchaID, challengeResponse)这个方法, 输入正确的时候,他就会执行下一行,如果输入不正确时就不执行了。但是我要的效果是输入正确就return true,否则就reutrn false
      

  3.   

    好奇怪,当输入错误验证码时,captchaService.validateResponseForID(captchaID, challengeResponse)这个方法为什么不能反正false
      

  4.   

    没执行那就是执行出异常了
    captchaService.validateResponseForID(captchaID, challengeResponse)
    这个方法catch下Invalid ID, could not validate unexisting or already validated captcha
    你看看是不是这个异常信息,如果是,就是sessionid的问题当第一次输入正确的时候,验证通过,此时,captcha会把captchaID(就是你的sessionid)从内存中清除掉(当然不是只有正确的时候才清除,只要执行这方法就会清的),当你想再次通过这个captchaID去验证的时候就报错了
      

  5.   

    这个问题解决了,因为我在这个action里面写了这个方法:
    public HttpServletRequest getRequest() {
    return ServletActionContext.getRequest();
    }导致json请求老是失败,我现在把这个方法写在BaseAction里面,然后这个Action去继承BaseAction,不过到现在我也不明白为什么要这么做。
    结贴了,谢谢大家的帮助。。