Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多账户认证下,集成jwt时tokenName被覆盖的问题 #583

Open
isixe opened this issue Feb 7, 2024 · 0 comments
Open

多账户认证下,集成jwt时tokenName被覆盖的问题 #583

isixe opened this issue Feb 7, 2024 · 0 comments

Comments

@isixe
Copy link

isixe commented Feb 7, 2024

对以下问题有疑问

在尝试用使用多账户时,按照文档集成了jwt功能,未手动为自定义 StpUserUtil 注入 StpLogicJwtFoxXxx 实现时,修改的tokenName可以生效。当手动注入后出现tokenName被覆盖的问题。

StpUserUtil代码样例,代码参考自同端多登陆

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

@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

	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";
            }
        });
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant