{"record":{"id":"3041325ccc4a41f5","repo":"alibaba/spring-ai-alibaba","slug":"oauth2usernotfound","errorCode":"Oauth2UserNotFound","errorMessage":"Oauth2 user can not be found.","messagePattern":"Oauth2 user can not be found\\.","errorType":"error_code","errorClass":"BizException","httpStatus":404,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/AccountServiceImpl.java","lineNumber":144,"sourceCode":"\t * @param refreshTokenRequest Refresh token request\n\t * @return New token response\n\t */\n\t@Override\n\tpublic TokenResponse refreshToken(RefreshTokenRequest refreshTokenRequest) {\n\t\tString accountId = tokenManager.getAccountIdFromRefreshToken(refreshTokenRequest.getRefreshToken());\n\t\tif (accountId == null) {\n\t\t\tthrow new BizException(ErrorCode.INVALID_REFRESH_TOKEN.toError());\n\t\t}\n\n\t\tTokenResponse response = createTokenResponse(accountId);\n\t\ttokenManager.deleteRefreshToken(refreshTokenRequest.getRefreshToken());\n\t\treturn response;\n\t}\n\n\t@Override\n\tpublic TokenResponse login(Oauth2User oauth2User) {\n\t\tif (oauth2User == null || StringUtils.isBlank(oauth2User.getUserId())) {\n\t\t\tthrow new BizException(ErrorCode.OAUTH2_USER_NOT_FOUND.toError());\n\t\t}\n\n\t\tString accountId;\n\t\tAccountEntity accountEntity = getAccountByName(oauth2User.getUserId());\n\t\tif (accountEntity == null) {\n\t\t\tAccount account = new Account();\n\t\t\taccount.setUsername(oauth2User.getUserId());\n\t\t\taccount.setNickname(oauth2User.getName());\n\t\t\taccount.setEmail(oauth2User.getEmail());\n\t\t\taccount.setIcon(oauth2User.getIcon());\n\t\t\taccount.setPassword(IdGenerator.uuid32());\n\n\t\t\taccountId = registerAccount(account);\n\t\t\taccountEntity = getAccountById(accountId);\n\t\t}\n\t\telse {\n\t\t\taccountId = accountEntity.getAccountId();\n\t\t}","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/AccountServiceImpl.java#L126-L162","documentation":"Thrown by AccountServiceImpl.login(Oauth2User) when the OAuth2 user object is null or its userId is blank. The method has no identity to authenticate or auto-register against, so it fails fast. It reflects an upstream OAuth2 provider response that did not yield a usable user id.","triggerScenarios":"Passing a null Oauth2User into login, or an Oauth2User built from a provider response where the userId field is null/empty (e.g. provider returned an error payload or a differently shaped response).","commonSituations":"Misconfigured OAuth2 client (wrong scopes) so the id claim is absent; provider API change returning a different JSON shape; upstream token exchange failed silently and a null user was propagated into the service; custom auth code passing the raw profile without mapping userId.","solutions":["Fix the OAuth2 mapping so the provider's user id claim (sub/id) is copied into Oauth2User.userId before calling login.","Check the upstream OAuth2 token/userinfo exchange result and handle its errors before constructing Oauth2User.","Verify OAuth2 client scopes include the identifier claim and the provider is correctly configured.","Log/inspect the raw provider response to confirm the expected fields are returned."],"exampleFix":"// before\nOauth2User user = new Oauth2User();\nuser.setName(profile.getName());\ntokenResponse = accountService.login(user); // userId blank -> BizException\n\n// after\nOauth2User user = new Oauth2User();\nuser.setUserId(profile.getId());\nuser.setName(profile.getName());\nif (user.getUserId() == null || user.getUserId().isBlank()) {\n    throw new IllegalStateException(\"Provider did not return a user id\");\n}\ntokenResponse = accountService.login(user);","handlingStrategy":"validation","validationCode":"if (oauth2User == null || oauth2User.getUserId() == null || oauth2User.getUserId().isBlank()) {\n    throw new IllegalArgumentException(\"OAuth2 provider did not return a userId\");\n}","typeGuard":"boolean isValidOauth2User(Oauth2User u) {\n    return u != null && u.getUserId() != null && !u.getUserId().isBlank();\n}","tryCatchPattern":"try {\n    return accountService.login(oauth2User);\n} catch (BizException e) {\n    if (\"Oauth2UserNotFound\".equals(e.getCode())) {\n        return Response.status(401).entity(\"OAuth2 provider did not return a user id\").build();\n    }\n    throw e;\n}","preventionTips":["Validate the provider's userinfo response (id/sub present) before mapping to Oauth2User.","Handle OAuth2 token-exchange failures explicitly instead of propagating a null/empty user.","Configure client scopes so the identifier claim is always returned.","Unit-test the provider-to-Oauth2User mapper with real provider payloads."],"tags":["oauth2","auth","validation","spring-ai-alibaba-admin"],"backgroundTag":"empty-required-field","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}