Open
Description
使用版本:
1.40.0
报错信息:
在开启从 Cookie 读取 Token 的开关 (is-read-cookie: true
) 后, 后端 jsp 直接访问报错:
cn.dev33.satoken.exception.NotLoginException: 未按照指定前缀提交 token,prefix=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;
}
复现步骤:
- Sa-Token 配置前缀
sa-token:
token-prefix: Bearer
token-name: Authorization
is-read-cookie: true
- 结果
- 前端 axios 请求时带上 前缀和 token 访问正常
- 浏览器直接访问 jsp 地址报错
- 尝试过,但未解决的方法
- 修改 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();
Metadata
Metadata
Assignees
Labels
No labels