{"record":{"id":"86298ab31229a543","repo":"dromara/Sa-Token","slug":"namespace","errorCode":null,"errorMessage":"namespace 不能为空","messagePattern":"namespace 不能为空","errorType":"exception","errorClass":"SaTokenException","httpStatus":null,"severity":"error","filePath":"sa-token-core/src/main/java/cn/dev33/satoken/temp/SaTempTemplate.java","lineNumber":71,"sourceCode":"\t */\n\tpublic SaRawSessionDelegator rawSessionDelegator;\n\n\t/**\n\t * 在 raw-session 中的保存索引列表使用的 key\n\t */\n\tpublic static final String TEMP_TOKEN_MAP = \"__HD_TEMP_TOKEN_MAP\";\n\n\tpublic SaTempTemplate(){\n\t\tthis(DEFAULT_NAMESPACE);\n\t}\n\n\t/**\n\t * 实例化\n\t * @param namespace 命名空间，用于多实例隔离\n\t */\n\tpublic SaTempTemplate(String namespace){\n\t\tif(SaFoxUtil.isEmpty(namespace)) {\n\t\t\tthrow new SaTokenException(\"namespace 不能为空\");\n\t\t}\n\t\tthis.namespace = namespace;\n\t\tthis.rawSessionDelegator = new SaRawSessionDelegator(namespace);\n\t}\n\n\n\t// -------- 创建\n\n\t/**\n\t * 为指定 value 创建一个临时 token (如果多条业务线均需要创建临时 token，请自行在 value 拼接不同前缀)\n\t *\n\t * @param value 指定值\n\t * @param timeout 有效时间，单位：秒，-1 代表永久有效\n\t * @return 生成的 token\n\t */\n\tpublic String createToken(Object value, long timeout) {\n\t\treturn createToken(value, timeout, false);\n\t}","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/dromara/Sa-Token/blob/ac2c7f6e94a78573cf0bcb932dd8b04e68fad189/sa-token-core/src/main/java/cn/dev33/satoken/temp/SaTempTemplate.java#L53-L89","documentation":"SaTempTemplate (Sa-Token's temporary-token / temp-session feature) is constructed with a namespace used to isolate its stored data. Both constructors validate the namespace: the no-arg constructor uses DEFAULT_NAMESPACE, but the String constructor throws SaTokenException when given null or empty. This is a fail-fast guard against misconfiguration, not a runtime state error.","triggerScenarios":"Calling new SaTempTemplate(null) or new SaTempTemplate(\"\"); also a subclass calling super(namespace) with a namespace computed from config that is missing or blank.","commonSituations":"Building a custom multi-namespace temp-token setup where the namespace is read from a config key that is absent in some environment; passing a trimmed/empty string from an env variable.","solutions":["Pass a non-empty namespace string, e.g. new SaTempTemplate(\"order-service\")","Use the no-arg constructor new SaTempTemplate() if you do not need namespace isolation","Validate/derive the namespace from config with a sensible required-value check at startup"],"exampleFix":"// before\nString ns = System.getenv(\"TEMP_NS\"); // null in prod\nnew SaTempTemplate(ns);\n\n// after\nString ns = Optional.ofNullable(System.getenv(\"TEMP_NS\")).orElse(\"default\");\nnew SaTempTemplate(ns);","handlingStrategy":"validation","validationCode":"if (SaFoxUtil.isEmpty(namespace)) {\n    throw new IllegalArgumentException(\"TEMP_NS config missing\");\n}\nSaTempTemplate t = new SaTempTemplate(namespace);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate namespace config once at startup, not per request","Prefer the no-arg constructor when isolation is not needed","Fail fast in configuration classes (@Validated @NotBlank) so the app never boots with a blank namespace"],"tags":["sa-token","temp-token","validation","configuration"],"backgroundTag":null,"analyzedSha":"ac2c7f6e94a78573cf0bcb932dd8b04e68fad189","analyzedAt":"2026-08-14T14:36:10.271Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}