/** * 使用JwtToken * 從cookie中獲取用戶ID方法 * @param request * @param tenantId * @return */ private String getUserIdFromCookie(HttpServletRequest request, String tenantId) { String token = CookieUtils.getCookie(request, "v_authorization"); if (token == null) { return null; } JwtToken jwtToken = null; try { jwtToken = Utils.decodeSignatureToken(token, this.cookieEncryptKey); } catch (Exception e) { return null; } long expire = jwtToken.getExpire().getTime(); long now = DateUtils.getGMTTimeInMillis().longValue(); if (now > expire) { return null; } Map<String, String> payload = jwtToken.getPayload(); String uid = (String) payload.get("uid"); if (!tenantId.equals(payload.get("tenantId"))) { return null; } if (!Utils.isAnonymityUser(uid)) { return null; } return uid; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
/** * 獲取用戶真實IP方法 * @param request * @return */ public String getIpAddress(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) { ip = request.getHeader("Proxy-Client-IP"); } if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) { ip = request.getHeader("WL-Proxy-Client-IP"); } if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) { ip = request.getHeader("HTTP_CLIENT_IP"); } if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) { ip = request.getRemoteAddr(); } return ip; }