{"record":{"id":"e2f1e290ddf338ce","repo":"apolloconfig/apollo","slug":"current-user-not-found-e2f1e2","errorCode":null,"errorMessage":"Current user not found","messagePattern":"Current user not found","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/NamespaceBranchController.java","lineNumber":271,"sourceCode":"  }\n\n  private void checkEmergencyPublishAllowedForUser(String env, boolean emergencyPublish) {\n    String authType = UserIdentityContextHolder.getAuthType();\n    if (emergencyPublish\n        && (UserIdentityConstants.USER.equals(authType)\n            || UserIdentityConstants.USER_TOKEN.equals(authType))\n        && !portalConfig.isEmergencyPublishAllowed(Env.valueOf(env))) {\n      throw new BadRequestException(\"Env: %s is not supported emergency publish now\", env);\n    }\n  }\n\n  private String resolveOperator(String queryOperator, String payloadOperator) {\n    String authType = UserIdentityContextHolder.getAuthType();\n    if (UserIdentityConstants.USER.equals(authType)\n        || UserIdentityConstants.USER_TOKEN.equals(authType)) {\n      UserInfo loginUser = userInfoHolder.getUser();\n      if (loginUser == null || StringUtils.isBlank(loginUser.getUserId())) {\n        throw new BadRequestException(\"Current user not found\");\n      }\n      return loginUser.getUserId();\n    }\n\n    if (UserIdentityConstants.CONSUMER.equals(authType)) {\n      String operator = StringUtils.isBlank(queryOperator) ? payloadOperator : queryOperator;\n      RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),\n          \"operator should not be null or empty\");\n      if (userService.findByUserId(operator) == null) {\n        throw BadRequestException.userNotExists(operator);\n      }\n      return operator;\n    }\n\n    throw new BadRequestException(\"Unsupported auth type: %s\", authType);\n  }\n\n  private boolean shouldHideConfigToCurrentUser(String appId, String env, String clusterName,","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/NamespaceBranchController.java#L253-L289","documentation":"HTTP 400 (BadRequestException). Thrown by NamespaceBranchController.resolveOperator when the auth type is USER or USER_TOKEN but UserInfoHolder.getUser() returns null or a UserInfo with a blank userId. The operator is normally derived from the authenticated principal, so this indicates the security context/principal was not populated correctly for a portal or user-token request.","triggerScenarios":"Any NamespaceBranch write/create/delete API call authenticated as USER or USER_TOKEN where the request reached the controller without a populated Spring Security principal — e.g. a misconfigured security filter chain, a user-token that authenticated but did not resolve to a UserInfo, or a session that expired mid-request.","commonSituations":"Portal SSO session expired between auth and controller invocation; a custom auth integration that sets authType=USER but forgets to populate UserInfoHolder; user-token with a corrupt/empty subject; running behind a gateway that strips the auth header.","solutions":["Re-authenticate: obtain a fresh portal session / user token and retry.","Verify the auth integration populates UserInfoHolder with a non-blank userId for USER/USER_TOKEN requests.","If calling as an OpenAPI consumer, authenticate as CONSUMER and supply an explicit operator instead.","Check the security filter chain ordering so the principal is set before the controller runs."],"exampleFix":"// before: USER_TOKEN auth but principal missing\nUserInfo user = userInfoHolder.getUser(); // null -> 400\n\n// after: ensure auth filter sets the principal\nif (user == null || StringUtils.isBlank(user.getUserId())) {\n    // re-auth or 401 before reaching resolveOperator\n}\n// or switch to CONSUMER auth and pass operator explicitly","handlingStrategy":"try-catch","validationCode":"// For USER/USER_TOKEN: ensure principal exists before any write.\nUserInfo u = userInfoHolder != null ? userInfoHolder.getUser() : null;\nif (u == null || StringUtils.isBlank(u.getUserId())) {\n  // re-authenticate; do not proceed to resolveOperator\n}","typeGuard":"null","tryCatchPattern":"try {\n  client.createBranch(appId, env, cluster, ns, operator);\n} catch (HttpClientErrorException.BadRequest e) {\n  if (e.getResponseBodyAsString().contains(\"Current user not found\")) {\n    refreshToken(); client.createBranch(appId, env, cluster, ns, operator);\n  }\n}","preventionTips":["Refresh portal/user-token sessions before they expire.","Ensure the auth filter populates UserInfoHolder for USER/USER_TOKEN.","Use CONSUMER auth with an explicit operator for headless automation."],"tags":["apollo-portal","openapi","auth","bad-request","operator","user"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}