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

Cookie 中设置的 Token 没有前缀,但最后从获取 Token 时校验了前缀 #750

Closed
SkiffBoy opened this issue Mar 13, 2025 · 1 comment

Comments

@SkiffBoy
Copy link

SkiffBoy commented Mar 13, 2025

使用版本:

1.40.0

报错信息:

在开启从 Cookie 读取 Token 的开关 (is-read-cookie: true) 后, 后端 jsp 直接访问报错:

cn.dev33.satoken.exception.NotLoginException: 未按照指定前缀提交 tokenprefix=Bearer
	at cn.dev33.satoken.exception.NotLoginException.newInstance(NotLoginException.java:134)
	at cn.dev33.satoken.stp.StpLogic.getTokenValue(StpLogic.java:306)
	at cn.dev33.satoken.stp.StpLogic.getLoginId(StpLogic.java:964)
	at cn.dev33.satoken.stp.StpLogic.checkLogin(StpLogic.java:948)
	at cn.dev33.satoken.stp.StpUtil.checkLogin(StpUtil.java:341)

希望结果:

StpLogic.java 第 303 行

if (noPrefixThrowException) {
    throw NotLoginException.newInstance(loginType, NO_PREFIX, NO_PREFIX_MESSAGE + ",prefix=" + tokenPrefix, null).setCode(SaErrorCode.CODE_11017);
} else {
    tokenValue = null;
}

修改为

if (noPrefixThrowException) {
    // 从 cookie 获取的值是不带前缀的,不需要抛异常
    if (!getConfigOrGlobal().getIsReadCookie()) {
        throw NotLoginException.newInstance(loginType, NO_PREFIX, NO_PREFIX_MESSAGE + ",prefix=" + tokenPrefix, null).setCode(SaErrorCode.CODE_11017);
    }
} else {
    tokenValue = null;
}

复现步骤:

  1. Sa-Token 配置前缀
sa-token:
  token-prefix: Bearer
  token-name: Authorization
  is-read-cookie: true
  1. 结果
  • 前端 axios 请求时带上 前缀和 token 访问正常
  • 浏览器直接访问 jsp 地址报错
  1. 尝试过,但未解决的方法
  • 修改 setTokenValueToCookie 方法,保存时增加前缀,但是原始的 Request 拿不到 Cookie
public void setTokenValueToCookie(String tokenValue, int cookieTimeout){
    SaCookieConfig cfg = getConfigOrGlobal().getCookie();
    String tokenPrefix = getConfigOrGlobal().getTokenPrefix();
    String cookieValue;
    if(SaFoxUtil.isEmpty(tokenPrefix) ) {
        // - 如果没有配置前缀模式,直接保存
        cookieValue = tokenValue;
    } else {
        // - 如果配置了前缀模式,则拼接上前缀保存
        cookieValue = tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT + tokenValue;
    }
    SaCookie cookie = new SaCookie()
            .setName(getTokenName())
            .setValue(cookieValue)
            .setMaxAge(cookieTimeout)
            .setDomain(cfg.getDomain())
            .setPath(cfg.getPath())
            .setSecure(cfg.getSecure())
            .setHttpOnly(cfg.getHttpOnly())
            .setSameSite(cfg.getSameSite())
            .setExtraAttrs(cfg.getExtraAttrs())
            ;
    SaHolder.getResponse().addCookie(cookie);
}
  • request.getCookies()无法获取名称为 Authorization 的 Cookie,但是 request.getHeader("Cookie") 中有
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getCookies();
@click33
Copy link
Collaborator

click33 commented Mar 28, 2025

参考:
Image

@click33 click33 closed this as completed Mar 28, 2025
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

2 participants