111 lines
3.3 KiB
Java
111 lines
3.3 KiB
Java
package com.wabestway.recall.util;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.wabestway.auth.api.user.ReqOrgInfo;
|
|
import com.wabestway.auth.api.user.ReqTenantInfo;
|
|
import com.wabestway.auth.api.user.ReqUser;
|
|
import com.wabestway.auth.api.user.TokenObj;
|
|
import com.wabestway.commons.exception.AlertException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.net.URLDecoder;
|
|
|
|
public class HeaderDataUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(HeaderDataUtil.class);
|
|
|
|
public HeaderDataUtil() {
|
|
}
|
|
|
|
public static ReqUser getUserData() {
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
.getRequest();
|
|
ReqUser reqUser = null;
|
|
try {
|
|
String header = request.getHeader(TokenObj._USER);
|
|
if (header != null) {
|
|
String str = URLDecoder.decode(header, "UTF-8");
|
|
logger.info("用户信息:{}", str);
|
|
reqUser = new Gson().fromJson(str, ReqUser.class);
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("用户信息转化异常", e);
|
|
throw new AlertException("未获取到header用户信息!");
|
|
}
|
|
|
|
return reqUser;
|
|
}
|
|
|
|
public static ReqOrgInfo getOrgInfoData() {
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
.getRequest();
|
|
ReqOrgInfo orgInfo = null;
|
|
try {
|
|
String header = request.getHeader(TokenObj._ORG);
|
|
if (header != null) {
|
|
String str = URLDecoder.decode(header, "UTF-8");
|
|
logger.info("机构信息:{}", str);
|
|
orgInfo = new Gson().fromJson(str, ReqOrgInfo.class);
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("机构信息转化异常", e);
|
|
throw new AlertException("未获取到header机构信息!");
|
|
}
|
|
|
|
return orgInfo;
|
|
}
|
|
|
|
public static ReqTenantInfo getTenantData() {
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
.getRequest();
|
|
ReqTenantInfo tenant = null;
|
|
try {
|
|
String header = request.getHeader(TokenObj._TENANT);
|
|
if (header != null) {
|
|
String str = URLDecoder.decode(header, "UTF-8");
|
|
logger.info("机构信息:{}", str);
|
|
tenant = new Gson().fromJson(str, ReqTenantInfo.class);
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("租户信息转化异常", e);
|
|
}
|
|
|
|
return tenant;
|
|
}
|
|
|
|
public static String getUserId() {
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
.getRequest();
|
|
String userId = null;
|
|
try {
|
|
String header = request.getHeader(TokenObj._USERID);
|
|
if (header != null) {
|
|
userId = URLDecoder.decode(header, "UTF-8");
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("用户ID信息转化异常", e);
|
|
}
|
|
|
|
return userId;
|
|
}
|
|
|
|
public static String getTenantId() {
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
.getRequest();
|
|
String tenantId = null;
|
|
|
|
try {
|
|
String header = request.getHeader(TokenObj._TENANTID);
|
|
if (header != null) {
|
|
tenantId = URLDecoder.decode(header, "UTF-8");
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("租户id信息转化异常", e);
|
|
}
|
|
|
|
return tenantId;
|
|
}
|
|
} |