{"record":{"id":"ae88e9deed1f3b18","repo":"iflytek/astron-agent","slug":"unauthorized","errorCode":"UNAUTHORIZED","errorMessage":"UNAUTHORIZED","messagePattern":"UNAUTHORIZED","errorType":"error_code","errorClass":"BusinessException","httpStatus":401,"severity":"warning","filePath":"console/backend/commons/src/main/java/com/iflytek/astron/console/commons/util/RequestContextUtil.java","lineNumber":24,"sourceCode":"import com.iflytek.astron.console.commons.exception.BusinessException;\nimport org.apache.commons.lang3.StringUtils;\nimport org.springframework.web.context.request.RequestContextHolder;\nimport org.springframework.web.context.request.ServletRequestAttributes;\n\nimport jakarta.servlet.http.HttpServletRequest;\n\npublic final class RequestContextUtil {\n\n    private RequestContextUtil() {}\n\n    public static String getUID() {\n        HttpServletRequest request = getCurrentRequest();\n        if (request == null) {\n            throw new BusinessException(ResponseEnum.UNAUTHORIZED);\n        }\n        String uid = (String) request.getAttribute(JwtClaimsFilter.USER_ID_ATTRIBUTE);\n        if (StringUtils.isBlank(uid)) {\n            throw new BusinessException(ResponseEnum.UNAUTHORIZED);\n        }\n        return uid;\n    }\n\n    public static UserInfo getUserInfo() {\n        HttpServletRequest request = getCurrentRequest();\n        if (request == null) {\n            throw new BusinessException(ResponseEnum.UNAUTHORIZED);\n        }\n        Object userInfoObj = request.getAttribute(JwtClaimsFilter.USER_INFO_ATTRIBUTE);\n        if (userInfoObj instanceof UserInfo userInfo) {\n            return userInfo;\n        } else {\n            throw new BusinessException(ResponseEnum.UNAUTHORIZED);\n        }\n    }\n\n    public static HttpServletRequest getCurrentRequest() {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/commons/src/main/java/com/iflytek/astron/console/commons/util/RequestContextUtil.java#L6-L42","documentation":"RequestContextUtil.getUID reads the authenticated user id from the current HttpServletRequest attribute set by JwtClaimsFilter. It throws BusinessException(UNAUTHORIZED) when there is no bound request or the USER_ID_ATTRIBUTE is missing/blank — i.e. the call is not backed by a valid, JWT-authenticated session.","triggerScenarios":"Calling getUID outside an HTTP request context (async task, scheduler, startup), or inside a request whose JWT was missing, invalid, or whose filter did not populate USER_ID_ATTRIBUTE (blank uid).","commonSituations":"Invoking console services from background threads/MessageListeners; calling an endpoint excluded from the JWT filter chain; expired or tampered token rejected upstream but handler still runs; tests calling the util without RequestContextHolder setup.","solutions":["Ensure the call happens within an authenticated HTTP request (token present and validated by JwtClaimsFilter)","Check the endpoint is not bypassing the JWT filter (filter chain/interceptor registration)","For async/background code, capture and pass the uid explicitly instead of reading request context","In tests, seed RequestContextHolder with a MockHttpServletRequest carrying USER_ID_ATTRIBUTE"],"exampleFix":"// before\nString uid = RequestContextUtil.getUID(); // throws UNAUTHORIZED in async task\n// after\nString uid = currentUserId != null ? currentUserId : RequestContextUtil.getUID(); // pass uid into async context explicitly","handlingStrategy":"try-catch","validationCode":"HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) != null ? ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() : null;\nboolean authed = req != null && StringUtils.isNotBlank((String) req.getAttribute(JwtClaimsFilter.USER_ID_ATTRIBUTE));","typeGuard":"static boolean hasAuthenticatedUser() { ServletRequestAttributes a = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); return a != null && StringUtils.isNotBlank((String) a.getRequest().getAttribute(JwtClaimsFilter.USER_ID_ATTRIBUTE)); }","tryCatchPattern":"try { String uid = RequestContextUtil.getUID(); } catch (BusinessException e) { if (e.getCode() == ResponseEnum.UNAUTHORIZED) { return ResponseEntity.status(401).build(); } throw e; }","preventionTips":["Never call RequestContextUtil from async threads, schedulers, or message consumers — pass uid explicitly","Ensure every user-facing endpoint is registered behind JwtClaimsFilter","In tests, seed RequestContextHolder with a MockHttpServletRequest containing the user attributes"],"tags":["java","auth","jwt","http-context"],"backgroundTag":"authentication-required","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}