wuyouzhuguli/SpringAll · warning · ValidateCodeException

验证码不存在!

Error message

验证码不存在!

What it means

ValidateCodeException from the 37.Spring-Security-RememberMe filter when the session holds no ImageCode (codeInSession == null). This is a session-state mismatch: no captcha exists for the current session at submit time.

Source

Thrown at 37.Spring-Security-RememberMe/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 in the session - the /code/image endpoint was never called for this session, or a different session is submitting.

Common situations: JSESSIONID cookie blocked so each request is a new session; remember-me flow split across sessions; captcha image never requested; session expired before submit; clustered deployment without shared sessions.

Related errors


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