jeecgboot/JeecgBoot · error · JeecgBootException

未获取到用户

Error message

未获取到用户

What it means

JwtUtil.getUserNameByToken extracts the X-Access-Token header, decodes the JWT, and reads the username claim. If the decoded token yields an empty/null username (token malformed, missing username claim, or decode failure upstream), it throws JeecgBootException('未获取到用户'). This is an authentication/integrity guard: a request that presents a token but yields no principal must not proceed.

Source

Thrown at jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/JwtUtil.java:201

			return oConvertUtils.isNotEmpty(clientType) ? clientType : CommonConstant.CLIENT_TYPE_PC;
		} catch (JWTDecodeException e) {
			log.warn("解析token中的clientType失败,使用默认值PC:" + e.getMessage());
			return CommonConstant.CLIENT_TYPE_PC;
		}
	}

	/**
	 * 根据request中的token获取用户账号
	 * 
	 * @param request
	 * @return
	 * @throws JeecgBootException
	 */
	public static String getUserNameByToken(HttpServletRequest request) throws JeecgBootException {
		String accessToken = request.getHeader("X-Access-Token");
		String username = getUsername(accessToken);
		if (oConvertUtils.isEmpty(username)) {
			throw new JeecgBootException("未获取到用户");
		}
		return username;
	}
	
	/**
	  *  从session中获取变量
	 * @param key
	 * @return
	 */
	public static String getSessionData(String key) {
		//${myVar}%
		//得到${} 后面的值
		String moshi = "";
		String wellNumber = WELL_NUMBER;

		if(key.indexOf(SymbolConstant.RIGHT_CURLY_BRACKET)!=-1){
			 moshi = key.substring(key.indexOf("}")+1);
		}

View on GitHub (pinned to 96fb33f5ec)

Solutions

  1. Have the client re-authenticate to obtain a fresh token.
  2. Verify the JWT signing secret (application.yml: jeecg.jwt.secret) matches across all nodes/services that mint and validate tokens.
  3. Confirm the username claim name in the token matches what JwtUtil.getUsername expects.
  4. Decode the token at jwt.io to confirm the payload contains the username claim.
Defensive patterns

Strategy: try-catch

Validate before calling

// Decode and check the username claim before trusting the token
import io.jsonwebtoken.Jwts;
Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
if (claims.get("username") == null) {
  throw new AuthException("Token missing username claim");
}

Type guard

public static boolean tokenHasUsername(String token) {
  try { return JwtUtil.getUsername(token) != null; }
  catch (Exception e) { return false; }
}

Try / catch

try {
  String username = JwtUtil.getUserNameByToken(request);
} catch (JeecgBootException e) {
  // token invalid/expired -> return 401
  response.sendError(401, "Invalid token");
}

Prevention

When it happens

Trigger: A request carries an X-Access-Token that is expired, tampered, signed with a different key, or correctly signed but missing the username claim. Also when the token format changed (custom claim name) and JwtUtil.getUsername still reads the old claim path.

Common situations: Token expiry window edge cases; token generated by a different deployment with a different secret; a token minted by an older code version using a different username claim key; frontend sending a stale token from localStorage after a server-side key rotation.

Related errors


AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14). Data as JSON: /api/errors/ab64b6d5dbe73cbe. Report an issue: GitHub.