yudaocode/SpringBoot-Labs · warning · java.lang.RuntimeException

小朋友,你没有登录哟!

Error message

小朋友,你没有登录哟!

What it means

Intentional demo exception in lab-71's UserController. getCurrentUser compares the Authorization header against the hard-coded token 'token001' issued by the login endpoint; any non-matching value throws a RuntimeException (HTTP 500 by default). Missing header never reaches this line — Spring rejects it with 400 because @RequestHeader is required.

Source

Thrown at lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.java:46

            return tokenMap;
        }
        throw new RuntimeException("小朋友,你的账号密码不正确哟!");
    }

    @GetMapping("/user/get-current")
    public Map<String, Object> getCurrentUser(@RequestHeader("Authorization") String authorization,
                                              @RequestParam("full") boolean full) {
        if ("token001".equals(authorization)) {
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", 1);
            // full 为 true 时,获得完整信息
            if (full) {
                userInfo.put("nickname", "芋道源码");
                userInfo.put("gender", 1);
            }
            return userInfo;
        }
        throw new RuntimeException("小朋友,你没有登录哟!");
    }

    @PostMapping("/user/update")
    public Boolean update(@RequestBody UserUpdateVO updateVO) {
        logger.info("[update][收到更新请求:{}]", updateVO.toString());
        return true;
    }

}

View on GitHub (pinned to 6c12efaed0)

Solutions

  1. POST /user/login with the demo credentials first and use the returned token verbatim in the Authorization header.
  2. If your client adds 'Bearer ', strip it or change the comparison to authorization.replace("Bearer ", "").
  3. Return 401 instead of a bare RuntimeException via @ControllerAdvice or ResponseStatusException.

Example fix

// before
if ("token001".equals(authorization)) { ... }
throw new RuntimeException("小朋友,你没有登录哟!");

// after — accept both raw and Bearer-prefixed tokens, correct status
String token = authorization.startsWith("Bearer ") ? authorization.substring(7) : authorization;
if ("token001".equals(token)) { ... }
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "未登录");
Defensive patterns

Strategy: validation

Validate before calling

// Obtain and normalize the token before calling
String token = loginAndGetToken(); // returns "token001" in this demo
if (token == null || token.isEmpty()) throw new IllegalStateException("not logged in");
String auth = token.startsWith("Bearer ") ? token : token; // demo expects raw value

Try / catch

try {
    return restTemplate.getForObject(url + "/user/get-current?full=true", Map.class,
            Collections.singletonMap("Authorization", "token001"));
} catch (HttpStatusCodeException e) {
    if (e.getStatusCode().is5xxServerError() && e.getResponseBodyAsString().contains("没有登录")) {
        // re-login and retry once
    }
    throw e;
}

Prevention

When it happens

Trigger: GET /user/get-current with an Authorization header whose value is not exactly 'token001' — e.g. a stale token, a typo, or a Bearer-prefixed value ('Bearer token001') which fails the strict equals.

Common situations: Calling get-current before login so no token is known; copying the token with extra whitespace or a 'Bearer ' prefix because real JWT flows use that format; restarting/resetting the demo and using an old token.

Related errors


AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14). Data as JSON: /api/errors/c6535a1785818bab. Report an issue: GitHub.