wuyouzhuguli/SpringAll · warning · ValidateCodeException

验证码不存在!

Error message

验证码不存在!

What it means

ValidateCodeException from the image-captcha ValidateCodeFilter in module 38 when the session ImageCode is null (codeInSession == null). Indicates the captcha was never generated for this session or the session changed between generation and submit.

Source

Thrown at 38.Spring-Security-SmsCode/src/main/java/cc/mrbird/validate/code/ValidateCodeFilter.java:51

            try {
                validateCode(new ServletWebRequest(httpServletRequest));
            } catch (ValidateCodeException e) {
                authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, e);
                return;
            }
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

    private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
        ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

        if (StringUtils.isBlank(codeInRequest)) {
            throw new ValidateCodeException("验证码不能为空!");
        }
        if (codeInSession == null) {
            throw new ValidateCodeException("验证码不存在!");
        }
        if (codeInSession.isExpire()) {
            sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
            throw new ValidateCodeException("验证码已过期!");
        }
        if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
            throw new ValidateCodeException("验证码不正确!");
        }
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

    }

}

View on GitHub (pinned to 614d2578d9)

Solutions

  1. Ensure GET /code/image is requested in the same session before login submit.
  2. Verify the JSESSIONID cookie is present on both requests.
  3. Use a shared session store (Redis) or sticky sessions in clusters.
  4. Check session timeout is sufficient.

Example fix

// before
POST /authentication/form  imageCode=ABCD   // no prior GET /code/image -> 不存在

// after
GET  /code/image           // populates session ImageCode
POST /authentication/form  imageCode=ABCD   // same JSESSIONID cookie
Defensive patterns

Strategy: retry

Validate before calling

// ensure a captcha exists in this session before allowing submit
fetch('/code/image', { credentials: 'same-origin' })
  .then(() => enableLoginForm());

Prevention

When it happens

Trigger: POST to login with a non-empty imageCode but no ImageCode object in the session - /code/image was not called for this session, or a different session is submitting.

Common situations: JSESSIONID cookie blocked; captcha image endpoint never hit; session expired before submit; clustered deployment without shared sessions; the image captcha was superseded by the SMS flow in testing.

Related errors


AI-assisted analysis of wuyouzhuguli/SpringAll@614d2578d9 (2026-08-14). Data as JSON: /api/errors/b8387e2c9d32e277. Report an issue: GitHub.