{"record":{"id":"ab207c9ff616e5be","repo":"paascloud/paascloud-master","slug":"failed-to-decode-basic-authentication-token","errorCode":null,"errorMessage":"Failed to decode basic authentication token","messagePattern":"Failed to decode basic authentication token","errorType":"http","errorClass":"BadCredentialsException","httpStatus":401,"severity":"error","filePath":"paascloud-common/paascloud-common-core/src/main/java/com/paascloud/core/utils/RequestUtil.java","lineNumber":141,"sourceCode":"\t * @return the auth header\n\t */\n\tpublic static String getAuthHeader(HttpServletRequest request) {\n\n\t\tString authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);\n\t\tif (org.apache.commons.lang.StringUtils.isEmpty(authHeader)) {\n\t\t\tthrow new BusinessException(ErrorCodeEnum.UAC10011040);\n\t\t}\n\t\treturn authHeader;\n\t}\n\n\tpublic static String[] extractAndDecodeHeader(String header) throws IOException {\n\n\t\tbyte[] base64Token = header.substring(6).getBytes(\"UTF-8\");\n\t\tbyte[] decoded;\n\t\ttry {\n\t\t\tdecoded = Base64.decode(base64Token);\n\t\t} catch (IllegalArgumentException e) {\n\t\t\tthrow new BadCredentialsException(\"Failed to decode basic authentication token\");\n\t\t}\n\n\t\tString token = new String(decoded, \"UTF-8\");\n\n\t\tint delim = token.indexOf(GlobalConstant.Symbol.MH);\n\n\t\tif (delim == -1) {\n\t\t\tthrow new BadCredentialsException(\"Invalid basic authentication token\");\n\t\t}\n\t\treturn new String[]{token.substring(0, delim), token.substring(delim + 1)};\n\t}\n}\n","sourceCodeStart":123,"sourceCodeEnd":154,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-common/paascloud-common-core/src/main/java/com/paascloud/core/utils/RequestUtil.java#L123-L154","documentation":"RequestUtil.extractAndDecodeHeader throws BadCredentialsException(\"Failed to decode basic authentication token\") when the Base64 portion of the Basic auth header cannot be decoded. It indicates the credential payload after \"Basic \" is not valid Base64, so authentication cannot proceed.","triggerScenarios":"Calling extractAndDecodeHeader with a header like \"Basic !!!not-base64!!!\" — the substring after index 6 fails Base64.decode and Base64.decode throws IllegalArgumentException.","commonSituations":"Clients manually building the header with an unencoded username:password string instead of Base64, double-encoding mistakes, or a frontend sending the raw token without the scheme handled correctly.","solutions":["Encode credentials properly: Base64(username + \":\" + password) and send as \"Basic \" + encoded.","Check that the scheme prefix is exactly \"Basic \" (6 chars) so the substring taken is really the Base64 part.","On the client, use a standard helper (e.g. btoa/HttpHeaders) rather than manual concatenation."],"exampleFix":"// before\nheader = \"Basic admin:secret\"; // raw, not Base64\n// after\nString encoded = Base64.getEncoder().encodeToString(\"admin:secret\".getBytes(StandardCharsets.UTF_8));\nheader = \"Basic \" + encoded;","handlingStrategy":"try-catch","validationCode":"String payload = header.startsWith(\"Basic \") ? header.substring(6) : null;\nif (payload == null || !Base64.getDecoder().decode(payload.getBytes(StandardCharsets.UTF_8)).hasRemaining()) {\n    throw new BadCredentialsException(\"Authorization header payload is not valid Base64\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    String[] creds = RequestUtil.extractAndDecodeHeader(header);\n} catch (BadCredentialsException e) {\n    response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());\n}","preventionTips":["Build Basic headers with a library helper instead of manual string concat.","Unit-test header construction on the client.","Ensure the scheme prefix is exactly \"Basic \"."],"tags":["http","basic-auth","base64","authentication"],"backgroundTag":"invalid-argument-format","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}