{"record":{"id":"43fc4bc5ab7eb4e0","repo":"apolloconfig/apollo","slug":"user-token-not-found-for-id-s","errorCode":null,"errorMessage":"user token not found for id:%s","messagePattern":"user token not found for id:(.+?)","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"warning","filePath":"apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java","lineNumber":271,"sourceCode":"    if (userPermissionValidator.hasManageUsersPermission()) {\n      operations.add(UserTokenOperation.USER_MANAGE);\n    }\n    if (userPermissionValidator.isSuperAdmin()) {\n      operations.add(UserTokenOperation.SYSTEM_ADMIN);\n    }\n    return operations;\n  }\n\n  @Transactional\n  public void createUserTokenAudits(Iterable<UserTokenAudit> userTokenAudits) {\n    userTokenAuditRepository.saveAll(userTokenAudits);\n  }\n\n  private UserToken findOwnedToken(long tokenId, String operator) {\n    validateOperator(operator);\n    UserToken userToken = userTokenRepository.findByIdAndUserId(tokenId, operator);\n    if (userToken == null) {\n      throw new NotFoundException(\"user token not found for id:%s\", tokenId);\n    }\n    return userToken;\n  }\n\n  private UserToken findToken(long tokenId) {\n    return userTokenRepository.findById(tokenId)\n        .orElseThrow(() -> new NotFoundException(\"user token not found for id:%s\", tokenId));\n  }\n\n  private String normalizeStatus(String status) {\n    if (StringUtils.isBlank(status)) {\n      return TOKEN_STATUS_ALL;\n    }\n    String normalizedStatus = status.trim().toLowerCase(Locale.ROOT);\n    if (TOKEN_STATUS_ALL.equals(normalizedStatus) || TOKEN_STATUS_ACTIVE.equals(normalizedStatus)\n        || TOKEN_STATUS_EXPIRED.equals(normalizedStatus)\n        || TOKEN_STATUS_REVOKED.equals(normalizedStatus)) {\n      return normalizedStatus;","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java#L253-L289","documentation":"Thrown as a NotFoundException by UserTokenService.findOwnedToken() or findToken() when a UserToken with the given tokenId cannot be found. findOwnedToken also scopes by userId (findByIdAndUserId), so the token must both exist and belong to the operator. findToken uses findById and throws if the Optional is empty. Both indicate the token ID does not correspond to any persisted token (or not one owned by the caller).","triggerScenarios":"Calling any operation that resolves a token by ID (rotateToken, revokeToken, etc.) with a tokenId that doesn't exist in the user_token table, or — for findOwnedToken — exists but belongs to a different user than the operator.","commonSituations":"Token was already deleted; token ID typo in the request; attempting to operate on another user's token (ownership mismatch); token ID from a different Apollo environment/deployment; stale client reference to a deleted token.","solutions":["List the current user's tokens via the token management API to find the correct tokenId.","Verify the tokenId belongs to the authenticated user (findOwnedToken enforces ownership).","If the token was deleted, it cannot be operated on — inform the user and create a new token if needed.","Ensure the tokenId is from the same Apollo environment/deployment."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify token exists and belongs to the user before operating\nUserToken token = userTokenRepository.findByIdAndUserId(tokenId, operator);\nif (token == null) {\n  throw new IllegalArgumentException(\n    \"Token not found or not owned by user: \" + tokenId);\n}","typeGuard":null,"tryCatchPattern":"try {\n  userTokenService.rotateToken(tokenId, operator);\n} catch (NotFoundException e) {\n  if (e.getMessage().contains(\"user token not found\")) {\n    log.warn(\"Token {} not found or not owned by {}\", tokenId, operator);\n    // refresh the token list in the UI\n  }\n  throw e;\n}","preventionTips":["List the user's tokens via the management API to find valid token IDs.","Refresh the token list in the UI if a token is not found — it may have been deleted.","Ensure the tokenId is from the same Apollo environment."],"tags":["apollo-portal","user-token","not-found","ownership","token-id"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}