Shiro + Redis 经常提示 UnknownSessionException:There is no session with id [XXX]
·
背景:整合Shiro和redis后,经常会提示There is no session with id报错,但不影响运行。
框架:使用的是shiro-redis框架。
解决方案:重新 DefaultWebSessionManager 类的 getSessionId 方法,在里面加上 session 是否过期的判断,如果过期就返回 null。
@Override
protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
String id = WebUtils.toHttp(request).getHeader(AUTHORIZATION);
//如果请求头中有 Authorization 则其值为sessionId
if (StringUtils.isNotBlank(id)) {
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
// 判断是否过期
if (!this.isSessionExpired(id)) {
return null;
}
return id;
} else {
//否则按默认规则从cookie取sessionId
return super.getSessionId(request, response);
}
}
Shiro + JWT 整合教程(找解决方法时看到的,挺不错):
GitHub - Smith-Cruise/Spring-Boot-Shiro: Shiro基于SpringBoot +JWT搭建简单的restful服务
更多推荐
所有评论(0)