publicSession.java

	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);
		}
		//1. 从请求头中拿到用户ID
		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);
		}
		//若切换过年度,则需要更新缓存中userSession的年度信息
		int busiYear = jsonObject.getIntValue("busiYear");
		if(busiYear!=se.getBusiYear()) {
			se.setBusiYear(busiYear);
			cacheService.putMap(null, "userSession",userId, se);
		}
		return se;
	}
	//获取request请求头中的登录信息
	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);
			//{"pwd_status":"1","tenant_id":"18301","userType":"2","userName":"测试1","userId":"1813203","busiYear":"2019","userCode":"333"}
			jsonObject = JSONObject.parseObject(userInfo);
		}
		return jsonObject;
	}
**总结:这是获取阶段,也就是getSession(获取到userID随后去缓存里面查询信息),什么时间放进去这个userInfo的呢?必须网关啊,毕竟要给所有的请求都要加上去**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值