package com.changhong.epc.rely.api.tool; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.system.prop.ChangHongProperties; import com.changhong.epc.rely.api.service.UserService; import com.iemsoft.framework.cloud.core.thread.Keys; import com.iemsoft.framework.cloud.core.thread.ThreadData; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.core.tools.SpringUtil; import com.iemsoft.framework.cloud.redis.service.CacheUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.math.NumberUtils; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; import static com.iemsoft.framework.cloud.core.thread.Keys.USER_LANGUAGE; @Slf4j public class FilterTool implements BaseConst{ private static final Map COOKIE_KEY; public static final Map FILTER_TYPE; static{ Map cookieKey = new HashMap<>(6); // tenantId cookieKey.put("tenantId" , TENANT_ID); // openId cookieKey.put("openId" , OPEN_ID); // 公司id cookieKey.put("companyId" , COMPANY_ID); // 公司主数据id cookieKey.put("companyMasterId", COMPANY_MASTER_ID); // token cookieKey.put("token" , TOKEN); //userName cookieKey.put("userName" , USER_NAME); //dataSourceId cookieKey.put("dataSourceId", DATA_SOURCE); cookieKey.put("Language" , USER_LANGUAGE); cookieKey.put("permType" , PERM_TYPE); COOKIE_KEY = Collections.unmodifiableMap(cookieKey); Map filter = new HashMap<>(3); /** * 对外 */ filter.put(FilterType.PUBLIC, request -> { log.debug("进入对外接口拦截"); String data = request.getParameter("data"); log.debug("对外接口传入参数为:{}", JSONTool.toJson(request.getParameterMap())); Map dataObj = JSONTool.toObj(data, Map.class); if(ObjectUtil.notEmpty(dataObj.get("tenantId"))) { String tenantId; ThreadData.set(TENANT_ID, tenantId = Objects.toString(dataObj.get("tenantId"), "")); if(ObjectUtil.notEmpty(dataObj.get("compId"))) { ThreadData.set(COMPANY_ID, NumberUtils.createInteger(Objects.toString(dataObj.get("compId"), "0"))); // Tenant tenant = // SpringUtil.getBean(CscApiService.class) // .getTenantAndCompany( // NumberUtils.createInteger(tenantId) // , company -> Objects.equals(company.getMasterCompanyId(), dataObj.get("compId").toString())); // if(ObjectUtil.notEmpty(tenant.getCompanys())) // ThreadData.set(COMPANY_ID, tenant.getCompanys().get(0).getId()); // ThreadData.set(COMPANY_MASTER_ID, NumberUtils.createInteger(Objects.toString(dataObj.get("compId"), "0"))); } } ThreadData.set(Keys.TENANT_ID, ThreadData.get(TENANT_ID)); log.debug("添加cookie数据:{}={}", Keys.TENANT_ID, ThreadData.get(Keys.TENANT_ID)); ThreadData.set(Keys.USER_ID, ThreadData.get(OPEN_ID)); log.debug("添加cookie数据:{}={}", Keys.USER_ID, ThreadData.get(Keys.USER_ID)); ThreadData.set(Keys.COMPANY_ID, Objects.toString(ThreadData.get(COMPANY_ID))); log.debug("添加cookie数据:{}={}", Keys.COMPANY_ID, ThreadData.get(Keys.COMPANY_ID)); }); /** * 对内 */ filter.put(FilterType.PRIVATE, request -> { log.debug("进入对内接口拦截"); Cookie[] cookie = request.getCookies(); if (cookie != null) { for (Cookie cook : cookie) { if (!ObjectUtil.empty(COOKIE_KEY.get(cook.getName()))) { log.debug("Cookie解密前key为:"+cook.getName()+"--value:"+cook.getValue()); ThreadData.set(COOKIE_KEY.get(cook.getName()), Des1.desDecrypt(cook.getValue(),Des1.SECRETKEY)); log.debug("{}:添加cookie数据:{}={}", HttpContextTool.getRequestUIR(request), COOKIE_KEY.get(cook.getName()), cook.getValue()); } } } try { SpringUtil.getBean(UserService.class).getUserInfo(); } catch (Exception e) { log.error(e.getMessage()); } log.debug("解密后---------->"); ThreadData.set(Keys.TENANT_ID, ThreadData.get(TENANT_ID)); log.debug("添加cookie数据:{}={}", Keys.TENANT_ID, ThreadData.get(Keys.TENANT_ID)); ThreadData.set(Keys.USER_ID, ThreadData.get(OPEN_ID)); log.debug("添加cookie数据:{}={}", Keys.USER_ID, ThreadData.get(Keys.USER_ID)); ThreadData.set(Keys.COMPANY_ID, Objects.toString(ThreadData.get(COMPANY_ID))); log.debug("添加cookie数据:{}={}", Keys.COMPANY_ID, ThreadData.get(Keys.COMPANY_ID)); //setCookie超时时间 if(ThreadData.get(TOKEN)!=null && !Objects.equals("",ThreadData.get(TOKEN))) { log.debug("添加token过期时间"); String key = String.format("token_%s" , ThreadData.get(OPEN_ID)); log.debug("当前缓存中登陆token:" + key); String time = SpringUtil.getBean(ChangHongProperties.class).getTokenTimeOut(); log.debug("当前超时时间:" + time); log.debug("当前token:" + ThreadData.get(TOKEN)); CacheUtils.set(key, ThreadData.get(TOKEN), Long.parseLong(time)); } }); /** * 主数据ftp */ filter.put(FilterType.MASTER, request -> { log.debug("进入主数据ftp接口拦截"); //暂时没有拦截目标 ThreadData.set(Keys.TENANT_ID, ThreadData.get(TENANT_ID)); log.debug("添加cookie数据:{}={}", Keys.TENANT_ID, ThreadData.get(Keys.TENANT_ID)); ThreadData.set(Keys.USER_ID, ThreadData.get(OPEN_ID)); log.debug("添加cookie数据:{}={}", Keys.USER_ID, ThreadData.get(Keys.USER_ID)); ThreadData.set(Keys.COMPANY_ID, Objects.toString(ThreadData.get(COMPANY_ID))); log.debug("添加cookie数据:{}={}", Keys.COMPANY_ID, ThreadData.get(Keys.COMPANY_ID)); }); FILTER_TYPE = Collections.unmodifiableMap(filter); } /** * 拦截类型 */ public enum FilterType{ /** * 对外 */ PUBLIC, /** * 对内 */ PRIVATE, /** * 主数据ftp */ MASTER, } /** * 拦截接口 */ @FunctionalInterface public interface FilterInterFace{ void filter(HttpServletRequest request); } }