wuyouzhuguli/SpringAll · error · ValidateCodeException

验证码不存在!

Error message

验证码不存在!

What it means

Thrown by SmsCodeFilter.validateCode (Logout project) when no SmsCode is in session under SESSION_KEY_SMS_CODE + mobileInRequest. CRITICAL BUG on line 46: mobileInRequest is read from the 'smsCode' param instead of 'mobile', so the session key is built from the SMS code value and will almost never match the stored SESSION_KEY_SMS_CODE+mobile key. This error thus fires on nearly every valid SMS login.

Source

Thrown at 60.Spring-Security-Logout/src/main/java/cc/mrbird/validate/smscode/SmsCodeFilter.java:54

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

    private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
        String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
        String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

        SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

        if (StringUtils.isBlank(smsCodeInRequest)) {
            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(), smsCodeInRequest)) {
            throw new ValidateCodeException("验证码不正确!");
        }
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

    }
}

View on GitHub (pinned to 614d2578d9)

Solutions

  1. Fix line 46 to read the 'mobile' parameter: ServletRequestUtils.getStringParameter(req, "mobile").
  2. Call GET /code/sms?mobile=<number> before POST /login/mobile to store the code.
  3. Ensure the submitted 'mobile' matches the one used to request the code.
  4. Preserve JSESSIONID across both requests.

Example fix

// before
String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

// after
String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "mobile");
Defensive patterns

Strategy: validation

Validate before calling

// client-side: request SMS with mobile, submit same mobile + smsCode
await fetch(`/code/sms?mobile=${encodeURIComponent(mobile)}`);

Try / catch

// after fixing line 46, treat '不存在' as '请先获取验证码' in AuthenticationFailureHandler

Prevention

When it happens

Trigger: Any SMS login due to the line-46 param bug; or legitimately when /code/sms was not called for that mobile, or the session expired.

Common situations: The copy-paste bug dominates; also mobile param mismatch, missing GET /code/sms?mobile=..., session loss.

Related errors


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