多账户认证下,集成jwt时tokenName被覆盖的问题
- Dominant language
- Java
- Stars
- 19k
- Forks
- 2.9k
- PR merge metrics
- No merged PRs in 30d
Description
### 对以下问题有疑问
在尝试用使用多账户时,按照文档集成了jwt功能,未手动为自定义 StpUserUtil 注入 StpLogicJwtFoxXxx 实现时,修改的tokenName可以生效。当手动注入后出现tokenName被覆盖的问题。
StpUserUtil代码样例,代码参考自[同端多登陆](https://sa-token.cc/doc.html#/up/many-account?id=_7%e3%80%81%e5%90%8c%e7%ab%af%e5%a4%9a%e7%99%bb%e9%99%86):
```
public class StpUserUtil {
public static final String TYPE = "user";
//重写stpLogic自定义tokenName
public static StpLogic stpLogic = new StpLogic("user") {
@Override
public String splicingKeyTokenName() {
return super.splicingKeyTokenName() + "-user";
}
};
}
```
在全局配置中添加jwt配置,参考自[在多账户模式中集成 jwt](https://sa-token.cc/doc.html#/plugin/jwt-extend?id=_7%e3%80%81%e5%9c%a8%e5%a4%9a%e8%b4%a6%e6%88%b7%e6%a8%a1%e5%bc%8f%e4%b8%ad%e9%9b%86%e6%88%90-jwt)。
```
@Configuration
public class SaTokenJwtConfig {
@Bean
public StpLogic getStpLogicJwt() {
return new StpLogicJwtForSimple();
}
@Autowired
public void setUserStpLogic() {
StpUserUtil.setStpLogic(new StpLogicJwtForSimple(StpUserUtil.TYPE); //问题所在
}
}
```
### 具体问题
StpLogicJwtForSimple 通过 StpUserUtil 中的 setStpLogic 方法覆盖了原有 StpUserUtil (StpUtil) 类中自定义的 stpLogic,导致自定义 tokenName 失效,原代码参考自文档样例 [码云 StpUserUtil.java](https://gitee.com/dromara/sa-token/blob/master/sa-token-demo/sa-token-demo-case/src/main/java/com/pj/satoken/StpUserUtil.java)
```
public static void setStpLogic(StpLogic newStpLogic) {
stpLogic = newStpLogic;
...
}
```
### 解决方法
StpLogicJwtForSimple 继承自 StpLogic,因此需要在手动为 StpUserUtil 注入 StpLogicJwtFoxXxx 实现时,通过 StpLogicJwtForSimple 的 splicingKeyTokenName 方法重写 tokenName
```
@Configuration
public class SaTokenJwtConfig {
@Bean
public StpLogic getStpLogicJwt() {
return new StpLogicJwtForSimple();
}
@Autowired
public void setUserStpLogic() {
StpUserUtil.setStpLogic(new StpLogicJwtForSimple(StpUserUtil.TYPE){
@Override
public String splicingKeyTokenName() {
return super.splicingKeyTokenName()+ "-user";
}
});
}
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with StpUserUtil.setStpLogic and StpLogicJwtForSimple, then compare the custom StpUserUtil.stpLogic example with the multi-account JWT documentation. Reproduce the manual injection flow and trace why the custom tokenName is lost; done means JWT injection preserves the intended per-account tokenName behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- authentication
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100