apolloconfig/apollo · error · BadRequestException

operator should not be null or empty

Error message

operator should not be null or empty

What it means

Thrown by UserTokenService.validateOperator() when the operator string is blank (null, empty, or whitespace-only, per StringUtils.isBlank). validateOperator() is called at the start of nearly every public method: createToken, findUserTokens, revokeToken, deleteToken, rotateToken, revokeTokenForAdmin, deleteTokenForAdmin. BadRequestException → HTTP 400.

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java:412

    }

    Calendar maxCalendar = Calendar.getInstance();
    maxCalendar.setTime(now);
    maxCalendar.add(Calendar.DAY_OF_YEAR, portalConfig.userTokenMaxExpireDays());
    if (expires.after(maxCalendar.getTime())) {
      throw new BadRequestException("Token expires exceeds max allowed days:%s",
          portalConfig.userTokenMaxExpireDays());
    }
    return expires;
  }

  private int resolveRateLimit(Integer rateLimit) {
    return rateLimit == null ? 0 : rateLimit;
  }

  private void validateOperator(String operator) {
    if (StringUtils.isBlank(operator)) {
      throw new BadRequestException("operator should not be null or empty");
    }
  }

  private UserTokenInfo toInfo(UserToken userToken) {
    return toInfo(userToken, new Date());
  }

  private UserTokenInfo toInfo(UserToken userToken, Date now) {
    UserTokenScope scope = parseScope(userToken);
    UserTokenInfo info = new UserTokenInfo();
    info.setId(userToken.getId());
    info.setUserId(userToken.getUserId());
    info.setName(userToken.getName());
    info.setTokenPrefix(userToken.getTokenPrefix());
    info.setStatus(resolveStatus(userToken, now));
    info.setOperations(scope.getOperations());
    info.setAppIds(scope.getAppIds());
    info.setEnvs(scope.getEnvs());

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Ensure the operator parameter is the authenticated user's userId, resolved from Spring Security context or the request.
  2. Add a null/blank check before calling UserTokenService methods and fail early with a clear message.
  3. For system-level operations, use a dedicated service account ID rather than leaving it blank.

Example fix

// before
userTokenService.createToken(request, "");
// after
String operator = userInfoHolder.getUser().getUserId();
userTokenService.createToken(request, operator);
Defensive patterns

Strategy: validation

Validate before calling

if (StringUtils.isBlank(operator)) {
    throw new IllegalStateException("No authenticated user found in security context");
}
userTokenService.createToken(request, operator);

Type guard

static boolean hasValidOperator(String operator) {
    return !StringUtils.isBlank(operator);
}

Try / catch

try {
    userTokenService.createToken(request, operator);
} catch (BadRequestException e) {
    if (e.getMessage().contains("operator should not be null or empty")) {
        return Response.status(400).entity("Authentication context missing operator identity").build();
    }
    throw e;
}

Prevention

When it happens

Trigger: Any call to UserTokenService that accepts an operator parameter where the operator is null, empty string, or whitespace. This includes programmatic calls where the caller failed to resolve the current user ID from the security context.

Common situations: The portal's request interceptor or security filter that normally injects the operator (current user) fails or is bypassed. A scheduled job or integration calls the service without a user context. The operator string was accidentally set to an empty variable.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/3b3a219543143830. Report an issue: GitHub.