public static Session getSession() {
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attr == null) {
return null;
}
HttpServletRequest request = attr.getRequest();
Session se = getSessionWithCache(request);
if(se==null) {
String tenant_id = request.getParameter("tenant_id");
if(StringUtil.isNotNull(tenant_id)) {
se = new Session();
se.setTenantId(Long.valueOf(tenant_id));
}else {
return null;
}
}
return se;
}
public static Session getSessionWithCache(HttpServletRequest request) {
if (cacheService == null) {
cacheService = (ICacheService) BeanContextFactory.getInstance().getBean(CacheService.class);
}
JSONObject jsonObject = getLoginUserInfo(request);
if(jsonObject == null) {
return null;
}
Long userId = jsonObject.getLong("userId");
Session se = (Session) cacheService.getMap(null, "userSession", userId);
if(se == null) {
se = createSession(jsonObject);
}
int busiYear = jsonObject.getIntValue("busiYear");
if(busiYear!=se.getBusiYear()) {
se.setBusiYear(busiYear);
cacheService.putMap(null, "userSession",userId, se);
}
return se;
}
public static JSONObject getLoginUserInfo(HttpServletRequest request) {
JSONObject jsonObject = null;
String userInfo = request.getHeader(USER_HEADER_KEY);
if (userInfo != null && !"".equals(userInfo)) {
byte[] userInfos = Base64.decodeBase64(userInfo.getBytes(Charset.forName("UTF-8")));
userInfo = new String(userInfos);
jsonObject = JSONObject.parseObject(userInfo);
}
return jsonObject;
}
**总结:这是获取阶段,也就是getSession(获取到userID随后去缓存里面查询信息),什么时间放进去这个userInfo的呢?必须网关啊,毕竟要给所有的请求都要加上去**